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 ...

Get VB & VBA in a Nutshell: The Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.