On Error Resume Next

G

Guest

i have an error trapper that goes something like this:

private sub MySub()
on error goto Err_MySub

some code here
exit sub

Err_MySub:
msgbox err.number & ": " & err.description
resume next
end sub

does anyone know if there is a way to show in the msgbox what line of code
generated the error? or is there a way to get the line number of "Next" in
"resume next"?
 
A

Allen Browne

There's not a simple way to show which line threw the error.

It is possible label each line, e.g. 10, 20, 30, ... and then use ERL to get
the label of the offending line. However, this does affect execution speed,
so is generally useful for debugging only.

A better solution is to break out any part of the procedure that is likely
to error into another small proc. For example, if you are trying to refer to
a property that may not exist (such as the Description of a Field in a
TableDef), you might create a function to tell you whether the property
exists before trying to use it, rather than try to guess which line of your
main procedure generated the error:
Public Function HasProperty(obj As Object, strPropName As String) As
Boolean
'Purpose: Return true if the object has the property.
Dim varDummy As Variant
On Error Resume Next
varDummy = obj.Properties(strPropName)
HasProperty = (Err.Number = 0)
End Function
 
G

Guest

When you get an error message press Ctrl+Break, the code will stop in this line
msgbox err.number & ": " & err.description

After this line write
Resume
Press F8 key to step each line of the code, the reume will move you to the
location of the error.

After you fix the problem dont forget to remove the resume, either wise when
you get an error it will put you nto a loop.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top