Solved

Line Numbers in VBA Code?

Posted on 2002-05-06
6
1,625 Views
Last Modified: 2012-06-27
I have a rather large VBA subroutine I wrote.  I am trying to split it up, but until then, there are a few areas that might encounter an error.  I'd like to know whereabouts in the module the error occurred.  

Mind you, this is distributed as an MDE, so the user cannot get to the code, even during an error.  Right now, I have it put up a screen, with the module name, and Access/Jet Error number. Is there a way to tell *where* in the module it happened?  (Of course, I can go back and devise where it occurred, based on ErrNo, but I'm lazy that way)....

Thanks in advance.
0
Comment
Question by:dougp23
6 Comments
 
LVL 1

Accepted Solution

by:
n f earned 50 total points
ID: 6991845
As far as I know, you can't get the line number to be returned, but you can get around this by creating a local variable at the beginning of your sub and then changing it at specific points in your code.  Change the variable to tell you what's going on in your code at that point.

For example:
Sub MySub()
On Error GoTo errTrap

Dim errHere as string

errHere = "top"
start your code...
....
errHere = "updating tblOne"
more code...
....
etc.

Then in the error trap,
errTrap:
   Msgbox "Error number " & err.number & vbCr & _
       "(" & Err.Description & ") occured at " & _
       errHere
   Exit Sub


The more times you change the variable, the more specific you can be in where the error is occuring.
         

0
 
LVL 57
ID: 6992153
Nothing outside of what was already metnioned as your using a MDE, which has no source code that can be read.  

Whatever labeling you do, you'll need to do it on your own.

Jim.
0
 
LVL 4

Expert Comment

by:srauda
ID: 6992353
Yep.  Use it all the time and it saves big time and headaches when debugging.

Try the following:

sub Test()

     dim x integer, y integer

05   on error goto Test_Err
10   x = 10
20   y = 0
30   msgbox x/y
40   exit sub

Test_Err:
    msgbox "Error: " & err.number & vbcrlf & "Description: " & err.description & vbcrlf & "Line: " & erl

end sub

Erl is an undocumented function in vb and vba.  If the function or sub has no line numbers, Erl returns 0.



0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 4

Expert Comment

by:srauda
ID: 6992357
For vb, there is a line number generator, but I have yet to find one for Access.  Was thinking of writing one for myself.
0
 
LVL 57
ID: 6992386
srauda,

<<For vb, there is a line number generator, but I have yet to find one for Access.  Was thinking of writing
one for myself. >>

  FMS, Inc. includes one as part of their Total Access Detective.

Jim.
0
 
LVL 1

Author Comment

by:dougp23
ID: 6994190
Thanks!  Even though I started out asking forline #s, I think this solution is cleaner.
As far as line # generator, the Access Developers Book (by Litwin, et. al.) has a nice line # generator (requires you to export the module to text first, then re-import it after it adds #s).

Like I said, though, I like nfeldman's idea.
Thank you all.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

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

23 Experts available now in Live!

Get 1:1 Help Now