How can I tell what the calling event is? (1 Viewer)

bhavdahl

Registered User.
Local time
Today, 04:29
Joined
Feb 4, 2008
Messages
17
Hope this isn't a stupid question. But is there a feature of some kind in VBA that will tell me what event called my subroutine? ...other than setting some kind of flag in the calling procedure. Like is there anything that will tell you what the currently executing event is as your sub routine starts? ...something like that.
 

MarkK

bit cruncher
Local time
Today, 02:29
Joined
Mar 17, 2004
Messages
8,187
There's a call stack, so the sequence in which currently executing subroutines have been called is available. Set a breakpoint in your code, and hit Ctrl + L, or Menu->view->Call Stack, or put a button on your toolbar.

Is that what you are looking for?
 

bhavdahl

Registered User.
Local time
Today, 04:29
Joined
Feb 4, 2008
Messages
17
Thanks! But I was looking for a way to pass the name of a calling procedure to a subroutine, to do different things depending on which event called it for example.

A better way than setting some kind of flag that is. If there was one.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:29
Joined
Feb 19, 2002
Messages
43,550
If your sub does different things depending on what event called it, rewrite it!! Do a search on coupling and cohesion to understand proper coding techniques.

To summarize, if you need to do a, b, and c when called from x and a, c, and d when called from y then you should break up the code so that you can call a and c separately so they are not coupled unnecessarily with b and d.
 

ChrisO

Registered User.
Local time
Today, 19:29
Joined
Apr 30, 2003
Messages
3,202
Bhavdahl.

What precisely is it that you are trying to do?

At runtime you can not determine the name of the calling procedure, not even the active procedure. You can pay for it but, as far as I know, not get it for free.

Even passing the name of the calling procedure would not be sufficient. Even if the procedure was behind a Form, passing the name of the Form and the procedure name would not be sufficient. A Form name + Procedure name can not be guaranteed to be unique.

So I think we need more information.

Chris.
 

bhavdahl

Registered User.
Local time
Today, 04:29
Joined
Feb 4, 2008
Messages
17
Detail is probably gonna be difficult.

They are coupled necessarily in this case and its not all that messy.

Everything is behind one form so all event subroutines, etc are unique. Passing the name works. I was just thinking it might be useful in general to be able to tell that. If its not free then yeah, it isn't worth it.

Thanks.
 

bhavdahl

Registered User.
Local time
Today, 04:29
Joined
Feb 4, 2008
Messages
17
So yeah, it is stupid to do that. I ended up doing something different after rethinking it. Thanks a lot for all the comments! That helped a lot.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 10:29
Joined
Sep 12, 2006
Messages
15,725
out of interest, why do you need to know the calling proc? if it is just to determine which path to follow in the sub, just pass some suitable option indicator as you originally suggested
 

bhavdahl

Registered User.
Local time
Today, 04:29
Joined
Feb 4, 2008
Messages
17
I figured knowing the last proc that executed might be cleaner sometimes, easier to keep track of, than setting and resetting some kind of switch.
 

Users who are viewing this thread

Top Bottom