Results 1 to 6 of 6

Thread: Capturing Function/Sub name

  1. #1

    Thread Starter
    Join Date
    Nov 1999
    Dover, NH


    I know that in Java you there is a method that captures the name of the method you are currently executing. This is great for logging the name of the method for errors. Is there anything like this in VB? I have been searching in vain for a while. Currently, I have had to create a string variable and reset it's value to the name of the Function or Sub each time I enter it. There must be a better way.

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    San Jose, CA


    Unfortunately there isn't a better way.


  3. #3
    New Member
    Join Date
    Nov 2003


    I think I have similiar kind of problem here. I am writing a function which I want this function to call another function which I will pass in as a variable. In other words, I want this function to execute many different functions at different time. So, how do I do this??? Pls help. Thanks in advanced

  4. #4

  5. #5
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Vancouver, Canada
    you could do something like this

    Private Sub Command1_Click()
    MsgBox CallByName(Me, "SayMessage", VbMethod, "hello")
    End Sub
    Function SayMessage(strMessage As String)
    SayMessage = MsgBox(strMessage)
    End Function


  6. #6
    Fanatic Member
    Join Date
    Jun 2003

    Well what I do is have a general errortrap subroutine and anytime I have an error I send it parameters, and one of the parameters is the sub or function the error occured in. I use MZTools and I write the error handler in there and then I just click a button and it produces the code for me so it is very easy to manage.

    VB Code:
    1. 'This would be how I would have a sub
    2. Private Sub lblProgress1_Click()
    4.     On Error GoTo ErrHandler
    6.     'the code here for the sub
    8. ErrHandler:
    9.     If Err.Number <> 0 Then
    10. 'Erl stores the actual line number of where the error occured
    11. 'which comes with MZTools        
    12. ErrorTrap Err.Number, Err.Description, Erl, Me.Name, "lblProgress1_Click"
    13.         Exit Sub
    14.     End If
    15. End Sub
    17. 'This is how my errortrap looks
    18. Private Sub ErrorTrap(ByVal Error As Long, _
    19.     ByVal sDescription As String, _
    20.     ByVal ErrLine As String, _
    21.     ByVal sForm As String, _
    22.     ByVal sFunction As String)
    25.     MsgBox "Error occured on form " & sForm & " in function " & sFunction & " on line " & ErrLine & vbCrLf & sDescription, vbOKOnly + vbCritical, "Error " & Error
    26. End Sub

    You can also have a custom message instead of using the default message by sending a string instead of Err.Description. You can also do a case switch on certain Error values if you like, which is what I do on certain screens. So I have a general ErrorTrap for each form in my project, but if you really want to get general you can always put the errortrap in a module and set it as public which I think is not such a good idea because I would think you would want to be as specific as you can when it comes to error handling for many reasons.
    Last edited by Maldrid; Dec 4th, 2003 at 10:43 PM.
    Motto: Anything for a laugh.

    Getting second place only means you are the first loser to cross the finish line.

Posting Permissions

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

Click Here to Expand Forum to Full Width