Visit our new web pages

We have launched a new extended set of web pages at www.alvechurchdata.co.uk. These old pages will stay on the server because people still have links to them but they will not be updated.

The new version of this page is at www.alvechurchdata.co.uk/hints-and-tips/accerrorlog.html.

An Access Error Log


Maintenance and debugging is very much easier if you have a reliable record of what went wrong. Once your database has been released into everyday use you will find yourself relying on the user's memory for reports of errors and details of error messages. Write yourself an error log so that you have an independent record of what has happened.

Error Log

A typical entry in our log file looks like this:

[23/09/2005],[15:55:49],[Backup failed],[53],[File not found]

We chose to use a simple text file so that we could read it from a user's PC without needing anything more sophisticated than NotePad. The regular format of fields delimited by [ & ] and separated by commas allow us to read and analyse the data automatically when we want to see whether there is any pattern in a large volume of data.

The log holds five fields:

  • Date
  • Time
  • Some descriptive text
  • The Access error number
  • The Access error message

The third parameter is particularly useful because it lets us give the user something friendlier than the typical Access error message. The error handler logs all five parameters but only displays this friendly message to the user.

Generating a log

Every routine in the application has an On Error call to the ErrorHandler sub.

Public Sub ErrorHandler(strReason As String)

Dim lngNumber As Long        ' Error number
Dim strDescription As String ' Error description

'-- Save these before On Error clears them
lngNumber = Err.Number
strDescription = Err.Description

On Error GoTo Err_ErrorHandler

'-- gstrHome is the name of a common network drive
Open gstrHome & "\Error.Log" For Append Lock Write As #1
'-- Write some basic info then close the file
Print #1, "[" & Date & "],[" & Time & "],[" & _
  strReason & "],[" & lngNumber & "],[" & _
  strDescription & "]"
Close #1

MsgBox strReason, vbCritical
Application.Quit acExit

End Sub

A typical call would be:

ErrorHandler "Backup Failed"

in the On Error section of the backup routine. The user does not need to know exactly where or why the backup has failed. All that the user needs to know is that the backup has failed and that they should call the support line. The rest of the detail is in the log file.

Extending the log

This is a simplified version of the code we use. The full log stores the id of the user and pc and an error grade is passed in as an extra parameter.

The lowest grade is 'Silent' for an event which is not really an error but which needs to be logged without any message being displayed to the user. Events such as 'Logon', 'Logoff' and 'Rebuild database' can be significant when debugging or when trying to tune the performance of the database.

The highest grade is 'Fatal' for an event where there can be no recovery and where the only possible response is an immediate shutdown.


Hints & tips

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map