PDA

View Full Version : Indentifying Current Module



smc2911
03-12-2006, 04:46 PM
The debugging I have been attempting in the EnableEvents thread has led me to the following question: can I get the name of the current Subroutine? It may seem like a silly question, since if you are editing a Subroutine, you obviously now what it's called, but I would find it useful for the following reason. I've been making liberal use of lines like this:
Debug.Print Now() & ": (getResult) " & Err.Descriptionand it would be useful to have something generic like this:
Debug.Print Now() & ": (" & <getsubname> & ") " & Err.Description which could then be cut and paste into any Subroutine without requiring further editing.

Sean.

P.S. If the same trick worked in Access, even better!

Jacob Hilderbrand
03-12-2006, 06:30 PM
I would just set a variable in each sub. So first set the global variable:


Global ModuleName As String

Then in each module just change the variable to the name of the module.

smc2911
03-12-2006, 06:37 PM
Nice approach! I'll try that, thanks.
Sean.

Jacob Hilderbrand
03-12-2006, 07:04 PM
Glad to help :beerchug:

Take Care

Cyberdude
03-12-2006, 08:05 PM
I'll take that request a step farther. Not only would I like the name of the executing macro, I'd like to see the list of calling macros that are currently waiting for the macro to complete. I didn't say that very well.
It's like this:
Macro "Main" calls a suroutine, which then calls a utility macro, which then calls another utility macro, which then crashes. I can get the name of the crashed macro, but because it's a frequently used utility, I have a problem knowing what macro(s) was calling it.
So one would have to have each calling macro store its name in a caller's list or stack. I suppose I could do that, but it would be nice if VBA has already done it. (Sigh)

smc2911
03-12-2006, 08:21 PM
That would be nirvana.
:grinhalo:

Jacob Hilderbrand
03-12-2006, 08:44 PM
Just set up a variable in each Sub that will store the calling macro.


Sub Macro1()

Call Macro2("Macro1")

End Sub

Sub Macro2(CallingMacro As String)

MsgBox CallingMacro

End Sub

Cyberdude
03-12-2006, 09:07 PM
Yeah, I've done variations of that, but it seems too inelegant. I hate to add arguments when they are otherwise not needed. But you're correct ... that's a way to do it. Also it gets messay when there are several callers in the chain. Thanx for the suggestion.

johnske
03-13-2006, 05:25 AM
If you need this, it might pay you to look at Stephen Bullens "CallTree" addin http://www.bmsltd.ie/excel/ :)

Bob Phillips
03-13-2006, 05:50 AM
Better still, in debug, check the call stack

View>Call Stack or Ctrl-L

smc2911
03-14-2006, 02:54 AM
Now that ctrl-L call stack is very tantalising! Now if only we could get access to that programmatically rather than just when the execution has been paused in debug mode. CallTree also looks like an interesting tool, but I'd still love something like do be able to use something like Application.CallTree.Previous.Name!

Sean,

Bob Phillips
03-14-2006, 03:55 AM
Now that ctrl-L call stack is very tantalising! Now if only we could get access to that programmatically rather than just when the execution has been paused in debug mode.

Not possible I am afraid, you would have to roll your own.