Solved

Programmatically Identify Current Executing Sub/Procedure

Posted on 2004-03-23
11
430 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now