PDA

View Full Version : Solved: Determine name of caller procedure



Benzadeus
07-27-2009, 05:18 AM
Hi,

I'd like to know if there is a function that returns the name of the procedure that invoked another one.

Let's suppose I run the code below from mdlCaller:

Sub mdlCaller()
Call mdlTest
End Sub

Sub mdlTest()

Dim sCaller As String

'Help needed here
sCaller = 'Code that returns "mdlCaller"

Debug.Print "This procedure was called by the procedure " & sCaller

End Sub

The only way I can do this is
Sub mdlCaller()
Call mdlTest("mdlCaller")
End Sub
Sub mdlTest(sCaller As String)

Debug.Print "This procedure was called by the procedure " & sCaller

End Sub

, but I don't want to pass arguments.

Is it possible?

mdmackillop
07-27-2009, 05:41 AM
How about using a Global variable

Option Explicit
Dim Caller As String

Sub test()
Caller = "Test"
Call msg
End Sub

Sub msg()
MsgBox Caller
End Sub

Benzadeus
07-27-2009, 07:36 AM
Instead using Caller = "Test"


I'd rather use a function that returns the sub that is running. It would be something like:
Caller = ThisSub.Name

Is there such function?

mdmackillop
07-27-2009, 07:42 AM
I just found this (http://etutorials.org/Microsoft+Products/access/Chapter+7.+VBA/Recipe+7.2+Create+a+Global+Procedure+Stack/) doing a Google Search. Maybe a bit OTT though

Benzadeus
07-27-2009, 07:49 AM
I just found this (http://etutorials.org/Microsoft+Products/access/Chapter+7.+VBA/Recipe+7.2+Create+a+Global+Procedure+Stack/) doing a Google Search. Maybe a bit OTT though

I agree. Well, maybe it is the best alternative for my case. I'll mark as solved.

Thanks for the help.

mdmackillop
07-27-2009, 08:02 AM
FYI You can get a Control name using Application.Caller, but it doesn't work with another sub in my test. If I come across a better way, I'll let you know.