Library tutorials & articles

Handling Errors in VB/VBA/VBS/ASP

Using the Err Object

To force an error to appear, in order to find the context, description and related help files for the topic at hand, you can use the following function. Call the public function, after placing it in a module, by the following code;

'// Form Code
'Create a form and ad a command button named Command1

Private Sub Command1_Click()
    Call GetErrData(TheNumberHere)
End Sub


'// Module Code

Public Function GetErrData(ByRef ErrorNumber As Long)
On Error GoTo ErrHandler

Err.Raise (ErrorNumber) ' this brief call to the event handler Raises the error

' Note that the Err.Source would be of little help here since
' this would be the calling project that you are in.

Exit Function
ErrHandler:
MsgBox "This is the error info..." & vbCrLf & _
"Number: " & Err.Number & vbCrLf & _
"Description " & Err.Description & vbCrLf & _
"Help Context ID" & Err.HelpContext & vbCrLf & _
"Help File" & Err.HelpFile, vbCritical, "ERROR MESSAGE"
Err.Clear   ' destroy the error and…
Resume Next
End Function


The following function will print out all available errors to a text file called, AllErrors.txt in the same location as your application file is stored.

'// Form Code
'Create a form and ad a command button named Command1

Private Sub Command1_Click()
    Call GetAllErrors()
    MsgBox "The Error document has been created", vbExclamation, "Finished!"
End Sub


'// Module Code

Public Function GetAllErrors()
'clean out residual error tags
Err.Clear
On Error GoTo ErrHandler
Dim I As Long, F As Integer, ErrFile As String
Dim MessageTxt As String

ErrFile = App.Path & "\AllErrors.txt"

F = FreeFile
Open ErrFile For Output As #F
Print #F, "Err.Number|Err.Description|Err.HelpContext|Err.HelpFile"

    For I = 1 To 65535 'all possible valid Long err numbers 'Loop the numbers and call them one by one Err.Raise (I)
GoHere:
' On resuming after getting the error message, go here
   
'only print the messages clearly defined
If MessageTxt <> "" And _
InStr(1, Trim(MessageTxt), "Application-defined or", vbTextCompare) = 0 Then
    Print #F, MessageTxt
End If

    Next I
Close #F

Exit Function
ErrHandler:
MessageTxt = Err.Number & "|" & _
 Err.Description & "|" & _
 Err.HelpContext & "|" & _
 Err.HelpFile
Err.Clear
Resume GoHere   ' Resume execution at the GoHere line label
End Function



For further details on the error object, please refer to the following topics in the Visual Basic help: Err Object, Trappable Errors, Error Function

Comments

  1. 26 Apr 2005 at 11:36

    Is there any way to handle errors in INFINITE LOOP.


    for example,


    While Not blnIdeaFound
                   blnIdeaFound = ProcessDetector(EXE_NAME, False)
    Wend


    IF THE CONTROL IS INSIDE THE LOOP ( INFINITE LOOP ), IS IT POSSIBLE TO HANDLE THIS CASE WITH A ERROR HANDLER ?

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Handling Errors in VB/VBA/VBS/ASP.

Leave a comment

Sign in or Join us (it's free).

Mike J

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!