Solved

Programmatically Identify Current Executing Sub/Procedure

Posted on 2004-03-23
11
437 Views
Last Modified: 2013-12-25
In my generic error handlers, I would like to be able to identify the current Procedure/Sub within which an error has occurred.  Is there any programmatic way to obtain the current call stack information or to identify the current sub executing (or perhaps something equivalent to the current line # in the code )?
0
Comment
Question by:jd576
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 27

Expert Comment

by:Dabas
ID: 10663927
Hi jd576,
VB.NET? C#? VB6?

Dabas
0
 

Author Comment

by:jd576
ID: 10664460
VB6
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10664527
jd576,
This is my own Error Handler, which I have used since VB4

Sub GeneralErrors(SubName As String, ErrNum As Integer)

Dim Msg$, Response As String
    Msg$ = Error$(ErrNum)
    Response = MsgBox("Error: " & Str$(ErrNum) & " " & Msg$ & " in " + SubName + " module. End Program?", vbYesNo + vbCritical)
    If Response = vbYes Then
        End
    End If
 End Sub

Every Function and Procedure starts with
On Error goto Error

And finishes with

Error:
    GeneralErrors "Procedure name goes here", Err.Number
    Resume Next

Dabas
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:Dabas
ID: 10664542
jd576,
If you also want to include the line where the error ocurred, you will have to number all the lines with a label, or at least those lines that are likely to produce an error.
The variable Erl provides the Line information

You can then change the above to
    GeneralErrors "Procedure name goes here, Line: " & Erl, Err.Number

Dabas
0
 

Author Comment

by:jd576
ID: 10664638
I missing something here.  Are you passing SubName, or are you programmatically obtaining SubName?   It is the "procedure name goes here"  that I want to be able to identify and code.   In my present code I have to enter this value manually for each specific sub, then pass it to the error handler, which also writes all error info to a Log file.  When an error occurs, I wish to be able to determine the sub in which it occurs programmatically and then pass it to the error handler -- avoiding rewritting the source sub name manually into each sub as I have been doing.  It is a big program, and I use the log file to track obscure errors that users cannot explain.

I suppose what am asking is this:  are the current SubName and Module available at run time?  How do I call them?  I know err.name, err. number, etc.  It is essentially the type of info that appears in the Call Stack.

Also, when you say set line number with label, can you give an example?  I am not sure how to do this.  

Thanks
0
 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 125 total points
ID: 10664659
jd576,
The answer is no, they are not available at run time. (They are in VB.NET!!!!)
What I have done is created my own AddIn to the VB IDE.
By clicking on an icon on my customized toolbar, I automatically create the On Error Goto Error line, and the lines at the bottom, including the correct sub name.

Dabas
0
 
LVL 27

Expert Comment

by:Dabas
ID: 10664669
jd576,
> Also, when you say set line number with label, can you give an example?  
> I am not sure how to do this.  

Public Sub MySub

On Error GoTo Error
10      Dim i as Integer
20      For i = 1 to 10
30            Debug.Print i
40      Next i
50      Debug.Print 1

Exit Sub
Error:
      msgbox Erl
End Sub

Dabas
0
 
LVL 6

Expert Comment

by:mmusante
ID: 10665204
Here a commercial solution to your problem (I didn't find a free one sorry) I use it in my company and it saved me a lot of times ...
(http://www.sharewaresoft.com/VBGuard-2000-download-2829.htm)
0
 
LVL 6

Accepted Solution

by:
mmusante earned 125 total points
ID: 10665285
I just found a similar free add-in here (http://www.downloadfreetrial.com/development/deve6022.html) it adds the procedure name but not the line-numbers and it doesn't make a copy of the original project too
0
 

Author Comment

by:jd576
ID: 10673399
Thanks.  I will split this one for the correct answer -- NO -- and reference to an add-in solution that seems to work.
0
 

Author Comment

by:jd576
ID: 10673400
Thanks.  I will split this one for the correct answer -- NO -- and reference to an add-in solution that seems to work.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB6 Compile Compatibility Issue 4 121
using Access 8 83
MS Access Form Control Background Color Change Depending On How Long The String Text Length Is 18 95
Advice in Xamarin 21 109
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question