Error Handling Routine

D

dave h

Hi,
In the code below, I created a small simulation of what is happening in a
larger context. Of course this is bad code, but I wanted to force an error
to get at the error handling problem. If I run this code without my error
handler (comment out the On Error Goto...) then Access returns a message box
for a runtime error of 91 and its description. With my error handling
enabled, my message box returns an error of 0 and no description. This is
the same behavior I get in my actual project. What am I doing wrong?
Thanks, Dave H.

Private Sub btnTest1_Click()
On Error GoTo Error_Handler

Dim rst As Recordset 'force an error
Forms("frmTest1").Controls("txtTest1") = rst.Fields("xx")

Exit_Procedure:
Exit Sub
Error_Handler:
On Error Resume Next
MsgBox "error Number " & Err.Number & ", " & Err.Description,
Buttons:=vbCritical
Resume Exit_Procedure
End Sub
 
M

Mark

Get rid of the On Error Resume Next, or put it after the Msgbox; it's
clearing the error number before you get a chance to use it.
 
D

dave h

Thanks Mark,
Kind of annoying however, as I took that error handling routine from the
WROX book Access 2003 VBA and they clearly use the On Error Resume Next and
then use a msgbox to display err.number and err.description. Sure glad you
guys are around. Dave H.
 
P

PC Datasheet

Sussman and Smith are too good to have done that!

Reread your book slowly and carefully. Check the context of what they are
doing.
 
D

dave h

I didn't mean to sound like I was passing judgement on the experts - because
I'm definitely an Access novice. And, the book is generally quite helpful.
BTW, this one is written by some other folks Cardoza, Hennig, Seach &
Stein - who also seem to be very competent. So, clearly I could be
misintrepeting something about the context - but I'm having trouble seeing
what that is. And this is not about beating an issue to death - I'm still
wondering if I'm missing some important point as I try to develop a good
error handling model that I can use over and over. If someone has some
insight, the book again is WROX Access 2003 VBA Programmer's Reference and
their final error handling model is on page 277. I have no doubt that I
could be misunderstanding how the Err.number display yields a valid result
when it comes shortly after the "On Error Resume Next". The only 2 lines of
code between the two are:
DoCmd.SetWarnings True
DoCmd.Hourglass False
Which they describe as cleanup items and should not restore a previously
cleared error code. They mention that the "On Error Resume Next", which is
the first line of "Error_Handler:" is used for "This overrides the normal
error handling and forces the code to contineue even if an error is
encountered".

Any insight will be appreciated. Thanks, Dave H
 
M

Marshall Barton

dave said:
In the code below, I created a small simulation of what is happening in a
larger context. Of course this is bad code, but I wanted to force an error
to get at the error handling problem. If I run this code without my error
handler (comment out the On Error Goto...) then Access returns a message box
for a runtime error of 91 and its description. With my error handling
enabled, my message box returns an error of 0 and no description. This is
the same behavior I get in my actual project. What am I doing wrong?
Thanks, Dave H.

Private Sub btnTest1_Click()
On Error GoTo Error_Handler

Dim rst As Recordset 'force an error
Forms("frmTest1").Controls("txtTest1") = rst.Fields("xx")

Exit_Procedure:
Exit Sub
Error_Handler:
On Error Resume Next
MsgBox "error Number " & Err.Number & ", " & Err.Description,
Buttons:=vbCritical
Resume Exit_Procedure
End Sub


The first thing you should do in the error handler code is
save the error number in a variable. There are too many
ways for the number to be reset (On Error) or vhanges (any
other error) for you to trust it after the handler does much
of anything.
 
G

Graham R Seach

Dave,

I just stumbled across this thread. I don't know who wrote that bit, but I
agree that it's pretty silly thing to do.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
---------------------------
 

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

Similar Threads

Calling Error Handling in VBA 1
Error Handling 5
Table Append gets error 3420 4
How can this be "no current record"? 8
Not in List Event 1
Error Handler 1
Tips for ACCDE and ACCDR 6
Custom ID Field 4

Top