Err (statement)


Err = value


Sets the value returned by the Err function to a specific Integer value.


Only positive values less than or equal to 32767 can be used.

Setting value 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, or Exit Function statement.


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

  If Err = 55 Then    'File already open.
    MsgBox "Cannot copy an open file. Close it and try again."
    MsgBox "Error '" & Err & "' has occurred."
    Err = 999
  End If
  Resume Next
End Sub

See Also

Error (statement); Error Handling (topic).

More information