Get procedure (sub/func) name

M

Manuel Soares

For troubleshooting purposes I want to be able to log the name of the
procedure which issued the error (as I currently am doing with the error
number and description). Is there a way to capture the sub/func name without
hard coding it in the error handler?

I found the following property, but can't seem to make it work:
EventProcPrefix.

Thanks in advance for your assistance!

Manuel
 
B

Banana

Manuel said:
For troubleshooting purposes I want to be able to log the name of the
procedure which issued the error (as I currently am doing with the error
number and description). Is there a way to capture the sub/func name without
hard coding it in the error handler?

I found the following property, but can't seem to make it work:
EventProcPrefix.

Thanks in advance for your assistance!

Manuel

While you indeed could do some reflection via VBE OM, it's quite limited
and ultimately has no connection with the error handling. The problem is
that in order to do anything with the VBE OM, you must know what the
line number of the module you are referring. So, to figure out the
procedure name, you do this:

Dim this As Module
Set this = CurrentProject.AllModules("Module1")

Debug.Print this.ProcOfLine(x, vbext_pk_Proc)


But you've traded in a problem for another problem... what is the number
of lines from top you're calling from? Well, there's a Find method that
can tell you, but to use it, you need to hand a target string... In
short, you've typed up a lot of code that essentially goes round the
robin to tell you the procedure name that you had to hard-code in the
Find() method. What about Erl? Indeed, you could do this:

Option Compare Database
Option Explicit

Property Get this() As Module
5
6 Set this = Modules("module1")
7
8 End Property

Private Sub test()
11
12 On Error GoTo proc_err
13
14 Debug.Print 1 / 0
15
16 Exit Sub
17
18 proc_err:
19
20 Debug.Print this.ProcOfLine(Erl, vbext_pk_Proc)
21 Debug.Print Erl
22
23 End Sub

But the thing is that you have to manually maintain the line numbers.
They were deprecated for a very good reason- they were unwieldy to
maintain especially in a large codebase, and the manner of counting here
is quite unusual here. It's normal to start over the counting per
procedure, but for the ProcOfLine() to work correctly, we must get the
actual line of the module, not whatever line number we may assign to
where the error may occur.


With regards to EventProcPrefix, this is in association with controls
and usually will give you the same information as contained in the
control's Name property. This is used in creating new event. For
example, when you add a Click event to the command button,
EventProcPrefix gives "Button1", which then becomes this:

Private Sub Button1_OnClick()

as the event handler. Of course, this won't help you if you need to know
which events for the same control fired.




May I recommend you trying out MZ-Tools? It's free and comes with a
utility to help build the error handling and will add the name of the
procedure as a hard-coded string for you.

MZ-Tools also has ability to automatically add in line numbers, as well.
However, I wouldn't really want to do this in a production code when the
far more simpler answer is simply to hard code the name of procedure in
the error handler:

Private Sub DoIt()

On Error GoTo Proc_Err

Debug.Print 1 / 0

Exit Sub

Proc_Err:

Msgbox "An Error occurred in DoIt Sub in Module1 Module."

End Sub


Best of luck.
 
R

RD

The only reliable way I've found is to have a generic log writing
function into which I pass the error number, description and, yes, the
name of the offending procedure which had been hard coded into the
error handler of that procedure.

I have never found this to be particulary onerous and use it quite a
lot in my apps.

Air code:
Function fFuncName() As Something
On Error GoTo ErrorHandler
Dim vRetVal as Variant
etc.
etc.
ExitPoint:
Exit Function
ErrorHandler:
vRetVal = fWriteLog("fFuncName", Err.Number, Err.Description, etc.)
Resume ExitPoint
End Function

fWriteLog(sProcName As String, lErrNum As Long, sErrDesc As String) As
Whatever
Etc.
End Function

Hope this helps,
RD
 

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

Top