See Also Example
Simulates the occurrence of an error; can be used to generate a user-defined error.
Syntax
Error errorcode
Remarks
The argument errorcode must be an integer between 1 and 32,767, inclusive. If errorcode is an error code defined by Visual Basic, the Error statement simulates the occurrence of that error; that is, it sets the value of Err to errorcode.
To define your own error code, use a value that is greater than any used by the standard Visual Basic error codes. You can create application-specific user-defined errors working down from error code 32,767.
If an Error statement is executed when no error-handling routine is enabled, Visual Basic displays an error message and stops program execution. If the Error statement specifies an error code that is not used by Visual Basic, the message User-defined error is displayed.
Err, Erl Functions
On Error Statement
Resume Statement
Trappable Errors
The example uses the Error statement to simulate the occurrence of an error so that the error handler is invoked and the associated error message is displayed. To try this example, paste the code into the Declarations section of a form. Then press F5 and click the form.
Sub Form_Click ()
Dim Msg, NL, UserError ' Declare variables.
On Error GoTo ErrorHandler ' Set up error handler.
NL = Chr(10) ' Define newline.
Msg = "Please enter an error number to see the associated error"
Msg = Msg & " message."
UserError = InputBox(Msg)
Error UserError ' Simulate error occurrence.
Exit Sub
ErrorHandler: ' Error handler line label.
Msg = "The error message for error number "
Msg = Msg & Err & " is:" & NL & NL
Msg = Msg & """" & Error(Err) & """"
MsgBox Msg ' Display message.
Resume Next
End Sub