ErrorToString Function

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

Public Shared Function ErrorToString(ByVal ErrorNumber As Integer) As String


  • ErrorNumber
    Optional. Any valid error number.


Exception type

Error number




ErrorNumber is out of range.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.


The ErrorToString function examines the property settings of the Err object to identify the most recent run-time error. The return value of the ErrorToString function corresponds to the Description property of the Err object. If ErrorNumber is a valid error number but is not defined, ErrorToString returns the string "Application-defined or object-defined error." If ErrorNumber is not valid, an error occurs. If ErrorNumber is omitted, the message corresponding to the most recent run-time error is returned. If no run-time error has occurred or ErrorNumber is 0, ErrorToString returns a zero-length string ("").

In Visual Basic version 6.0 and earlier, this functionality was provided by the Error function.


The following code uses the ErrorToString function to display error messages that correspond to the specified error numbers.

Dim ErrorNumber As Integer 
For ErrorNumber = 61 To 64   ' Loop through values 61 - 64.
   MsgBox(ErrorToString(ErrorNumber))   ' Display error names in message box.
Next ErrorNumber


Namespace: Microsoft.VisualBasic


Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also


Structured Exception Handling Overview for Visual Basic

Unstructured Exception Handling Overview


Err Object (Visual Basic)

Description Property (Err Object)