Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

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 Common Error Handler.

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:

Error Message Info.

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 Trial Run.

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

Exit Function

BugHandler Err, Err.Description, "DataProcess()", "Module4", CurrentDb.Name
Resume DataProcess_Exit
End Function

Common Error Handler Info and Log File.

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()"

Exit Function

MsgBox Err & " : " & Err.Description, , "BugHandler()"
Resume BugHandler_Exit
End Function

The Library Database.

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.

Technorati Tags: ,

No comments:

Post a Comment

Comments subject to moderation before publishing.


Subscribe in a reader
Your email address:

Delivered by FeedBurner


Popular Posts

Blog Archive

Powered by Blogger.

Follow by Email


Forms Functions How Tos MS-Access Security Reports Class Module msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet MS-Access Scurity MS-Access and Internet Array External Links Queries msaccess reports Downloads msaccess tips Accesstips Objects Collection Object Property Event Menus and Toolbars Controls MsaccessLinks Process Controls WithEvents Art Work VBA msaccess How Tos Combo Boxes Dictionary Object Graph Charts List Boxes Query msaccessQuery Calculation Command Buttons Form Report Command Button Data Emails and Alerts RaiseEvent Custom Functions Custom Wizards DOS Commands Data Type Object Reference ms-access functions msaccess functions msaccess graphs msaccess reporttricks Item Key msaccessprocess security advanced Access Security Add Auto-Number Field Type Fields Form Instances Macros Menus Recordset SubForm Top Values Variables msaccess email msaccess menus progressmeter Access2007 Copy Excel Expression Join Methods Microsoft Numbering System Records Security Split Table Time Difference Utility Workgroup Wrapper Classes database function ms-access msaccess wizards reference text tutorial vba code Access Emails and Alerts Access Fields Access How Tos Access Mail Merge Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Database Records Defining Pages Diagram Disk Dynamic Lookup Error Handler Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands hyperlinks iSeries Date iif msaccess msaccess alerts pdf files restore switch toolbar updating upload