The information below applies only to the VB6 edition of the global error handler (SimplyVB6 Global Error Handler)
To demonstrate how to enable and disable the SimplyVB6 Global Error Handler, let's create a simple example application. We will add a global error handler to your application that simply logs all errors that occur to a text file.
Step 1. Add the VB6 COM Reference
Create a new VB6 EXE application and go to the Project >> References... menu option.
(Alternatively, you can go do this with the Add-Ins >> SimplyVB6 Global Error Handler >> Setup normal COM library DLL support option)
Put a tick next to the 'SimplyVB6 Global Error Handler Library' (as above).
Press OK.
Step 2. Add the initialization VB6 code
You need to activate the library by telling it where the procedure you want to run on error is located. We do this using the AddressOf VB6 feature.
Copy and paste the following code into your VBA module:
Public Sub EnableErrorHandler()
If ErrEx.EnableGlobalErrorHandler(AddressOf MyGlobalErrorHandler, True) = False Then
MsgBox "EnableErrorHandler() failed. Failed to activate the global error handler."
End If
End Sub
Public Sub DisableErrorHandler()
ErrEx.DisableGlobalErrorHandler
End Sub
The DisableErrorHandler routine is completely optional and not required here. Once your application is unloaded, the VB6 Global Error Handler is also unloaded automatically. In this example we won't be using it.
Step 3. Call our initialization procedure in your startup routine
In order for our error handler to be initialized when your application is loaded, you should call our new procedure from your startup routine (either in Sub Main() or from your initial startup form).
Step 4. Implement our global error handling routine
Now for the important part.
In the initialization code, we have set the global error handler name as "MyGlobalErrorHandler".
So we now need to implement the procedure. Create a new standard module - name it ModGlobalErrorHandler and then copy & paste this code:
Public Sub MyGlobalErrorHandler()
LogErrorToFile
End Sub
Public Sub LogErrorToFile()
Dim FileNum As Long
Dim LogLine As String
On Error Resume Next ' If this procedure fails, something fairly major has gone wrong.
FileNum = FreeFile
Open "C:\ErrorLog.txt" For Append Access Write Lock Write As FileNum
Print #FileNum, Now() & " - " & CStr(ErrEx.Number) & " - " & CStr(ErrEx.Description)
'We will seperate the call stack onto seperate lines in the log
With ErrEx.CallStack
Do
Print #FileNum, " --> " & .ProjectName & "." & _
.ModuleName & "." & _
.ProcedureName & ", " & _
"#" & .LineNumber & ", " & _
.LineCode & vbCrLf
Loop While .NextLevel
End With
Close FileNum
End Sub
This routine (above) is simply logging every error that occurs to a log file on your C drive. Nothing fancy for this example.
Now try it out! Create a form with a command button and raise an artificial error (division by zero in this example):
Private Sub Command1_Click()
MsgBox 1 / 0
End Sub
Now open your application ensuring that the initialization routine (EnableErrorHandler) is run. On clicking the test button you should now see the new default Vista dialog in all it's glory:
The Vista dialog is fully customizable (and you certainly don't have to use it if you'd prefer to set up your own form dialog instead).
Now just check the C:\ErrorLog.txt file was generated by opening the file in Notepad:
Tip: If you're new to using the SimplyVB6 Global Error Handler, I strongly recommend you set a breakpoint on the 'MsgBox 1 / 0' line of code and then step through the error (F8 key) to understand the program flow better.