ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   On Error Command (https://www.excelbanter.com/excel-programming/375157-error-command.html)

Jason Zischke

On Error Command
 
Hi all,

I was wondering if there was a way to do some sought of global On Error
Command because all I need is for it to pop up with the same message and to
go throughout all of my forms & modules and put on the On Error Command and
with the Error Trapping in all of my proceedures?

Jason

NickHK

On Error Command
 
Jason,
There no simple "On Error Call ThisRoutine" that you can use in VBA.
But if the top most (first) routine has an error handler active, with the
"On Error GoTo [Handler]" syntax, errors will bubble up to from called
routines, if they do not deal with the error themselves.
You can see what happens if you comment out the line
On Error GoTo CommandButton1_Error

If you need error handling added to all your routines, I believe MZTools
provides a simple way of this.
http://www.mztools.com/index.htm

Private Sub CommandButton1_Click()
On Error GoTo CommandButton1_Error

Call FunctionWithHandler
Call FunctionWithOutHandler
'Call AnyOtherRoutine

Exit Function
CommandButton1_Error:
Select Case Err.Number
Case vbObjectError + 514
MsgBox "Error from " & Err.Source
Resume Next
Case vbObjectError + 515
MsgBox "Error from " & Err.Source
Case Else
MsgBox "Error from " & Err.Source
End Select
End Sub

Private Function FunctionWithHandler() As Long
On Error GoTo FunctionWithHandler_Error
'Force an error
Err.Raise vbObjectError + 513, "FunctionWithHandler", "Handled in this
routine"
'Force an error
Err.Raise vbObjectError + 514, "FunctionWithHandler", "Not handled in this
routine"

FunctionWithHandler = 1

Exit Function
FunctionWithHandler_Error:
Select Case Err.Number
Case vbObjectError + 513
'Handle the error somehow
Resume Next
Case Else
'Pass the error up to the next active error handler
Err.Raise vbObjectError + 514, "FunctionWithHandler", "Not handled in
this routine"
End Select
End Function

Private Function FunctionWithOutHandler() As Long
'Force an error
Err.Raise vbObjectError + 515, "FunctionWithOutHandler", "Not handled in
this routine"
FunctionWithOutHandler = 1
End Function

NickHK

"Jason Zischke" wrote in message
...
Hi all,

I was wondering if there was a way to do some sought of global On Error
Command because all I need is for it to pop up with the same message and

to
go throughout all of my forms & modules and put on the On Error Command

and
with the Error Trapping in all of my proceedures?

Jason





All times are GMT +1. The time now is 04:36 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com