Name of current Private Sub

J

James

Hello,

I am bringing in error reporting, when an error happens it emails me with
the Form Name, Err.Description, and I want the name of the Private Sub that
is running.

I can find Form Name and Err.Description easy but how do I find what the Sub
name is?

Or could I somehow find the control that has focus. But it is not always a
control or command button that is running code, for instance I have OnCurrent
running code.

Any ideas?

Many thanks

James
 
M

Martin

It looks like you need a universal error function or subroutine. I've read
about these but haven't had to use them (there's probably an article or two
on the web). The essential idea is that you create a function with arguments
that include the name of the subroutine calling it:

Sub myErrorRoutine(mySubName, myErr)
MsgBox myErr.Number & " " & myErr.Description & vbNewLine & "Error
happened in " & mySubName
End Sub

I've kept it simple but you could obviously use If/Then or Select/Case
statements to perform different actions depending on the error values and
which subroutine generated the error.

To use it, you call this subroutine from within every routine in your
project, e.g.

Sub Test()
....
Exit Sub
error_handler:
myErrorRoutine "Test", Err
End Sub
 
Top