Solved

Programmatically Identify Current Executing Sub/Procedure

Posted on 2004-03-23
11
433 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Basic Excel Formatting error 4 106
vb6 connector to mongodb 2 84
Prevent user closing word document opened with VB6 6 74
IF ELSE Statement in Excel Macro VBA 16 64
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

773 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