Err, Erl Functions

See Also422P4GP              Example3FDV0W>Low

Returns error status.





After an error occurs, the Err function returns an Integer run-timeCYRM35 error code identifying the error.  The Erl function returns a Long that is the line number18F50XF of the line in which the error occurred or the line most closely preceding it.

Because Err and Erl return meaningful values only after an error has occurred, they are usually used in error-handling routines to determine the error and the corrective action.  Both Err and Erl are reset to 0 after any form of the Resume or On Error statement and after an Exit Sub or Exit Function statement within an error-handling routine.


Caution   If you set up an error handler using On Error GoTo and that error handler calls another procedure, the value of Err and Erl may be reset to 0.  To make sure that the value doesn't change, assign the values of Err or Erl to variables before calling another procedure or before executing Resume, On Error, Exit Sub, or Exit Function.


You can directly set the value returned by the Err function using the Err statement.  You can set the values for both Err and Erl indirectly using the Error statement.

The Erl function returns only a line number, not a line labelGH72Z1, located at or before the line producing the error.  Line numbers greater than 65,529 are treated as line labels and can't be returned by Erl.  If your procedure has no line numbers, or if there is no line number before the point at which an error occurs, Erl returns 0.

See Also

Err StatementGU30FH

Error Statement5AEF2Y

On Error Statement3PFT0KD

Resume StatementB4FKXK

Trappable Errors4LGWYDD

Err, Erl Functions Example

The example shows an error-handling routine.  If there are no additional errors, Err returns error number 11 (Division by zero) and Erl returns line 1030.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   1000  Dim A, B, C                 ' Declare variables.

   1010  On Error GoTo ErrHandler    ' Set up an error handler.

   1020  B = 1

   1030  A = B \ C                   ' Cause a "Division by zero" error.

   1040  Exit Sub


   ErrHandler:                       ' Error handler line label.

      MsgBox "Error number " & Err & " occurred at line " & Erl

      Resume Next

End Sub