Click to See Complete Forum and Search --> : Capturing Function/Sub name
tpatten
Nov 29th, 1999, 08:22 PM
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.
MartinLiss
Nov 29th, 1999, 08:30 PM
Unfortunately there isn't a better way.
------------------
Marty
aileen
Dec 4th, 2003, 06:42 PM
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
MartinLiss
Dec 4th, 2003, 06:58 PM
Loop up CallByName.
BuggyProgrammer
Dec 4th, 2003, 06:59 PM
you could do something like this
[Highlight=VB]
Private Sub Command1_Click()
MsgBox CallByName(Me, "SayMessage", VbMethod, "hello")
End Sub
Function SayMessage(strMessage As String)
SayMessage = MsgBox(strMessage)
End Function
[Highlight=VB]
Maldrid
Dec 4th, 2003, 09:39 PM
tpatten-
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.
'This would be how I would have a sub
Private Sub lblProgress1_Click()
On Error GoTo ErrHandler
'the code here for the sub
ErrHandler:
If Err.Number <> 0 Then
'Erl stores the actual line number of where the error occured
'which comes with MZTools
ErrorTrap Err.Number, Err.Description, Erl, Me.Name, "lblProgress1_Click"
Exit Sub
End If
End Sub
'This is how my errortrap looks
Private Sub ErrorTrap(ByVal Error As Long, _
ByVal sDescription As String, _
ByVal ErrLine As String, _
ByVal sForm As String, _
ByVal sFunction As String)
MsgBox "Error occured on form " & sForm & " in function " & sFunction & " on line " & ErrLine & vbCrLf & sDescription, vbOKOnly + vbCritical, "Error " & Error
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.