Generic error handler

Meltdown

Registered User.
Local time
Today, 22:39
Joined
Feb 25, 2002
Messages
472
Below is a simple error handler that I usually wrap most of my sub code in.

Is it possible to have this error handler called without hard-coding it into every sub? - so all my sub code runs in an error-handling wrapper automatically.

Thanks
Melt


Code:
On Error GoTo Error_Handler

'Run code here

Exit_Handler_Click:
    Exit Sub

Error_Handler:
    MsgBox Err.Description
    Resume Exit_Handler_Click
 
The short answer is no.

The long answer yes, if there is no error handler defined in a sub, then the error is passed up the call stack to the next sub that has an enabled error handler. So in the following code . . .
Code:
Sub Test1
On Error GoTo handler

   Test2

final:
   Exit Sub

handler:
   MsgBox err.Description
   Resume final
End Sub

Sub Test2
   Debug.Print 1 / 0  [COLOR="Green"]'generates division by zero error[/COLOR]
End Sub
The error handler in Test1 will handle the error in Test2.
 
Use an addin called mz tools. It lets you make a hotkey that inserts your error handler in vba.
 
Thanks for the replies.

So, if I have Form with 10 subs, is the recommendation to add an error handler to each sub, or just add one and let the error event bubble-up?

Regards
Melt
 
To each, his own.

For me, I develop for unsophisticated (Access) users. I usually have error handling on all but the most simple routines. In addition to displaying an error message on the screen, I also write the error, routine, form/report, time and user logon name to an error log table. This helps when I get a call "something went wrong when I clicked the button". Routine perusal of the log table also shows up such things as where users are doing things I'd never thought they would do.
 

Users who are viewing this thread

Back
Top Bottom