Err.Number Property

Syntax

Err.Number

Description

A read-write property containing a long value that represents the error code for the last error generated.

Rules at a Glance

  • When a runtime error is generated within the program, the error code is automatically assigned to Err.Number.

  • The Number property is updated with an application-defined error whose code is passed as an argument to the Err.Raise method.

  • When using the Err.Raise method in normal code, your user-defined error codes can't be greater than 65536 and less that 0. (For an explanation, see the final note in Section 7.1.6 of the entry for the Err.Raise method.)

  • VBA error numbers in the range of 1–1000 are for its own trappable errors. In addition, error numbers from 31001 to 31037 are also used for VBA trappable errors. In implementing a series of application-defined errors, your error handlers should either translate application errors into VBA trappable errors or, preferably, assign a unique range to application-defined errors.

  • When using the Err.Raise method in ActiveX objects, add the vbObjectError constant. (–2147221504) to your user-defined error code to distinguish OLE errors from local application errors.

  • When control returns to the local application after an error has been raised by the OLE server, the application can determine that the error originated in the OLE server and extract the error number with a line of code like the following:

    Dim lError as Long
    If Err.Number And vbObjectError Then _
       lError = Err.Number XOr vbObjectError

Programming Tips and Gotchas

  • An error code is a useful method of alerting your program that a function within an ActiveX or class object has failed. By returning a number based on the vbObjectError constant, you can easily determine that an error has occurred. By then subtracting vbObjectError from the value returned by the object's function, you can determine the actual error code:

    If Err.Number < 0 then
       Err.Number = Err.Number - vbObjectError
    End If

  • You can create a sophisticated multiresult error-handling routine by using the Err.Number property as the Case statement within a Select Case block, taking a different course of action for different errors, as this snippet demonstrates:

    Select Case Err.Number
       Case < 0 
          'OLE Object Error
          Set oObject = Nothing
          Resume DisplayErrorAndExit
       Case 5
          'increment the retry counter and try again
          iTries = iTries + 1
          If iTries < 5 Then
             Resume RetryFunctionCall
          Else
             Resume DisplayErrorAndExit
          End If
       Case 20
          'we almost expected this one!
          Resume Next
       Case Else
          Resume DisplayErrorAndExit
    End Select

See Also

Err Object, Err.Raise Property, Chapter 6
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset