PDA

View Full Version : Find parent Sub Name



Movian
06-13-2011, 08:52 AM
Hi,
i have discovered that a sub is being called multiple times without need. To try and track down which subs are calling this sub i am wondering if there is a way to msgbox the parent subs name when the child sub is run

is there a command to get this ?

Such as

public sub form_onOpen()
call childsub
end sub
public sub childsub
msgbox parentsubname
end sub

HiTechCoach
06-13-2011, 08:01 PM
I could be wrong but in Access VBA there is no built in property or method to do what you want.

The only way I have every figured out to handle this is to pass the sub name from the call code as a parameter.


I current do this for a global error handled. Every procedure has a local variable with the procedure's name. This variable is passed to the error handler procedure. This way I can track what procedure generated teh error.

hansup
06-14-2011, 07:17 AM
The Access Cookbook includes a recipe which describes how to create and use a call stack: Recipe 7.2 Create a Global Procedure Stack I think that can allow you to do what you want. The downside is that you would have to alter every procedure so that it places its name on the stack when it starts, and removes its name from the stack when it finishes.

Decide whether that overhead is worth it to you. The Access Cookbook is available from Safari Online, in addition to the "dead trees" version. I also found it at etutorials.org, but I'm unsure whether that's a legal copy.

I would consider what I think HiTechCoach suggested.

Public Sub childsub(Option WhoCalledMe As String ='')
If Len(WhoCalledMe) > 0 Then
msgbox WhoCalledMe
Else
MsgBox "caller anonymous"
End If
End Sub

HiTechCoach
06-14-2011, 10:16 PM
Yep .... that is what I was suggesting.

orange
06-18-2011, 06:40 AM
Movian,
Here's an excerpt from FMC re call stack in a DEBUGGING article at
http://www.fmsinc.com/tpapers/vbacode/debug.asp


Call Stack [Ctrl L]

http://www.fmsinc.com/tpapers/vbacode/debug/vba-call-stack.jpgThe call stack keeps track of the procedure calling chain so you can easily see how you got to the current procedure through all the other procedures. Retrieve it under View, Call Stack, or press [Ctrl L].
From this dialog, you can click on any procedure and jump immediately to it. Before analyzing the details of the current procedure, it may be more important to understand how and why you got there since the problem may be there rather than in the current procedure.


Hope it's helpful.