VB6Parse / Library / Environment / error_dollar

VB6 Library Reference

VB6 Error$ Function

The Error$ function returns the error message string corresponding to a given error number.

Syntax

Error$([errornumber])

Parameters

Returns

Returns a String containing the error message associated with the error number.

Remarks

Typical Uses

  1. Display error messages in message boxes
  2. Log error messages to files or debug output
  3. Format custom error messages
  4. Retrieve predefined system error messages
  5. Build error reporting systems
  6. Test error handling code
  7. Document expected errors
  8. 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

Performance Notes

Best Practices

  1. Use with error handlers for user-friendly messages.
  2. Combine with Err.Number for complete error info.
  3. Log Error$() output for debugging.
  4. Don't rely on exact message text (use error numbers).
  5. Provide context with error messages.
  6. Use for displaying errors to users.
  7. Document which errors your code may encounter.
  8. Test error paths with Error$() logging.
  9. Prefer Error$() over Error for String variables.
  10. 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

Limitations

← Back to Environment | View all functions