Solved

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

Posted on 2002-07-10
4
156 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bit defender blocks good applications 2 83
Adding to a VBA? 6 63
Run code from text file in vb 1 57
Child Form in front 4 39
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…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

863 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

23 Experts available now in Live!

Get 1:1 Help Now