Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    239

    Question Unanswered: Determine the sub and function name in VB

    Hi I need to determine the sub and function name in VB
    Can anyone please help.

    Code:
    Private Sub Next_Record_Click()
      	msgbox "This is Sub: " & <some code that retrieve the sub or function name>
    End Sub
    This should display the message: "This is Sub: Next_Record_Click"

    Thanks for any proposals

    Sincerely
    Vestigo

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    441
    I found this somewhere

    msgbox Module.name & " " & Module.ProcOfLine(Module.CountOfLines, vbext_pk_Get)

    BUT - I think it will only work in mdb's not mde's (which makes it a tad useless?), and I think it needs a reference to the Microsoft Visual Basic for Applications Extensibility N.N library.

    I think for practical purposes (errorhandling etc), hardcode the module/sub names, and use the above only for debugging - perhaps also with line numbers (see the freeware MZTools for how to add/remove very easily) and the Erl statement.

    Edit - btw - the above to be used in a form/reports class module
    Last edited by RoyVidar; 01-13-06 at 05:59.
    Roy-Vidar

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    You could probably interrogate the call stack too - I haven't really looked at this however I think I have read that it is fraught with complications and not necessarilly 100% reliable. Could be wrong - someone might have a magic bullet answer.
    Testimonial:
    pootle flump
    ur codings are working excelent.

  4. #4
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Provided Answers: 1
    What is your purpose for this information? If it's for error trapping, you could do something similar to what my last client had in place and create a custom error handling sub.

    Basically it was a global sub that accepted name of the sub, error number and optional descriptor (useful for tracking a specific location of error). The sub created an entry into an MSSQL database where one could view all errors from all applications in real time.

    Perhaps you could leverage something similar.
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  5. #5
    Join Date
    Nov 2002
    Location
    Norway
    Posts
    239

    Thumbs up Yes, error traping.

    Your right. The purpose of this was (mainly) error trapping.
    I pass the information trough to a global routine, include a number of other information (such as variables). The Error trapping routine send this information as a text file via FTP to my server. My server detect that a new error has been received and inform programmer instantly via SMS.

    I used Roy Vidars proposal and hardcoded the sub/function name into the code. (No big deal actually, just a job that needed to be done.)

    Thanks for advise.

    Sincerely Vestigo

  6. #6
    Join Date
    Dec 2003
    Posts
    268

    How I do it

    I too am assuming you are using this for some type of error handling. How I go about doing this is with some constant values.

    One the module name, or form, or class i declare a name
    Code:
    const CLASSNAME = "MyFancyClass"
    Then for each method (function or sub) i add yet another const
    Code:
    private sub DoSomethingClever(ByVal Whattodoitto as object)
    
    on error goto ErrHandler
    private const METHODNAME = "DoSomethingClever"
    
    ... a bunch of wonderfully clever code
    
    ExitHere:
    exit sub
    
    ErrHandler:
    msgbox Err.number & ": " & err.description, classname & ":" & methodname
    This works for us. I dont know of a function call to pull the current method and object.

  7. #7
    Join Date
    Dec 2003
    Posts
    268

    On a side note

    I'd love to see how the error trapping is being implemented through a method. I've given that some thought, but never had any time to implement it.

  8. #8
    Join Date
    Apr 2003
    Location
    Alabama, USA
    Posts
    154
    Vestigo,

    In almost all of my procedures I try to log errors that are generated to a table. But your method is definately more sofisticated than my simlpe routine!!

    But short to keeping a constant in the procedure to store the procedure name, I had always typed the procedure name manually into a string that is sent to a error logging function that adds information to my error logging table.

    There is a VBA tool (MZTools) that, among other things, can automatically add a procedure header and error handling. This helps me to standardize my error handling. Obviously some procedures require some tweaking to trap certain errors.

    The biggest help if that it willl automatically fill in the module name and procedure name as a string via constants that you use when you set it up. If you prefer to use constants as mjweyland suggested, you can set it up to do that as well.

    Here's how a simple error handler can be set up;
    Code:
    On Error GoTo ErrMsg
    
      {PROCEDURE_BODY}
    
    ExitHere:
      Exit {PROCEDURE_TYPE}
    
    ErrMsg:
      MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure {PROCEDURE_NAME} of {MODULE_TYPE} {MODULE_NAME}"
      Resume ExitHere
    http://AccessDB.Info

    You live and learn. At any rate, you live. - Douglas Adams

Posting Permissions

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