One of the most frequently asked questions here at TheScripts is: Can I replace Standard Access Error Messages with my own? The answer is yes under certain circumstances and it involves the placement of code in a Form's Error() Event and setting a specific Parameter to either 1 of 2 values. Many Errors occur while your Form is active and it is within this context that you can replace the standard Access behavior when these Errors occur with behavior that is a little friendlier to the User. As previously stated, by attaching code to the Error() Event of a Form, your Procedure will be called whenever a Trappable Error occurs while the Form is running.
The Syntax for the call is as follows:
Sub Form_Error(DataErr As Integer, Response As Integer)
__1. DataErr - contains the Error Number for the Error that just occurred.
__2. Response - Allows you to specify how you want Access to handle the Error. If your code handles the Error to your satisfaction and you don't want Access to intervene or display its own message, place the value acDataErrContinue in Response. If you want Acces to display its own Error Message, place acDataErrDisplay in Response.
The following code traps 4 Errors that might pop up. In each case, the procedure replaces the Standard Access Error Message with its own. If an Error occurs that it hadn't planned on, the Procedure just passes the responsibility back to Access:
- Private Sub Form_Error(DataErr As Integer, Response As Integer)
-
Const conErrDataValidation = 3317
-
Const conErrDataType = 2113
-
Const conErrDuplicateKey = 3022
-
Const conErrNullKey = 3058
-
-
Select Case DataErr
-
Case conErrDataValidation, conErrDataType
-
'your Error Message here
-
Response = acDataErrContinue
-
Case conErrDuplicateKey
-
'your Error Message here
-
Response = acDataErrContinue
-
Case conErrNullKey
-
'your Error Message here
-
Response = acDataErrContinue
-
Case Else
-
' It's an unexpected error. Let Access handle it.
-
Response = acDataErrDisplay
-
End Select
-
End Sub