Can a Sub/Function print the names of its callers?

G

Guest

If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers. Line numbers would be great too if that is
possible.

Example:

sub Main()
call Sub
end sub

sub Sub1()
call sub2(3)
end sub

Sub Sub2(n as long)
if n > 2 then
'Code to print
' Sub 2 called with value n
' from Sub1 at line 2
' from Main at line 2
end
end if
end sub
 
G

Guest

Check out the UserName property of the Application object.

Apparently I am not understanding your reply. Application.UserName returns
the name of my computer, not the names in the sub/functuion call chain.
 
J

James Ravenswood

If I detect an error in a Sub/Function, I would like to be able to print
the sequence of callers.  Line numbers would be great too if that is
possible.

Example:

sub Main()
        call Sub
end sub

sub Sub1()
        call sub2(3)
end sub

Sub Sub2(n as long)
    if  n > 2 then
        'Code to print
        '    Sub 2 called with value n
        '   from  Sub1 at line 2
        '   from Main at line 2
        end
    end if
end sub

You can always "remember" the caller if its name is not exposed:

Dim caller As String

Sub main()
caller = "main"
Call sub1
End Sub

Sub sub1()
MsgBox caller
'do other stuff
End Sub

In fact, you could even implement a Stack and have each sub push its
name onto the stact prior to calling another sub and poping the name
after the other sub's return.
 
G

Guest

You can always "remember" the caller if its name is not exposed:

Dim caller As String

Sub main()
caller = "main"
Call sub1
End Sub

Sub sub1()
MsgBox caller
'do other stuff
End Sub

In fact, you could even implement a Stack and have each sub push its
name onto the stact prior to calling another sub and poping the name
after the other sub's return.

Thanks for your reply. I was hoping to use the capability to help generate
a message when a routine somewhere down a call chain detected an unexpected
error. It would be used with already written routines.

I know that VBA must already maintain a call stack. I was hoping that, in
it might contain caller names & perhaps line numbers and that I could
access info in the stack. It sounds like there is no such capability.

You are, of course, correct that I could maintain my own stack but that
would require code before and after each call which would be difficult for
subs and much more clumsy when using functions.
i = f(3 + g(4)) + h(3.14159, SomethingElse)

I was hoping that being an intrepreted rather than compiled language (at
least to a degree) VBA might include such a capability.
Thanks anyway.
 
G

Guest

You can always "remember" the caller if its name is not exposed:

Dim caller As String

Sub main()
caller = "main"
Call sub1
End Sub

Sub sub1()
MsgBox caller
'do other stuff
End Sub

In fact, you could even implement a Stack and have each sub push its
name onto the stact prior to calling another sub and poping the name
after the other sub's return.

Thanks for your reply. I was hoping to use the capability to help generate
a message when a routine somewhere down a call chain detected an unexpected
error. It would be used with already written routines.

I know that VBA must already maintain a call stack. I was hoping that, in
it might contain caller names & perhaps line numbers and that I could
access info in the stack. It sounds like there is no such capability.

You are, of course, correct that I could maintain my own stack but that
would require code before and after each call which would be difficult for
subs and much more clumsy when using functions.
i = f(3 + g(4)) + h(3.14159, SomethingElse)

I was hoping that being an intrepreted rather than compiled language (at
least to a degree) VBA might include such a capability.
Thanks anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top