Results 1 to 12 of 12
  1. #1
    Glenn_Suggs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    72

    Can I return a module name?


    Hello everyone.. Does anyone know a way to get the name of a currently running module? I want to pass it to an error routine so I can track it later. The name of the function or sub would be good as well if that's possible. Thanks in advance... Glenn

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    The module name is not needed (and I dont think you can) , since you can Ctrl-F the function...
    but you must write your own message:

    Code:
    sub MySub()
    
    on error goto ErrTrap
    
    'LOTS O CODE HERE
    
    exit sub
    ErrTrap:
    msgbox err.descripton, vbCritical, "MySub():" & err 
    end sub

  3. #3
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    If you do not have a copy of MZTools V3.0, you should get one. It adds Error Handling --and many other things -- with the click of a button.
    The utility is free and available here.

    example:

    From a recent post, I asked to have error handling added and this is one part of the code produced. It reports the error number and description and identifies the module invovled.
    Code:
    QuantityAllocated_Error:
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure QuantityAllocated of Module Module1"

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I do not know for sure but, I believe that when there is a Run Time exception, everything in the Stack is thrown back into the Heap so there would not be anything to look for. The closest thing I have seen to what you are describing would be to have your procedures and functions leave a bread crumb trail as they execute. You could use a global variable to store a string value as procedures fire. The procedures would concatenate to the global variable and a function would write the variable to a table.

    A quick search came up with
    http://stackoverflow.com/questions/6...ect-call-stack

    and the second answer in
    http://social.msdn.microsoft.com/for...tack-using-vba

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Glenn,

    Perhaps you could tell us more about your intended use and application. As I mentioned MZTools is a great utility, however if you are planning some sort of global, universal error handler, you may be better advised to test/purchase vbWatchdog from Wayne Phillips (it's in one of ItsMe's links also).

    For simple flow monitoring you can add these lines as you build your code.
    Code:
    Debug.Print "Enter Module YourModuleName"
    '
    '   888Your module's code here
    '
    Debug.Print "Exit Module YourModuleName"
    You could direct the output to a table if required. You could add a Timestamp if needed...

  6. #6
    amrut is offline Expert
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2012
    Location
    Dubai
    Posts
    614
    Want to add a bit to Orange's post regarding MZ Tools. Add line numbers to the code and modify the standard error message to include line number so that the exact location of error is known.
    Code:
    QuantityAllocated_Error:      MsgBox "Error Line: " & Erl & vbnewline  & Err.Number & " (" & Err.Description & ") in procedure QuantityAllocated of Module Module1"

  7. #7
    Glenn_Suggs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    72
    Yes... I'm trying to set up a global error handler. I wonder if the Call Stack dialog box data on the View menu can be accessed programmatically? It seems the information there must be stored in a table, even if it's one of the system objects. I looked around in the system objects but didn't see anything that looked like the Call Stack info. I guess it must be stored some other way. But if I could get the most recent entry in that list, I think that would be what I'm looking for.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by Glenn_Suggs View Post
    ...I guess it must be stored some other way....
    It would be interesting if you could find it. I recall a while back I found some code to go after the debug.print results.

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Glenn,
    Did you look at vbWatchdog from Wayne Phillips? Did you get a copy of MZTools?

    Here's a link from FMS with lots of info that you may or should be familiar with.

    Good luck, and let us know if/when you proceed.

  10. #10
    Glenn_Suggs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Posts
    72
    Yes thanks. I'm reading all about FMS, vbWatchdog, and MZTools..

  11. #11
    Monterey_Manzer is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2012
    Posts
    164
    It sounds like there are some great tools available for tracking code execution. However, if you just want the module name for some error trapping purposes, this works just fine for me.

    Code:
    Dim strModule as String
        strModule = Application.VBE.ActiveCodePane.CodeModule.Parent.name

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    @ Monterey_Manzer
    That is good to know

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 08-19-2014, 12:41 PM
  2. What is Module?
    By glen in forum Modules
    Replies: 1
    Last Post: 09-14-2012, 09:11 AM
  3. class module vs regular module
    By Madmax in forum Modules
    Replies: 1
    Last Post: 05-01-2012, 03:44 PM
  4. Module Help
    By gumbi17 in forum Modules
    Replies: 3
    Last Post: 10-20-2011, 03:38 PM
  5. Replies: 4
    Last Post: 05-16-2011, 04:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums