Microsoft Access 2010 Bible

(Rick Simeone) #1

Part III: More-Advanced Access Techniques


836


The Error event
Access provides a special Error event when running a bound form or report. The Error event
provides a nice way to trap an error that occurs in the database engine supplying data to the form
or report. You need to create an event procedure for the Error event to trap these errors. The pro-
cedure looks like one of the following, depending on whether it was a form or a report:

Sub Form_Error(DataErr As Integer, Response As Integer)
‘Insert error handler here
End Sub

Sub Report_Error(DataErr As Integer, Response As Integer)
‘Insert error handler here
End Sub

There are two arguments for these subroutines:

l (^) DataErr: DataErr is the error code returned by the Access database engine when an
error occurs. Note that the Err object is superseded by Error event and is not helpful
when this event is triggered by a problem with the data underlying the form or report.
You must use the DataErr argument to determine which error occurred.
l (^) Response: Response is set to either of the following constants by the procedure:
l AcDataErrContinue: Ignore the error and continue without displaying the default
Access error message.
l AcDataErrDisplay: Display the default Access error message. (This is the default.)
When you use AcDataErrContinue, you can then supply a custom error message or
handler in place of the default error message.
The following is a typical Form_Error event procedure:
Private Sub FormError(DataErr As Integer, Response As Integer)
Dim strMsg As String
Select Case DataErr
Case 7787 ‘OverwriteErr:
strMsg = “You lose. Click on OK to see”

& “updates from other people.”
MsgBox strMsg, vbOKOnly + vbInformation
Response = acDataErrContinue
Case 7878 ‘DataChangedErr:
strMsg = “Another user has changed this”
& “data while you were looking at it.”

& vbCrLf & “Click OK to see “ _
& “the other user changes.”
MsgBox strMsg, vbOKOnly + vbInformation
Response = acDataErrContinue
Case Else
‘Default for any other errors:
Response = acDataErrDisplay
End Select
End Sub

Free download pdf