Err.Number (property)

Syntax

Err.Number [= errornumber]

Description

Returns or sets the number of the error.

Comments

The Err.Number property is set automatically when an error occurs. This property can be used within an error trap to determine which error occurred.

You can set the Err.Number property to any Long value.

The Number property is the default property of the Err object. This allows you to use older style syntax such as those shown below:

Err = 6

If Err = 6 Then MsgBox "Overflow"

The Err function can only be used while within an error trap.

The internal value of the Err.Number property is reset to 0 with any of the following statements: Resume, Exit Sub, Exit Function. Thus, if you want to use this value outside an error handler, you must assign it to a variable.

Setting Err.Number to –1 has the side effect of resetting the error state. This allows you to perform error trapping within an error handler. The ability to reset the error handler while within an error trap is not standard Basic. Normally, the error handler is reset only with the Resume, Exit Sub, Exit Function, End Function, or End Sub statements.

Example

'This example forces error 10, with a subsequent transfer to

'the TestError label. TestError tests the error and, if not

'error 55, resets Err to 999 (user-defined error) and returns

'to the Main subroutine.

Sub Main()

  On Error Goto TestError

  Error 10

  MsgBox "The returned error is: '" & Err() & " - " & _

  Error$ & "'"

  Exit Sub

  TestError:

    If Err = 55 Then 'File already open.

      MsgBox "Cannot copy an open file. Close it and try again."

    Else

      MsgBox "Error '" & Err & "' has occurred!"

    Err = 999

  End If

  Resume Next

End Sub

See Also

Error Handling (topic)

More information

E