Error handling shortcut?

B

BruceM

I prefer error handling that identifies the error clearly. For instance:

Private Sub Form_Load()

On Error GoTo ProcErr

****Code here****

ProcExit:
Exit Sub

ProcErr:
MsgBox "Error #" & Err.Number & ", " & Err.Description & " - Form_Load"
Resume ProcExit

End Sub

However, I would like to avoid the need to type:
MsgBox "Error #" & Err.Number & ", " & Err.Description & " - Form_Load"
every time. I realize that parts of this line are specific to the sub, but
is there a way I can use a shortcut for part of it, such as:
"Error #" & Err.Number & ", " & Err.Description
or even:
MsgBox "Error #" & Err.Number & ", " & Err.Description

Maybe a function or constant would do the trick, but I don't know how to
pull it off.
 
B

BruceM

Thanks for the link. I was puzzled at first because the Start menu folder
that is installed does not include a way to actually run the program, which
is done instead from within VBA. The explanation on that point was either
unclear or I just missed it.
I tried the default error handler in MZ Tools, and am puzzled by a couple of
things. Where I would have used:

ProcExit:
Exit Sub

and then, after the

MZ Tools uses:

On Error GoTo 0
Exit Sub

From what I can understand, On Error GoTo0 halts error handling for the
routine, but I'm not sure that's what I want. Or do I? By the time the
code reaches that point there is no more code to run anyhow, other than Exit
Sub.

Also, I start the sub with On Error GoTo ProcErr

At the end of the sub I use:

ProcErr:
MsgBox "Error #" & Err.Number & etc.
Resume ProcExit

End Sub

MZ tools does not have a Resume line after the MsgBox line. I assume that
without it the code proceeds to End Sub, but it is different from what I
have used, and seems a bit contrary to what I have read from Microsoft on
the subject, so I'm a bit unsure if the default MZ Tools error handling will
do what I have come to expect from an error handler.

I understand that I can customize the error handling routine, but I wonder
if I should.
 
D

Douglas J. Steele

You can customize what MZTools inserts as error handling to whatever you
want.

(Sorry, I don't have it installed on this machine, so I can't give you
instructions. If you can't figure it out, post back, and I'll try to find
time to check on my machine at home tonight)
 
B

BruceM

Thanks, I figured it out readily enough. I can customize it to the error
handling to which I have become accustomed (and which I understand). I just
wondered whether the MZ Tools default is a better approach. Specifically,
where I would use:

ProcExit:
Exit Sub

MZ Tools uses:

On Error GoTo 0
Exit Sub

Also, the MZ Tools default is not to have a Resume line after the MsgBox
line. I wasn't sure just why it isn't used.

However, none of that matters very much. Please don't go to any extra
trouble about the MZ Tools configuration, which I think I understand well
enough. My error handling routine works, and MZ Tools can do pretty much
all of the repetitive work of inserting it. I was just trying to understand
some of the code a little better than I do.
 
D

Douglas J. Steele

I typically put

ProcExit:
On Error Resume Next
Exit Sub

but only if I have code before the Exit Sub that might fail (closing
recordsets, checking values, etc).

I'd definitely want a Resume statement after the error handler displays the
message, so that the Err object is reset.
 
B

BruceM

OK, thanks. That clears it up. I see what you're saying about adding the
Resume Next line in some contexts. Sounds like it would be best to add that
as needed rather than automating its insertion. I will go with the Resume
line after the error message in all cases, as I have been doing.
Thanks again for the link to MZ Tools. It looks like it will be useful for
all sorts of stuff once I have read some more about what it can do.
 

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
proper code placement 4
why is my form dirty? 6
Error handling question 15
Error code 3078 1
Outlook events stop work randomly 0
Modify Date procudure 2
Before Update validation 2

Top