How to have a Custom Error message instead of the Default Access ones.
There was a question on the forum wanting to change the default error message.
Firstly we need to know the error message we are wanting to trap and change.
You could use a MS Article on how to list all error codes or just google it but this table would be handy for other ones.
Error Codes
Code
Description
3022
The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index t
...
...
Now open your Form, open the Property Sheet and change to the 'Events' tab and find the 'On Error', click the ellipse (...) and choose Code Builder.
Add the following code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Debug.Print "DataErr = "; DataErr
End Sub
Now if you run your Form, the error appears, check the Immediate Window and get the error code from there.
Now that we have the error code we can use it, replace the previous code with:
Private Sub Form_Error (DataErr As Integer, Response As Integer)
Const conDuplicateKeyViolation = 3022
If DataErr = conDuplicateKeyViolation Then
MsgBox "There was a key violation."
Response = acDataErrContinue
Else
'Display a standard error message
Response = acDataErrDisplay
End If
End Sub
Now this can easily be changed for any other message.