[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

Name of Function, Sub, Property

Is there any way to retrieve a functions name at runtime?

Sub DoSomething()
'Instead of
MsgBox "This message is sent to you from: DoSomething"
'something like
MsgBox "This message is sent to you from: " & GetCurSubName
'would be nice
End Sub
0
richtsteig
Asked:
richtsteig
  • 3
1 Solution
 
mcriderCommented:
This question has been asked many times...

The only way to do this is to set a global variable with the name of the function that is currently running.

For example:

Global RunningFunction As String

Function MyFunct1(X As String) As String
   RunningFunction="MyFunct1"
   MsgBox "This message is sent to you from: " & GetCurSubName
End Function


Function GetCurSubName() As String
   GetCurSubName = RunningFunction
Exit Function

--------------------------------------

But that just uses unnecessary commands... Use instead:


Global GetCurSubName As String

Function MyFunct1(X As String) As String
   GetCurSubName = "MyFunct1"
   MsgBox "This message is sent to you from: " & GetCurSubName
End Function


Cheers!
0
 
deightonCommented:
Y do U need a public var?
0
 
mcriderCommented:
You only need a public or global Var if you want to reference the calling function/subroutine from another function/subroutine.

For example:

Global RunningFunction As String

Function MyFunct1(X As String) As String
   RunningFunction="MyFunct1"
   MsgBox "This message is sent to you from: " & RunningFunction
   If Not Err = 0 then
      ErrorRoutine Err
   Endif
End Function

Sub ErrorRoutine(eNum as Long)
   Msgbox "Error Number "+Cstr(eNum)+" occurred in module "+RunningFunction
End Sub


Cheers!


0
 
mcriderCommented:
Thanks for the points!


Cheers!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now