Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the current procedure name and line number 3

Status
Not open for further replies.

sergiog

Programmer
Feb 17, 2003
24
MT
Hi

Can you get the current procedure name and line number in VB to insert in an error handler ?

cheers
sergio
 
Erl will give you the line number. As far as what sub your in, I put a separate error handler in each sub. Maybe someone else has a better solution far that part.

Tom
 
Thanks for the Erl part. About the procedure name, icluding a separate error handler for each sub is what i used to do.

i was wondering if there is way to be a better programmer and make my code more generic.

cheers
 
If your aim is to generate an error message that tells you which routine an error occured in, then the only way is to have an error handler in each routine (I believe).

The way I report errors is to include the Me.Name property in the error message (this gives the form name), then the Routine name, then the line number.

Eg, in Form_Load:

ErrorHandler:
MsgBox "Error in " & Me.Name & " in Form_Load, line " & Erl

Many people think that line numbers went out with the Ark but for debugging apps that clients are using I find them really useful. I wrote the following code to add and remove line numbers from code, if it's any use.

It takes into account conditional compilation statements, and other VB commands which can't be prefixed with a line number.

To use it, copy your source code into Clipboard then run this project, you can then choose whether to add or remove line numbers. The resulting code is copied back into clipboard so you can paste it back over the top of your original selection.


Begin VB.Form Form1
BorderStyle = 3 'Fixed Dialog
Caption = "Line Numbers - by andy.groom@dial.pipex.com"
ClientHeight = 6375
ClientLeft = 1665
ClientTop = 1785
ClientWidth = 8790
BeginProperty Font
Name = "Arial"
Size = 8.25
Charset = 0
Weight = 400
EndProperty
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 6375
ScaleWidth = 8790
ShowInTaskbar = 0 'False
Begin VB.CommandButton b_Del
Caption = "Remove line numbers"
Height = 495
Left = 6915
TabIndex = 2
Top = 750
Width = 1740
End
Begin VB.CommandButton b_Add
Caption = "Add line numbers"
Height = 495
Left = 6915
TabIndex = 1
Top = 165
Width = 1740
End
Begin VB.TextBox t_Code
BeginProperty Font
Name = "Lucida Console"
Size = 9
Charset = 0
Weight = 400
EndProperty
Height = 6075
Left = 120
MultiLine = -1 'True
ScrollBars = 3 'Both
TabIndex = 0
Text = "frm_LineNumbers.frx":0000
Top = 150
Width = 6675
End
End

Const NumSpace = 4 ' Number of padding spaces allowed for line numbers at the start of each line

Private Sub b_Add_Click()

Dim Conditional As Boolean
Dim Continuation As Boolean

b_Add.Enabled = False

n$ = t_Code.Text
b& = 10

Do While (n$ <> &quot;&quot;)
a& = InStr(n$ & vbCrLf, vbCrLf)
l$ = Left$(n$, a& - 1)
n$ = Mid$(n$, a& + 2)

If (Left$(Trim$(l$), 1) = &quot;#&quot;) Then Conditional = Not Conditional

If (Conditional = False) Then
If ((Continuation = True) Or (Trim$(l$) = &quot;&quot;) Or (Left$(l$, 1) <> &quot; &quot;) Or (Left$(Trim$(l$), 4) = &quot;Dim &quot;) Or (Left$(Trim$(l$), 6) = &quot;ReDim &quot;) Or (Left$(Trim$(l$), 7) = &quot;Static &quot;) Or (Left$(Trim$(l$), 1) = &quot;#&quot;) Or (Left$(Trim$(l$), 5) = &quot;Case &quot;) Or (Left$(Trim$(l$), 1) = &quot;'&quot;)) Then
' No line number...
l$ = Space$(NumSpace) & l$ & vbCrLf
ElseIf ((Left$(Trim$(l$) & &quot; &quot;, 7) = &quot;Return &quot;) Or (Left$(Trim$(l$), 8) = &quot;Exit Sub&quot;)) Then
l$ = l$ & vbCrLf
Else
l$ = Left$(CStr(b&) & Space$(NumSpace), NumSpace) & l$ & vbCrLf
b& = b& + 10
End If
Else
l$ = Space$(NumSpace) & l$ & vbCrLf
End If

If (Len(l$) < 3) Then
Continuation = False
Else
Continuation = (Mid$(l$, Len(l$) - Len(vbCrLf), 1) = &quot;_&quot;)
End If

NewN$ = NewN$ & l$
Loop

t_Code.Text = NewN$

Clipboard.Clear
Clipboard.SetText NewN$

b_Add.Enabled = True

End Sub

Private Sub b_Del_Click()

Dim Conditional As Boolean
Dim Continuation As Boolean

b_Del.Enabled = False

n$ = t_Code.Text
b& = 10

Do While (n$ <> &quot;&quot;)
a& = InStr(n$ & vbCrLf, vbCrLf)
l$ = Left$(n$, a& - 1)
n$ = Mid$(n$, a& + 2)

If (Left$(Trim$(l$), 1) = &quot;#&quot;) Then Conditional = Not Conditional

If (Conditional = False) Then
ln$ = Left$(l$, NumSpace)
If (ln$ = Space$(NumSpace)) Then
l$ = Mid$(l$, 5) & vbCrLf
Else
If (IsNumeric(ln$) = True) Then
l$ = Mid$(l$, NumSpace + 1) & vbCrLf
Else
l$ = l$ & vbCrLf
End If
End If
Else
l$ = Mid$(l$, 5) & vbCrLf
End If

If (Len(l$) < 3) Then
Continuation = False
Else
Continuation = (Mid$(l$, Len(l$) - Len(vbCrLf), 1) = &quot;_&quot;)
End If

NewN$ = NewN$ & l$
Loop

t_Code.Text = NewN$

Clipboard.Clear
Clipboard.SetText NewN$

b_Del.Enabled = True
End Sub


Private Sub Form_Load()
n$ = Clipboard.GetText
t_Code.Text = n$
End Sub


- Andy
_______________________________
&quot;On a clear disk you can seek forever&quot;
 
Both MZ-TOOLS and VB ASSISTANT (2 free add-ins) allows you to build and delete line numbers.

Robert Berman
Data Base consultant
Vulcan Software Services
thornmastr@earthlink.net
 
cheers thornmastr,

MZ-TOOLS is AMAZING !!!!!!!

go to mz-tools.com and download it everyone coz it's brilliant.

thanks again
sergio
 
Guess what? I just created this round circle out of wood, I'm thinking of calling it... &quot;The Wheel&quot;...

Still, it's more fun writing code yourself I reckon.

- Andy
_______________________________
&quot;On a clear disk you can seek forever&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top