Procedure Name | Type | Description |
(Declarations) | Declarations | Declarations and private variables for the CErrorHandlerVB6 class. |
AppTitle | Property | Get the application title (defaults to the value of your application's app.title property). |
CurrentOperation | Property | Get the current operation. |
Destination | Property | Get the destination. |
DisplayMsgOnError | Property | Get whether a message box is displayed when an error is handled. |
ErrorDescription | Property | Get the description of the error that caused the error handler to be triggered. |
ErrorLine | Property | Get the line number of the procedure that caused the error when the error handler was triggered. |
ErrorNumber | Property | Get the error code that caused the error when the error handler is triggered. |
IncludeExpandedInfo | Property | Get the value of IncludeExpandedInfo. |
ProcName | Property | Get the name of the procedure containing the error which caused the error handler to be triggered. |
TraceExecution | Property | Get the value of the TraceExecution property. If this property is true, a log file is maintained which tracks each procedure which is logged by the Push method. Even if no error occurs in the procedure, an entry is written. This information can be useful to trace the order in which procedures are called in your application. By default the information is stored in a file named "trace.log" in your application's directory. |
Class_Initialize | Initialize | Set initial values to defaults which may be overridden with property settings. |
Class_Terminate | Terminate | Close trace log if opened, and check to be sure that all Push calls are balanced by Pop calls. If you raise an assertion at this point, double-check your code to see that you are calling the error handler correctly. |
ClearLog | Method | Clear the error log (or table depending on the setting of the Destination property). If Destination is a string property indicating a DOS text file, this function simply deletes the file. If Destination is a Recordset object, this method deletes all rows from the recordset. |
HandleError | Method | Log errors and respond to run-time errors. This is the main routine which handles a run-time error. It is responsible for displaying an error message, writing log information to a file, and raising events back into the caller program. This method is called if a run-time error branches to a section of code which calls this method. This method should only be called AFTER an error has occurred. First the current state of the error object is saved (error message, line, description). The BeforeHandlerCalled event is raised in the program which instantiated the error handler. The Cancel argument to BeforeHandlerCalled is tested. If the user does not cancel the error handler, by setting Cancel to True, the error handler proceeds to save error state information to the log file. After the error information has been saved, the AfterHandlerCalled event is raised to give the caller a chance to provide a custom response to the error, and the "AppSpecificHandler" private subroutine is called. If your program is not receiving the BeforeHandlerCalled or AfterHandlerCalled events, you can use this procedure to provide custom error handling, for example to display a custom form, or to provide the user a chance to exit the program cleanly. |
Pop | Method | Pops the current procedure name off the error handling stack. Normally this procedure is called when exiting a procedure normally when no error has occurred. It must be balanced by a call to the Push method when the procedure is first called. |
Push | Method | Pushes the supplied procedure name onto the error handling stack. You must manually assign the name of the current procedure to the strProc argument. It must be balanced by a call to the Pop method which is called when the procedure exits normally. |
AppSpecificErrHandler | Private | Custom error handler stub subroutine. The recommended way to use the CErrorHandlerVB6 class is to have it raise events into your program. You can receive the BeforeHandlerCalled and AfterHandlerCalled events. There you can provide specific functionality for your program, such as displaying a custom error message form, or closing the program cleanly. The advantage to using the events is that the CErrorHandlerVB6 object can be completely generic. All custom app-specific logic is contained in your program, not in the class itself. If you cannot use the events however, because you are declaring the error handler object variable in a standard module, which cannot receive events, you can customize this procedure to provide app-specific error-handling logic. All app-specific logic should be confined to this procedure. That way when you incorporate the CErrorHandlerVB6 class into your application, this is the only part of the code that will need to be modified. |
GetLastErr | Private | This method is called to get the last error that occurred. We increment the pointer as one of the last things done in Push() in order to add the next procedure to the next item available in the stack array. However, if an error occurred before we could add the next procedure to the stack, we need to go back to the previous item in the array to get the error that occurred. |
LogErrorToFile | Private | Logs the most recent error to a disk file. The name of the file is specified by setting the Destination property of the error handler object to a string containing the file name path. |
LogErrorToTable | Private | Logs the most recent error to a table. The table is specified by setting the Destination property of the error handler object to a recordset object which you create in your application. |
' Example of CErrorHandlerVB6 ' ' To try this example, do the following: ' 1. Create a new form ' 2. Add a command button named 'cmdTest' ' 3. Add a check box named 'chkResponseMode'. ' - When this box is unchecked the default error handling message will be displayed. ' - When it is checked, a custom error handling action will be taken ' 4. Paste all the code from this example to the new form's module. Private WithEvents mErrHandler As CErrorHandlerVB6 ' This example assumes that the sample files are located in the folder named by the following constant. Private Const mcstrSamplePath As String = "C:\TVSBSamp" Private Sub Form_Load() On Error GoTo PROC_ERR ' Create the error handler object and set some of its optional properties Set mErrHandler = New CErrorHandlerVB6 With mErrHandler .AppTitle = "My Application" .Destination = mcstrSamplePath & "\err.txt" .DisplayMsgOnError = True .IncludeExpandedInfo = True .CurrentOperation = "Initializing" .Push "Form_Load" End With PROC_EXIT: mErrHandler.Pop Exit Sub PROC_ERR: mErrHandler.HandleError GoTo PROC_EXIT End Sub ' Standard error handling style used for all non-trivial procs Private Sub cmdTest_Click() On Error GoTo PROC_ERR mErrHandler.Push "cmdTest_Click" Call Proc1 PROC_EXIT: mErrHandler.Pop Exit Sub PROC_ERR: mErrHandler.HandleError GoTo PROC_EXIT End Sub Private Sub Proc1() On Error GoTo PROC_ERR mErrHandler.Push "Proc1" Call Proc2 PROC_EXIT: mErrHandler.Pop Exit Sub PROC_ERR: mErrHandler.HandleError GoTo PROC_EXIT End Sub Private Sub Proc2() On Error GoTo PROC_ERR mErrHandler.Push "Proc2" ' Specify a checkpoint to narrow down the location of the error if you are not using line numbers mErrHandler.CurrentOperation = "I'm about to die" ' Simulate an error MsgBox (1 / 0) ' This code is bypassed by the error handler mErrHandler.CurrentOperation = "I'll never make it" PROC_EXIT: mErrHandler.Pop Exit Sub PROC_ERR: mErrHandler.HandleError GoTo PROC_EXIT End Sub Private Sub chkResponseMode_Click() On Error GoTo PROC_ERR mErrHandler.Push "chkResponseMode_Click" ' When unchecked, let the error handler display a standard error message. When checked, no message ' will be displayed, so display a custom message in the BeforeHandlerCalled event of the error object mErrHandler.DisplayMsgOnError = Not (chkResponseMode.Value = vbChecked) PROC_EXIT: mErrHandler.Pop Exit Sub PROC_ERR: mErrHandler.HandleError GoTo PROC_EXIT End Sub Private Sub mErrHandler_AfterHandlerCalled() ' This event is raised after the .HandleError method is called. If mErrHandler.DisplayMsgOnError = False Then MsgBox "Closing application" ' If you are writing the error to a text file this will open the file for viewing Shell "notepad.exe " & mErrHandler.Destination, vbNormalFocus Unload Me End If End Sub Private Sub mErrHandler_BeforeHandlerCalled(Cancel As Boolean) ' This event is raised when the .HandleError method is called on the errorhandler object. ' If the Cancel argument to this event is set to True, then the AfterHandlerCalled event will ' not be raised Dim strCustomMessage As String If mErrHandler.DisplayMsgOnError = False Then With mErrHandler strCustomMessage = "Custom Error Handler" & vbCrLf & _ "Something bad happened while doing: " & .CurrentOperation & vbCrLf & _ "The current procedure is: " & .ProcName & vbCrLf & _ "The error message is: " & .ErrorDescription & vbCrLf & _ "The error number is: " & .ErrorNumber & vbCrLf & _ "The error log will now be opened." MsgBox strCustomMessage, , .AppTitle If MsgBox("Close the application?", vbQuestion + vbYesNo, .AppTitle) = vbNo Then Cancel = True End If End With End If End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP