HillTJ
To train a dog, first know more than the dog..
- Local time
- Today, 07:20
- Joined
- Apr 1, 2019
- Messages
- 731
All, I found, what I thought was a useful bit of code in the Access 2019 Bible "Deployment" section & thought it may be useful to others. What it does is write the error code etc to a text file. Previously, I'd always appended to an error log table within access, using an append to the error log table routine, then it is usual to write a form to display the error details. Using the below technique, the error log is a text file that can be read from outside access & could always be imported into excel if you wished to filter/sort records.
The first sub 'DisplayErrorMessage' is my 'normal' print error message routine triggered by the On-Error event of the code in a module. I've then included the sub 'LogError' which came from said textbook.
I'm sure both routines can be refined/improved, but to me an error log as a text file in the same folder as the database is pretty neat. Particularly, I could imagine in the case of a deployed application, then the user/administrator could locate the error log file for inspection.
The first sub 'DisplayErrorMessage' is my 'normal' print error message routine triggered by the On-Error event of the code in a module. I've then included the sub 'LogError' which came from said textbook.
I'm sure both routines can be refined/improved, but to me an error log as a text file in the same folder as the database is pretty neat. Particularly, I could imagine in the case of a deployed application, then the user/administrator could locate the error log file for inspection.
Code:
Option Compare Database
Option Explicit
Public Sub DisplayErrorMessage(ErrorNumber As Integer, CallingRoutine As String) ' my original code
MsgBox "MS Access has generated the following error" & vbCrLf & vbCrLf & _
"Error Number: " & ErrorNumber & vbCrLf & _
"Error Source: " & CallingRoutine & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occurred!"
Call LogError(CallingRoutine, ErrorNumber, Err.Description) ' the new bit
End Sub
Public Sub LogError(ProcName As String, ErrNum As Integer, ErrDescription As String) ' from Access 2019 Bible
Dim sFile As String
Dim lFile As Long
Dim aLogEntry(1 To 6) As String
Const sLogFile = "Error.Log"
Const sLogDelim = "|"
On Error Resume Next
sFile = CurrentProject.path & "\" & sLogFile
lFile = FreeFile
aLogEntry(1) = Format(Now, "yyyy-mm-dd hh:mm:ss")
aLogEntry(2) = ErrNum
aLogEntry(3) = ErrDescription
aLogEntry(4) = ProcName
aLogEntry(5) = Screen.ActiveForm.Name
aLogEntry(6) = Screen.ActiveControl.Name
Open sFile For Append As lFile
Print #lFile, Join(aLogEntry, sLogDelim)
Close lFile
End Sub
Last edited: