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
LVL 2
richtsteigAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
deightonprogCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.