Number Property

Applies To

Err Object.

Description

Returns or sets a numeric value specifying an error. Number is the Err object’s default property.

Syntax

object.Number [= errornumber]

The Number property syntax has these parts:

Part

Description

object

Always the Err object.

errornumber

A Long integer representing a Basic error number or an OLE SCode error value.


Remarks

Because the Number property is the Err object’s default property, existing code that uses the Err function does not have to be revised. All of the following correctly return or set the Number property:


Err.Number = 5            ' Set the Err object's Number property to 5.= 5                    ' Set the Err object's Number property to 5..Print Err            ' Display the Err object's Number property..Print Err.Number    ' Display the Err object's Number property.

When returning a user-defined error from an OLE Automation object, set Err.Number by adding the number you’ve chosen as an error code to the constant vbObjectError. For example, you use the following code to return the number 1051 as an error code:


Err.Raise Number := vbObjectError + 1051, Source:= "SomeClass"

Not all Visual Basic host applications can create OLE Automation objects. See your host application’s documentation to determine whether it can create classes and OLE Automation objects. However, all host applications can create OLE Automation controllers, and should use code like the following when interpreting error codes returned from objects:


' Code to trap a specific VB object error.Err.Number = vbObjectError + 1051 Then
...    Err.Number - vbObjectError > 0 Then
    ' Other Visual Basic object-generated errors are between 0-65535.
    If Err.Number - vbObjectError < 65535 Then    
        ...        ' Code to handle Visual 
    End If        ' Basic object errors.Err.Number > 0 Then    
' If a VB object returns a system-defined error,there is no need to
' strip vbObjectError. 
    If Err.Number < 65535 Then
        ...
    End IfIf

The first If traps a specific object-defined error by combining it with the constant vbObjectError. Action can then be taken for the object’s error number 1051. The first ElseIf traps a range of object-defined errors. It subtracts vbObjectError from Err.Number, then examines the result to see if it is in the range for an object-defined error. If it is, the action taken should depend on the documentation of the error provided by the object. The final ElseIf checks to see if the error is simply a Visual Basic trappable runtime error (a system error), for example File not found (Error 53) that may have been passed back by the object. Note that although the range for these is 0–65535, the actual number of defined errors is currently fewer than 1000.

Since Number is a property, it behaves like a function call, and this may be significant if Err is passed to a procedure. If you have existing code in which you rely on a change in Err being copied back to the caller through the procedure’s parameter, it may fail even if Err is passed ByRef. However, since Err is a global object, there is no need to pass it as an argument at all. Make any desired changes directly to Err or Err.Number in the called procedure.

See Also

Description Property, Err Object, HelpContext Property (Visual Basic), HelpFile Property (Visual Basic), LastDLLError Property, Source Property.

Example

This example examines the Number property of the Err object to determine whether an error returned by an object was defined by the object, or whether it was mapped to an error defined by Visual Basic. Note that the constant vbObjectError is a very large negative number that an object adds to its own error code to indicate that the error is defined by the server. Therefore, subtracting it from Err.Number strips it out of the result. If the error is object-defined, the base number is left in MyError, which is displayed in a message box along with the original source of the error. If Err.Number represents a Visual Basic error, then the Visual Basic error number is displayed in the message box.


' First, strip off the constant added by the object to indicate one
' of its own errors.= Err.Number - vbObjectError
' If you subtract the vbObjectError constant, and the number is still 
' in the range 0-65,535, it is an object-defined error code.MyError > 0 and MyError < 65535 Then
    Msg = "The object you accessed assigned this number " & _
        "to the error: " & MyError & ". " & _
        "The originator of the error was: " & Err.Source & ". " & _
        "Press F1 to see the originator’s Help topic. "
'Otherwise it is a Visual Basic error number.
    Msg = "This error (#" & Err.Number & ") " & _
        "is a Visual Basic error number. Press Help button " & _
        "or F1 for the Visual Basic Help topic for this error. "If
    MsgBox Msg, , "Object Error", Err.HelpFile, Err.HelpContext