Solved

Numbering code lines and then return that line number in error handling?

Posted on 2002-07-10
4
157 Views
Last Modified: 2010-05-02
How do I number lines and then return that line number in error handling?
0
Comment
Question by:BLWWLB
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 7144007
in VB this is NOT possible, in VB.net this functionality is given automatically.
Sorry
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7144043
You number code lines by manually adding line numbers at the beginning of the logical line or on a blank line.  (Or you can use an app to automatically add line numbers for you.)

To reference these line numbers in the error trappign routine, use the keyword ERL, such as msgbox "Error in procedure xxx at (or after) line number " & erl
0
 
LVL 5

Expert Comment

by:rpai
ID: 7144053
You can do something like this in your code:-

Private Sub Test()
Dim x as Integer
Dim y as Integer
Dim sError as String

On Error Goto ErrHandler
  sError = ""
  sError = "Initializing x"
  x = 1
  sError = "Initializing y"
  y = 0
  sError = "Performing Division"
  x = x/y
  sError = "Printing"
  Debug.Print x
Exit Sub

ErrHandler:
  Err.Raise Err.Source, Err.Desription & vbcrlf & "Error When: " & sError, Err.Number

Exit Sub
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 50 total points
ID: 7144132
Using ERL:

private sub ErrorTest()
  ' test the error line variable
  on error goto Error_Trap

  dim a as integer
10 msgbox "starting test"
20 debug.print "will this generate an error?"
30 debug.print "how about this?" & a
   a=20
50 debug.print "or this: " & a
55 a=0.5
60 debug.print "or this: " & 5/a
70 ' line number without code
  a=0
  debug.print "or maybe this: " & a/0
  exit sub

Error_Trap:
  msgbox "Error in ErrorTest at line " & erl
  resume next
end sub

You should get an error at line 60 and line 70, but the 70-error is really two lines below.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

808 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