Solved

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

Posted on 2002-07-10
4
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 105
to transfer string from C lanaguage to VBA 4 85
VBA - If Bookmark = "XXBOOKMARKXX" then 15 67
Dinamic report to Crosstab query 9 60
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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 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…

751 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