Error, Error$ Function

See Also8BHAVAZ              Example2MKA4EC>Low

Returns the error message that corresponds to a given error code.

Syntax

Error[$][(errorcode)]

Remarks

Error returns a Variant8PHEAW3; Error$ returns a String.

The argument errorcode must be an integer between 1 and 32,767, inclusive.  If errorcode is omitted, the message string corresponding to the most recent run-timeCYRM35 error is returned.  If errorcode is not defined in Visual Basic, the returned message string is User-defined error.

Some error messages use internal variables to provide specific information about an error.  When an error occurs, words appropriate to the context of the error are substituted in the error message string.  Under normal circumstances, the following error message appears with a control array element index number inserted between the single quotation marks:

   Control array element '11' doesn't exist

 

However, when you use Error[$] to return an error message for a specified error code, there is no context for that error message and the appropriate text can't be inserted.  As a result, the message string returned by Error[$] contains no context-specific information between the single quotation marks.  A space is inserted in lieu of the actual context information.

If Error[$] is called with no argument, it returns the message string for the most recent run-time error.  If a run-time error has occurred, the returned message string includes substitutions appropriate to the context of the error.  If no run-time error has occurred, Error[$] returns a zero-length string ("").

The Err function returns the error code for the most recent run-time error.  Therefore, the following lines are equivalent except that in the first case, the text placed in A includes appropriate substitutions, and in the second case, no substitutions are made.

   A = Error

   A = Error(Err)


See Also

Err, Erl Functions3XXD805

Error Statement5AEF2Y

Trappable Errors4LGWYDD


Error, Error$ Functions Example

The example uses the Error function to display error messages associated with user-input error numbers.  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