Determining error context

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an error-trapping routine in an automated application (i.e.
non-user-interactive) that appends an entry to an ErrorLog table. I can
capture several elements of the error, including the error #, error
description, current form, current control, & time. I am missing one thing,
however (or at least that's all I know I am missing at this point....):

How can I determine the specific Sub or VBA line number where the error
occurred so that I can locate not only the nature of the error, but its
source?
 
Unfortunately, VBA does not expose the procedure name, so you need to
hard-code that into the call to your error logger.

There is a neat utility from www.mztools.com that allows you to configure
the error handler you want, and drop it into the current procedure
(including the hard-coded procedure name) by clicking a toolbar button.
Highly recommended.

Regarding the line that caused the error, it is possible to label the lines
in your procedure, and use ERL to return the most recent label passed in the
routine. ERL is actually a throwback to the old days when BASIC was an
interpreted language with line numbers. VBA does support it, with with a
huge performance penalty, so it is suitable for debugging only and needs to
be removed from the final application.

Another common approach is to temporarily bypass your normal error hanlder
for a line or 2 that is expected to fail, and use:
On Error Resume Next
DoSomethingThatMightFail
If Err.Number <> 0 Then ... 'handle the error you expected.
On Error Goto Err_Hander 'reinstate your normal error hander.

While that is occassionally useful, I much prefer to break out the line that
is likely to error into a separate procedure and not mess up the main proc
like that. For example, if you are testing a property that might not exist,
call a function to indicate whether it does rather than try to figure out
which line of the main proc generated the 3270 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
 
Thank you again, Allen. I am in the process of going through several apps to
add in the error context identification, and boing back through EVERY
procedure to hard-code the procedure name was beginning to look a little
daunting. Hopefully, the tool will help make this process a little faster.

Capturing the context did not seem particularly important until I started
developing a non-interactive app. Capturing just an error number is fine when
the user can identify what he was doing when it occurred, but it got a lot
more complex once the entire app was running unattended (it is an automated
app that downloads CSV files via FTP and imports the contents into a DB2
database).
 
Back
Top