PC Review


Reply
Thread Tools Rate Thread

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

 
 
NoSpam@aol.com
Guest
Posts: n/a
 
      13th Jul 2010
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

 
Reply With Quote
 
 
 
 
NoSpam@aol.com
Guest
Posts: n/a
 
      13th Jul 2010
On Tue, 13 Jul 2010 14:18:05 -0400, Ron Rosenfeld <(E-Mail Removed)> wrote:

>On Tue, 13 Jul 2010 13:43:35 -0400, (E-Mail Removed) wrote:
>
>>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

>
>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.
 
Reply With Quote
 
James Ravenswood
Guest
Posts: n/a
 
      13th Jul 2010
On Jul 13, 1:43*pm, NoS...@aol.com wrote:
> 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.
 
Reply With Quote
 
NoSpam@aol.com
Guest
Posts: n/a
 
      13th Jul 2010
On Tue, 13 Jul 2010 12:42:35 -0700 (PDT), James Ravenswood
<(E-Mail Removed)> wrote:

>On Jul 13, 1:43*pm, NoS...@aol.com wrote:
>> 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.


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.
 
Reply With Quote
 
NoSpam@aol.com
Guest
Posts: n/a
 
      13th Jul 2010
On Tue, 13 Jul 2010 12:42:35 -0700 (PDT), James Ravenswood
<(E-Mail Removed)> wrote:

>On Jul 13, 1:43*pm, NoS...@aol.com wrote:
>> 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.


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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
excel 2007 localized function names -> english names [JULO] Microsoft Excel Discussion 0 14th Apr 2008 06:24 PM
VBA to capture (print or list) names of all Sub's or Function macro's in Personal.xls EagleOne@discussions.microsoft.com Microsoft Excel Misc 7 12th Apr 2008 09:46 PM
Exposing bool to non-MS, non-C++ callers Bob Altman Microsoft VC .NET 11 25th Sep 2007 07:45 PM
How do I print only names and company names in a category? =?Utf-8?B?bGRtY2k=?= Microsoft Outlook Contacts 1 29th Jun 2007 10:37 PM
Finding procedure callers Chris Dunaway Microsoft VB .NET 6 20th Jul 2005 10:13 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:18 AM.