We help IT Professionals succeed at work.

Getting the calling function NAME at run time

yronnen asked
Medium Priority
Last Modified: 2010-05-03

I have a situation like this: I have some function, and when it starts I want to know which function called it.

For example, I have these functions F1() and F2() that call F3(). When I start F3 from F1 I want to get a message box
that says "Function F1() called me".

How can I do that? And I don't mean solutions like keeping a global string and fill the name of the current function whenever it starts. I want it to happen automatically!

Is it possible?
Watch Question


I think this might solve the problem
In the Global module declare
Global FunctionCalled as string
sub f1()
msgbox functioncalled
functioncalled="function F1"
end sub
sub f2()
msgbox functioncalled
functioncalled="function F2"
end sub
sub f3()
msgbox functioncalled
functioncalled="function f3"
end sub
sub main
end sub
I don't think this is possible in VB,  yronnen.
Beside you are loosing nothing when you use a global variable.

8) Bin Huwairib


What I mean is that I want it to be done automatically!
I don't want to go to each function and add a line with its name.

To yronnen
Can i see the part of code where you are facing this problem?
The global variable woudn't work, because depending on where you are in the program, other functions could also be setting this variable at the same time.
You would have to pass the FunctionName as a string to ensure that the called function knows who called it.

Paste this into a form, and Click it

Function CalledFunction(ByVal CallerFunctionName As String)
    MsgBox CallerFunctionName & " Called Me"
End Function
Private Sub Form_Click()
    CalledFunction "Form_Click"
End Sub


You don't understand what I mean. I DO NOT want to go to every function that calls F3() and call it using their names. I want to get their names automatically.


I understand what you mean, but I don't know of any way to do it - but why do you want to do this?  What is the actual predicament you are facing ?


Is that right?  That would mean VB was running more than one bit of code at once - I didn't think it could suddenly fly off to another routine unless there was a DoEvents - I never thought of this I'm scared.

FailSafe from NuMega can help you manage this problem, but...it does automatically add lines to each function to track the calling sequence.  Not what you want, but it does automate the process.

Also, under VB5 you can compile with symbols. Not sure it you can easily get to these symbols at run time (I think PSAPI.DLL under WIN32 has support to get this debug information)...but normally you would not ship a program with symbolic debug on.
Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.