Consulting

Results 1 to 12 of 12

Thread: Indentifying Current Module

  1. #1
    VBAX Regular
    Joined
    Mar 2006
    Posts
    44
    Location

    Indentifying Current Module

    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:
    [vba]Debug.Print Now() & ": (getResult) " & Err.Description[/vba]and it would be useful to have something generic like this:
    [vba]Debug.Print Now() & ": (" & <getsubname> & ") " & Err.Description[/vba] 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!

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    I would just set a variable in each sub. So first set the global variable:

    [vba]
    Global ModuleName As String[/vba]

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

  3. #3
    VBAX Regular
    Joined
    Mar 2006
    Posts
    44
    Location
    Nice approach! I'll try that, thanks.
    Sean.

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Glad to help

    Take Care

  5. #5
    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)

  6. #6
    VBAX Regular
    Joined
    Mar 2006
    Posts
    44
    Location
    That would be nirvana.

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Just set up a variable in each Sub that will store the calling macro.

    [vba]
    Sub Macro1()

    Call Macro2("Macro1")

    End Sub

    Sub Macro2(CallingMacro As String)

    MsgBox CallingMacro

    End Sub
    [/vba]

  8. #8
    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.

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    If you need this, it might pay you to look at Stephen Bullens "CallTree" addin http://www.bmsltd.ie/excel/
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Better still, in debug, check the call stack

    View>Call Stack or Ctrl-L

  11. #11
    VBAX Regular
    Joined
    Mar 2006
    Posts
    44
    Location
    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,

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by smc2911
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •