To create an event procedure that is executed when the Error event occurs, set the OnError property to [Event Procedure], and click the Build button.
Private Sub Form_Error(DataErr As Integer, Response As Integer)Private Sub Report_Error(DataErr As Integer, Response As Integer)
The Error event procedure uses the following arguments.
Argument |
Description |
DataErr |
The error code returned by the Err object when an error occurs. You can use the DataErr argument with the Error function to map the number to the corresponding error message. |
Response |
An intrinsic constant determining whether or not an error message is displayed. The Response argument can be one of the following constants. |
Constant acDataErrContinue
acDataErrDisplay |
Description Ignore the error and continue without displaying the default Microsoft Access error message. You can supply a custom error message in place of the default error message. (Default) Display the default Microsoft Access error message. |
You can’t cancel the Error event.
Error Event — Macros.
The following example shows how you can replace a default error message with a custom error message. When Microsoft Access returns an error message indicating it has found a duplicate key (error code 3022), this event procedure displays a message that gives more application-specific information to users.
Private Sub Form_Error(DataErr As Integer, Response As Integer) Const conDuplicateKey = 3022 Dim strMsg As String If DataErr = conDuplicateKey Then Response = acDataErrContinue strMsg = "Each employee record must have a unique " _ & "employee ID number. Please recheck your data." MsgBox strMsg End IfSub