Custom error message code not working?

J

john

From http://www.databasedev.co.uk/custom-error-message.html I use:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
'If an error occurs because of missing data in a required field
'display our own custom error message

Const conErrRequiredData = 3314

If DataErr = conErrRequiredData Then
MsgBox ("Please ensure that you enter a First Name and Last Name")
Response = acDataErrContinue
Else
'Display a standard error message
Response = acdatadisplay
End If
End Sub

I tested it with the error code 3022 (double values). The strange thing is
that the custom message is shown, but that all other errors don't result in
the standard messages or any other message. When I omit the else clause, it
seems to be working fine. Custum message is shown for 3022 and the other
error's standard messages are shown as well.

Is something wrong with the above code or am I doing something wrong?
Thanks in advance,

John
 
J

Jeanette Cunningham

Hi john,
the confusing thing about errors is that there are 2 different types of
errors at least on an access form.
There are what I call "data errors" and what I call "VBA" errors (my
terminology) at least.

The code below only runs if the form detects any data errors
Const conErrRequiredData = 3314

If DataErr = conErrRequiredData Then
MsgBox ("Please ensure that you enter a First Name and Last Name")
Response = acDataErrContinue
Else
'Display a standard error message
Response = acdatadisplay
End If
End Sub
for example 3314, where a field is required, there are other common ones
that often crop like "you must select an item from the list"
or "no current record"
data error 3022 is "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 to permit duplicate
entries and try again."

The "data errors" are trapped by putting code like you have above in the
form's error event.

Other errors "VBA errors" come from errors while your code is running, or
while you are coding a form (and other reasons) for example if your code
tells Access to open form Beetles and these isn't a form called Beetles or
if you misspell the name of a control.
These errors appear from your code routines.
I wish I could suggest a good reference for learning about errors, but I
struggled with this myself. I never did find anything that explained it in a
way I could understand. I have just picked it up over the years.

Jeanette Cunningham
 
J

john

Thanks Jeanette for your reply,

I tested the code with error 3022. To force some other error I entered text
in a numeric field. With the else clause I get no message at all and when I
leave out the else clause I do get a standard Access message. When I don't
enter the text in the numeric field and force an duplicate key error, the
custom message is shown whether I leave the else clause in or out (of course
this is behavior as expected.)

I'll do some further checking.

john
 
J

Jeanette Cunningham

John,
there is a mistake in the code for error testing.
on this line
Response = acdatadisplay
change to
Response = acDataErrDisplay

Jeanette Cunningham
 
J

john

Great! That did the trick. I'll report the error to the owner of the
website.
Thanks again.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top