# Number
{: .no_toc }

Returns or sets a **Long** value specifying an error. **Number** is the default member of the **Err** object, so a bare reference to **Err** is equivalent to **Err.Number**. Read/write.

Syntax:
- **Err**.**Number**
- **Err**.**Number** **=** *errorNumber*

*errorNumber*
: A **Long** error code to assign to the **Err** object. When read, **Number** returns the current error code, or **0** if no error is active.

When returning a user-defined error from an object, set **Err.Number** by adding the chosen error code to the [**vbObjectError**](/tB/Modules/Constants/index.md#vbObjectError) constant. For example, the following code returns 1051 as an error code:

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

> [!NOTE]
> **Number** accepts a number above 65535. VB6 and 64-bit VBA raise error 5, *Invalid procedure call or argument*, for one.

> [!NOTE] BETA 1045
> **Number** keeps an **HRESULT** assigned to it as it is. VB6 and 64-bit VBA store the VB error of the same meaning for the ones that [**Raise**](/tB/Modules/ErrObject/Raise.md) turns into VB errors: after `Err.Number = &H80070057`, **Number** is 5 there, and in twinBASIC it is -2147024809.

## Common Errors

| Error | When |
|---|---|
| [94 (`&H5E`)](/Reference/Run-time-Errors.md#94), *Invalid use of Null* | *errorNumber* is **Null** |

## Example

The first example illustrates a typical use of the **Number** property in an error-handling routine.

```tb check_build
Sub Demo()
    On Error GoTo Handler

    Dim x As Double, y As Double
    x = 1 / y                ' Create division-by-zero error.
    Exit Sub
Handler:
    MsgBox Err.Number
    MsgBox Err.Description
    ' Check for division-by-zero error.
    If Err.Number = 11 Then
        y = y + 1
    End If
    Resume
End Sub
```

The second example examines the **Number** property of the **Err** object to determine whether an error returned by an Automation object was defined by the object, or whether it was mapped to a built-in error.

The constant **vbObjectError** is a very large negative number that an object adds to its own error code to indicate that the error is server-defined; subtracting it from **Err.Number** strips it back out. 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 built-in error, the built-in error number is displayed instead.

```tb check_build
Dim myError As Long, msg As String
' Strip off the constant added by the object to indicate one of its own errors.
myError = Err.Number - vbObjectError
' If you subtract vbObjectError and the number is still in the range 0-65535,
' it is an object-defined error code.
If 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."
Else
    msg = "This error (# " & Err.Number & ") is a built-in error number." _
        & " Press the Help button or F1 for the Help topic for this error."
End If
MsgBox msg, , "Object Error", Err.HelpFile, Err.HelpContext
```

## See Also

- [Description](/tB/Modules/ErrObject/Description.md) property
- [Source](/tB/Modules/ErrObject/Source.md) property
- [Raise](/tB/Modules/ErrObject/Raise.md) method
- [Clear](/tB/Modules/ErrObject/Clear.md) method
