Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 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…

760 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

20 Experts available now in Live!

Get 1:1 Help Now