VB6 Error$ Function
The Error$ function returns the error message string corresponding to a given error number.
Syntax
Error$([errornumber])
Parameters
errornumber: Optional. A numeric expression representing an error number. If omitted, returns the error message for the most recent error (Err.Number).
Returns
Returns a String containing the error message associated with the error number.
Remarks
Error$returns aString, whileError(without the $) returns aVariant.- If
errornumberis omitted, returns the message for the current error (Err.Number). - If the error number is not recognized, returns "Application-defined or object-defined error".
- System errors (1-1000) return predefined messages.
- User-defined errors typically start at vbObjectError.
- Does not raise or clear errors, only retrieves messages.
- Can be used to display error messages to users.
- Related to the
Errobject andErrorstatement. - Returns an empty string if error number is 0.
Typical Uses
- Display error messages in message boxes
- Log error messages to files or debug output
- Format custom error messages
- Retrieve predefined system error messages
- Build error reporting systems
- Test error handling code
- Document expected errors
- Create user-friendly error dialogs
Basic Examples
Example 1: Get current error message
On Error Resume Next
x = 1 / 0
MsgBox Error$()
Example 2: Get specific error message
MsgBox Error$(11) ' "Division by zero"
Example 3: Display error in handler
On Error GoTo ErrHandler
' code
Exit Sub
ErrHandler:
MsgBox "Error: " & Error$()
Example 4: Log error message
Debug.Print "Error occurred: " & Error$()
Common Patterns
Pattern 1: Display formatted error
MsgBox "An error occurred: " & Error$()
Pattern 2: Log error with number
Debug.Print "Error " & Err.Number & ": " & Error$()
Pattern 3: Custom error message
If Err.Number <> 0 Then
msg = "Operation failed: " & Error$()
End If
Pattern 4: Get specific error text
errMsg = Error$(53) ' "File not found"
Pattern 5: Error handler logging
On Error GoTo ErrHandler
' code
Exit Sub
ErrHandler:
Open "errors.log" For Append As #1
Print #1, Now & ": " & Error$()
Close #1
Pattern 6: Compare error messages
If Error$() = Error$(11) Then
' Handle division by zero
End If
Pattern 7: Build error report
report = "Error Number: " & Err.Number & vbCrLf
report = report & "Description: " & Error$() & vbCrLf
Pattern 8: Test error messages
For i = 1 To 100
Debug.Print i & ": " & Error$(i)
Next i
Pattern 9: User-friendly error dialog
MsgBox "Sorry, an error occurred:" & vbCrLf & Error$(), vbExclamation
Pattern 10: Conditional error handling
If InStr(Error$(), "File") > 0 Then
' Handle file-related errors
End If
Advanced Usage
Example 1: Comprehensive error logger
Sub LogError()
Dim msg As String
msg = "Error " & Err.Number & " at " & Now & vbCrLf
msg = msg & "Description: " & Error$() & vbCrLf
msg = msg & "Source: " & Err.Source & vbCrLf
Debug.Print msg
End Sub
Example 2: Error message translator
Function GetFriendlyError() As String
Select Case Err.Number
Case 11
GetFriendlyError = "Cannot divide by zero"
Case 53
GetFriendlyError = "The file was not found"
Case Else
GetFriendlyError = Error$()
End Select
End Function
Example 3: Error documentation generator
Sub DocumentErrors()
Dim i As Integer
Open "errors.txt" For Output As #1
For i = 1 To 1000
If Error$(i) <> "" Then
Print #1, i & vbTab & Error$(i)
End If
Next i
Close #1
End Sub
Example 4: Error testing utility
Function TestError(errNum As Integer) As String
On Error Resume Next
Err.Raise errNum
TestError = Error$()
Err.Clear
End Function
Error Handling
- Returns empty string for error number 0.
- Returns "Application-defined or object-defined error" for unrecognized errors.
- Does not raise or clear errors itself.
- Safe to call at any time.
Performance Notes
- Fast, constant time O(1) lookup.
- No side effects on error state.
- Safe for repeated calls.
Best Practices
- Use with error handlers for user-friendly messages.
- Combine with
Err.Numberfor complete error info. - Log
Error$()output for debugging. - Don't rely on exact message text (use error numbers).
- Provide context with error messages.
- Use for displaying errors to users.
- Document which errors your code may encounter.
- Test error paths with
Error$()logging. - Prefer
Error$()overErrorforStringvariables. - Clear errors after handling with
Err.Clear.
Comparison Table
| Function/Statement | Purpose | Returns |
|---|---|---|
Error$ |
Get error message string | String |
Error |
Get error message variant | Variant |
Err.Description |
Current error description | String |
Err.Number |
Current error number | Long |
Platform Notes
- Available in VB6 and VBA.
- Error messages are in English by default.
- Some error messages may be locale-specific.
VBScriptusesErr.Descriptioninstead.
Limitations
- Returns English messages (may not be localized).
- Cannot customize built-in error messages.
- Limited to VB6's predefined error numbers.
- Does not provide error source or context.
- Message text may change between VB versions.