QuickStart: Enabling and disabling (VBA)



 Sept 2010: v2 of VBA Global Error Handler is now available vbWatchdog (no DLLs!)

The basics

The information below applies only to the VBA edition of the global error handler (SimplyVBA Global Error Handler)

To demonstrate how to enable and disable the SimplyVBA 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 VBA COM Reference

With your Access database application open, launch the VBA IDE by pressing Ctrl+G.    Go to the Tools >> References...  menu option.

(Alternatively, you can go do this with the Add-Ins >> SimplyVBA Global Error Handler >> Setup normal COM library DLL support option)

VBA REFERENCES PICTURE

Put a tick next to the 'SimplyVBA Global Error Handler Library' (as above).
Press OK.

Step 2. Add the initialization VBA code

You need to activate the library by telling it what the name of the function is that you want to call when an error occurs.

Copy and paste the following code into your VBA module:
Public Function EnableErrorHandler()
    
    If ErrEx.EnableGlobalErrorHandler(Access.Application, "MyGlobalErrorHandler") = False Then
 
    	MsgBox "EnableErrorHandler() failed.  Failed to activate the global error handler."
    
    End If
    
End Function

Public Function DisableErrorHandler()
    
    ErrEx.DisableGlobalErrorHandler
    
End Function
Info:  You will notice that we have declared these two routines as Functions without any return value. We declare them as functions for convenience so that we can call the routines from an Access macro (Step 3), which isn't possible if we declare them as Subs.

The DisableErrorHandler routine is completely optional and not required here. Once your application is unloaded, the VBA Global Error Handler is also unloaded automatically.  In this example we won't be using it.

Step 3. Add an AutoExec macro to call the initialization routine on startup

In order for our error handler to be initialized when your application is loaded, you should create a new Access macro named AutoExec. You should add the macro line RunCode EnableErrorHandler(), like this:

AUTOEXEC MACRO PICTURE


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 that procedure. Create a new standard VBA module and name it ModGlobalErrorHandler. Now 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 separate the call stack onto separate 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

Public Sub MyTestSub()

    Debug.Print 1 / 0

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!

Close your application and re-open it (so that the AutoExec macro runs). Now we need to generate an error for test purposes, so in the VBE immediate window type:
MyTestSub
MyTestSub was one of the subroutines in the example code that I made you copy & paste over - it simply causes a division by zero exception. You should now see the new default Vista dialog in all it's glory:

Basic Vista Error Dialog Example picture


The Vista dialog is fully customizable (and you certainly don't have to use it if you'd prefer to set up your own Access form dialog instead). See the Sample.mdb for examples.

Now just check the C:\ErrorLog.txt file was generated by opening the file in Notepad:

Basic Example Error Log picture


Tip:  If you're new to using the SimplyVBA Global Error Handler, I strongly recommend you set a breakpoint on the 'Debug.Print 1 / 0' line of code and then step through the error (F8 key) to understand the program flow better.

Tips on implementing your global error handler routine

  1. Always use the ErrEx function inside of your global error handler routine instead of Err
  2. Errors that occur inside of your global error handler routine will not be passed on to the global error handler recursively.   Instead, use local error handling inside of your global error handling, if you so wish.
  3. The properties of ErrEx are unique to the error that is being handled, and not effected by any local errors that may occur inside of your global error handling routines (unlike Err)
  4. Set breakpoints and step through the errors and global error handling routines. It might sound obvious, but it's worth remembering.