Centralized Error Handler and Error Log
In an earlier article with the VBA Utility program we have seen how to scan through a VBA Module (both Standard Module and Form/Report Modules – Class Modules) and insert Error Handler lines automatically, wherever they are found missing. Find the link to that Article here.
I am sure that Utility Program will help you to save time, otherwise you spent on typing/copy pasting and modifying hundreds of lines of error-trap code in your sub-routines or functions. The whole idea behind the error handler is to take care of unexpected errors and if necessary report it to the developer so that the logical error can be rectified once and for all. Besides that the program should not break the code unexpectedly. If it is a minor issue the user can ignore and continue to do what he/she is doing.
A particular Project will have hundreds of Sub-Routines/Functions in Standard Modules and Form/Report Modules. When the error message pops up the message will carry the Error Number and Error Description. The message box’s title will carry the Function/Sub-Routine name, if this is included in the title parameter of the MsgBox() Function. But, the user may not notice this valuable information to pass on to the programmer so that he can go directly into that program and do what he needs to do to rectify the error.
A better approach to these kind of issues is to create a common Error Handler Program and maintain an Error Log Text File on disk. When an error occurs in a Function/Sub-Routine call the common Function with the necessary parameters (Error Number, Error Description, Function/Sub-Routine Name, Module Name, Database Name). The common error handler program will not only display the error message but also write out the details into a Text File on Disk.
If you have several Microsoft Access based Applications installed on Local Area Network all of them can save the error log information into a single text file on Server’s common location. The error log will carry the Date, Time, Module Name and Database name, besides the normal error values Error Number and Error Description.
A Text file image with sample error log entries is given below:
The error log entry have all the details (Date & Time of Error, Database Path, Module Name, Procedure name etc. to pin-point the location of the Error. Even if the user doesn’t bother to report the problem to the Administrator, the Administrator can periodically check this log file to monitor his application’s health.
The following sample data processing program DataProcess() attempts to open the input table Table_1, but the table doesn’t exist (got deleted or renamed by mistake) and the program runs into error:
Public Function DataProcess() Dim db As Database, rst As Recordset, x On Error GoTo DataProcess_Error Set db = CurrentDb Set rst = db.OpenRecordset("Table_1", dbOpenDynaset) Do While Not rst.EOF x = rst.Fields(0).Value Loop rst.Close DataProcess_Exit: Exit Function DataProcess_Error: BugHandler Err, Err.Description, "DataProcess()", "Module4", CurrentDb.Name Resume DataProcess_Exit End Function
When the above program runs into error it calls the BugHandler() Program and passes the Module Name and Database Path as last two parameters in addition to Error Number, Error Description and Program name. The VBA Code of BugHandler() main program is given below:
Public Function BugHandler(ByVal erNo As Long, _ ByVal erDesc As String, _ ByVal procName As String, _ ByVal moduleName As String, _ ByVal dbName As String) On Error GoTo BugHandler_Error Dim logFile As String Dim msg As String 'Error Log text file pathname, change it to the correct path 'on your Local Drive or Server Location logFile = "c:\mdbs\bugtrack\acclog.txt" 'Open log file to add the new error log entry Open logFile For Append As #1 'write the log details to log file Print #1, Now() & vbCr Print #1, "Database : " & dbName & vbCr Print #1, "Module : " & moduleName & vbCr Print #1, "Procedure: " & procName & vbCr Print #1, "Error No.: " & erNo & vbCr Print #1, "Desc. : " & erDesc & vbCr Print #1, String(80, "=") & vbCr Close #1 msg = "Procedure Name: " & procName & vbCr & "Error : " & erNo & " : " & erDesc MsgBox msg, , "BugHandler()" BugHandler_Exit: Exit Function BugHandler_Error: MsgBox Err & " : " & Err.Description, , "BugHandler()" Resume BugHandler_Exit End Function
You can save the above Code into a common Library Database, where you have saved your own common library functions, so that it can be attached to your Projects.
This method will write out the details of errors from your databases into a common place accessible to you all the time. When an Error is reported by the User you can directly check the details of it without asking the user to spell out.