How to get Private sub Name in a String

AceBK

Registered User.
Local time
Today, 09:00
Joined
Dec 2, 2011
Messages
75
This might be a simple question, but I am trying to get the name of the Private sub into a text string. I have an error handler in my vba code, but when the error pops up, it does not specify where the error is, since the codes call different private subs. So what I am doing now is changing the title of the error message box to be the form name and the name of the Private sub, but I am not having any success. I can just copy and paste the title into the string, but I have possibly 500 different Private subs, so copying a line of code would be easier. This is what I have:

Code:
Private Sub DisableGetShippingTaxes()

'I removed my code, since it is not necessary for this question.

My_Error_Handler:
    Select Case Err.Number
        Case Else
            MsgBox _
                "An unexpected error has occurred.  The system's description " & _
                    "of this error is:" & vbCr & vbCr & _
                    "Error " & Err.Number & ": " & Err.Description, _
                vbExclamation, _
                [COLOR="Red"][B]Me.Module & "_DisableGetShippingTaxes"[/B][/COLOR]

Me.Module returns "Forms_Invoices", the name of the form, which is perfect. I tried me.Name, but that just gives me the name of the form, "Invoices". Now to get the name of the Private sub. Any help is greatly appreciated.
 
There is nothing in VBA to achieve what you want.

Just get MZ Tools and use its automatic Error Handler insertion.

BTW There is no need to put an error handler in every procedure. Some are so trivial that that adding error handlers is clutter. Errors in procedures with no handler will be passed up the calling chain until it finds an error handler.
 
There are a thousand ways to do this, none of them easily automated. If you search hard enough on the web, you might find something that helps you get inside the process stack (because when debugging, you can do a "View call stack".) However, it is complex code that deals with an area of memory where even the slightest accidental muck-about will crash your code hard.

The only way I have ever found to do this is, via methodical coding, to assure that anywhere that I have an error handler, I have a call to my common error handling routine that includes the name I want reported if that trap occurs.

If all you wanted to know was the control action that led to an event, you could show the potentially offending source based on the fact that a particular control was active at the moment, since you can do Screen.ActiveControl.Name to find out what has focus. In general, this is useful but not 100% of the time, since a control action could lead to a form event or a trap event that would muddle up the visibility of what you wanted, and the source of the trap would become obscured.

An "Old Programmer's Rule" applies here - "Access cannot tell you anything you didn't tell it first." In this case, if you don't give Access some usable markers, it won't be able to tell you where it is by remembering/parroting the last marker it passed.

Compiled code deals in internal addresses, not label names. Therefore, your code doesn't know where it is. It is even worse, since VBA code is semi-compiled and then interpreted so it isn't even bound to a fixed place in memory. And that means that it cannot tell you where it was in a meaningful manner even if you have access to the physical mapping information for running VBA modules.
 

Users who are viewing this thread

Back
Top Bottom