Results 1 to 13 of 13
  1. #1
    Join Date
    Jan 2003
    Posts
    81

    Unanswered: Name of the sub or function in code

    In a procedure, the statement "Me.Name" refers to the form a procedure belongs to. I need to capture the name of the procedure itself. I am looking for something of the like "Me.Procedure.Name". How to do it?

    Thanks,

    Bjorn

  2. #2
    Join Date
    Jan 2003
    Posts
    81
    oops. Sorry. This post belongs in Microsoft Access...

  3. #3
    Join Date
    Jul 2003
    Location
    Michigan
    Posts
    1,941
    Are you trying to find the name of the currently executing sub/function?

    I'm not sure if there's a way to do it in the manner you are looking for.
    I would declare a global variable, and at the beginning of each sub/function, I would put:
    gStrVar = "YourSubName"

    You'd get the same result, and you could reference the variable from anywhere.
    Inspiration Through Fermentation

  4. #4
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    I have been searching for a way to do that very thing. Are you doing this as part of an global error handler? That is what I was trying to do and after a few weeks of trying to figure it out I came across this article:

    http:/www.fmsinc.com/tpapers/vbacode/debug.asp

    It covers error handling and debugging in general, but there is a little code to create and track the call stack. The procedure names are manual, but once you implement it, it will show you all of the procedures that were called to reproduce the error.

  5. #5
    Join Date
    Jan 2003
    Posts
    81
    Bingo. Global error handler it is. I am pasting the same error code into every single procedure. An error in a procedure will call my error function ExplainError(err.number,err.description,form.name, sub.name). I must know which procedure in which module raised the error. If I first have to feed the name of the procedure into every sub.name in every procedure it will take me a week. I rather spend 2 weeks trying to figure out how to have Access hand it to me... I know it must live somewhere while the procedure is open and I will hunt it down, tag it and bag it!

    Thanks for the article.

    Bjorn

  6. #6
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    If you are able to figure it out, I would be VERY interested to know how you were able to do it. Can you post it or send me an email? I would appreciate it.

  7. #7
    Join Date
    Jan 2003
    Posts
    81
    The article seems to put a damper on the project though:

    taken from the article:

    "Unfortunately, while VB/VBA tracks the procedure call stack and lets you examine it under the View, Call Stack menu item while debugging, this information is not available to your code. The only way to generate this is to track it yourself."

  8. #8
    Join Date
    Feb 2004
    Location
    Chicago, IL
    Posts
    1,312
    That's why I gave up. I assumed the article was fairly current. However, Microsoft may have changed the way it works. I ran out of time researching the subject, and when I found this article it was the best idea I had seen so I created the error handler. Adding it to procedures you have already written is a very tedious job. But once it is done adding new procedures doesn't take that much time. And the information you get when an error occurs is great, assuming you haven't forgotten to Push or Pop a procedure name.

  9. #9
    Join Date
    Jan 2005
    Posts
    68

    insert procedure name into a module

    after reading your original message i searched all over the internet, as i am going into testing in a few weeks, and want the same thing. it seems that the only way to call the current procedure is to set a variable in it manually. this is ridiculous (i did find several other functions that read the procedure name)! it is not gracefull, but i put together a function that does work. I think that it could also be used to add error handling code, etc.
    Note: this code is fresh and not thoroughly tested. if anyone has any problems or advice on how to optimize it please let me know.

    Public strCurrProc As String
    Public Sub InsertProcName(mdlName As String)
    '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
    'this function cycles through a module, and inserts the appropriate '
    'string to set or clear the public variable [strCurrProc] to the '
    'module name and the procedure name. '
    '''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''
    Dim mdl As Module
    Dim introw As Integer
    Dim strRowText As String
    Dim i As Integer

    Set mdl = Modules(mdlName)
    introw = 1

    For i = 1 To mdl.CountOfLines
    strRowText = mdl.Lines(introw, 1)

    'check the line of text. if it is a procedure name then insert
    'a row and print the module name / procedure name
    If Left(strRowText, 10) = "Public Sub" Or _
    Left(strRowText, 15) = "Public Function" Or _
    Left(strRowText, 11) = "Private Sub" Or _
    Left(strRowText, 16) = "Private Function" Or _
    Left(strRowText, 3) = "Sub" Or _
    Left(strRowText, 8) = "Function" Then
    introw = introw + 1
    mdl.InsertLines introw, _
    "strCurrProc = """ & mdlName & " / " & strRowText & """"
    introw = introw + 1
    'if the line of text is the end of a procedure then clear the
    '[strCurrProc] variable
    ElseIf Left(strRowText, 7) = "End Sub" Or _
    Left(strRowText, 12) = "End Function" Then _
    mdl.InsertLines introw, "strCurrProc = """""
    introw = introw + 2
    Else
    introw = introw + 1
    End If
    Next i

    End Sub

    paste this code into a module, then open the module you want to update.
    type Call InsertProcName([Module Name])
    that should do it

    let me know how it works out
    Last edited by Frosty1; 02-09-05 at 05:32. Reason: formatting

  10. #10
    Join Date
    Feb 2005
    Location
    England.
    Posts
    232
    For general reror handling, I have also looked at this recently. Sorry to say that I found absolutley nothing on how to get the procedure name into anything.

    In the end, I went through the process of building the db with fixed names so I knew what had caused my error.

    A lot of over-head to set-up once you have established code, but easy to manage once the initial set-up has been done.

    Sorry I can't be of more help.

  11. #11
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    its a kludgey workaround, but you could get the same effect by storing the module name in a global variable


    private function DoSomething(WithSomething as integer) as integer
    if debug then strModuleName="DoSomething"
    .....

    end function

    Debug beign a global constant, if debug is set then store the module name

  12. #12
    Join Date
    Jan 2005
    Posts
    68
    i think the key was that we want a way to return the current procedure name, rather then just the module name. if there is a function to do this built in to access it seems to be a well kept secret.

  13. #13
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    Thats the point, like you I don't think there is an inbuilt way of retrieving what you want, so either don't do it, OR develop a workaround

Posting Permissions

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