Where to put my Error handling Code

P

Pamela

I have a control that needs a specific 5 digit number to be entered. I have
an input mask on this control requiring the 5 numbers but if the user doesn't
enter it correctly the system gives a very non-user-friendly error message.
I'd like to replace this message explaining again that it needs to be the
full 5 digit number. I tried entering it in AfterUpdate and OnDirty events
but it seems that the system's Error handling fires first and I still get
their error message. From where does this fire that I can replace it?? I
don't have any other code attached to this control and most other posts here
speak to handling the errors w/ other code. Just in case you need it, here's
my code and it may well need tweaking!
On Error GoTo AssnNumberErr
AssnNumberErr:
If Err.Number Then
MsgBox "Please enter the 5 digit CAP Number"
End If
Thanks so much!
Pamela
 
T

theDBguy

Hi Pamela,

You'll have to check for the specific error number or perhaps check the
Previous Control value to make sure the user was at that control but try
placing your code in the Form's OnError event.

Hope that helps...
 
M

Marshall Barton

Pamela said:
I have a control that needs a specific 5 digit number to be entered. I have
an input mask on this control requiring the 5 numbers but if the user doesn't
enter it correctly the system gives a very non-user-friendly error message.
I'd like to replace this message explaining again that it needs to be the
full 5 digit number. I tried entering it in AfterUpdate and OnDirty events
but it seems that the system's Error handling fires first and I still get
their error message. From where does this fire that I can replace it?? I
don't have any other code attached to this control and most other posts here
speak to handling the errors w/ other code. Just in case you need it, here's
my code and it may well need tweaking!
On Error GoTo AssnNumberErr
AssnNumberErr:
If Err.Number Then
MsgBox "Please enter the 5 digit CAP Number"
End If


Try using the Form's Error event:

Select Case DataErr
Case <the error number>
MsgBox "Please enter the 5 digit CAP Number"
Case Else
MesgBox DataErr & " - " & Error(DataErr)
End Select
 
T

theDBguy

Hi Pamela,

There is an example in your own code for that. Use:

Err.Number

In the forms OnError event, try this:

MsgBox Err.Number & " -- " & Err.Description

Hope that helps...
 
D

Dorian

Don't use an input mask!
Put code in the BeforeUpdate event to validate your form's content.
Set Cancel to true if the checks fail otherwise set cancel to false.
e.g.
If MyForm!MyNumber = <whatever>
set cancel to false
else
msgbox "your custom error message",,"Error"
Myform!Mynumber.setfocus
set cancel to true
end if
-- Dorian
"Give someone a fish and they eat for a day; teach someone to fish and they
eat for a lifetime".
 
P

Pamela

Hi Marshall,
If I do this on the Form's OnError Event, will this be THE error message
that pops up for EVERY error that may occur? I have other controls on this
form for which this message would not be appropriate. Also, you have listed
"Case <error number> -- what is this error number and where do I get it?
Someone else mentioned that I have it (Err.Number) in "my" code but the error
code I have listed here is just something I tried to copy and tweak out of an
Access Help book - it isn't based on my understanding. I assumed from my
book that Err.Number is a command, procedure, function (whatever) that Access
recognizes but it certainly doesn't specifically refer to anything I've done
 
T

theDBguy

Hi Pamela,

I tried to give you an example of code that you could put in the Form's
Error event to display the Error number generated when the user violates your
Input Mask.

Have you tried it?
 
P

Pamela

Sorry, DB, my knowledge is so minimal that you may give a great answer but
it'll take me time and more posts to understand it... LOL Now after your
last post, I figured out exactly what you intended for me to do and got an
answer of "0 --" which I assumed then was my error number. I tried to use
that w/ the code Marshall provided which seemed easy enough but I'm now
getting "Application defined or object defined error" when I intentionally
input only 4 digits. Any other help suggestions?? Here's my code now:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
'MsgBox Err.Number & " -- " & Err.Description
Select Case DataErr
Case 0
MsgBox "Please enter the 5 digit CAP Number"
Case Else
MsgBox DataErr & " - " & Error(DataErr)
End Select
End Sub
Thanks for your patience with me!
 
P

Pamela

Hi DB,
I took what you gave me and tried putting it back with my own code (from the
book LOL) and it almost worked!! My error message popped up but then I think
focus needs to be returned to that control so that the user can change it.
Right now, after clicking OK to my error message, the standard message also
opens which then returns focus to the control. I tried to add
Me.AssnNumber.SetFocus to the end of the the code, before the End If but it
doesn't seem to be working. Any suggestions?? Again, thanks so much for
your help - I fully recognize that you and everyone here is giving great
help, it's just my own learning that I'm stumbling over.
Pamela
 
T

theDBguy

Hi Pamela,

I think error number 0 means there's no error. Let's pretend that the actual
error number is 2501, then try this code in your form's Error event:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If Err.Number = 2501 Then
MsgBox "Sorry, you must enter a 5-digit code.", vbExclamation, "Wrong Code!"
Me.TextboxName.Undo
Me.TextboxName.SetFocus
End If
End Sub

(untested)
Hope that helps...
 
P

Pamela

So how do I get the real error code?? All it keeps giving me is "0 --". I
even tried double checking the standard Access error message but it doesn't
give a code - it just says that the entry is invalid for the input mask...
Thanks!
 
T

theDBguy

Hi Pamela,

I just did a quick test and was able to use something like this instead:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DataErr = 2279 Then
MsgBox "Sorry, you must enter a 5-digit code.", vbExclamation, "Wrong
Code!"
Me.TextboxName.Undo
Response = acDataErrContinue
End If
End Sub

Hope that helps...
 
T

theDBguy

Hi Pamela,

You're welcome. I should have fired my copy of Access a long time ago
instead of giving you advice from memory. As soon as I opened up Access and
created a test form, I immediately noticed that the error number was being
passed to the event handler and being reset right away. If you'll notice, we
are now testing for DataErr instead of Err.Number in the code.

Good luck with your project!
 

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