Hi!
I'm almost finished coding my database and I wanted to include an error logger in it.
So I looked up Allen Browne's error handler, basically it says to include the following in every sub used:
[FONT="] [/FONT]
Which I did and it works fine. Then it asks to create a table which can log errors call tLogErrors and to have a global function:
Now I created a module called error handling and put the above function in it and call the module ErrorHandling.
Now i tested this in a form that was generating errors. The error message comes up fine but it doesnt log errors into the table. What am I doing wrong? Why are the errors not being logged?
I'm almost finished coding my database and I wanted to include an error logger in it.
So I looked up Allen Browne's error handler, basically it says to include the following in every sub used:
[/FONT][FONT="]1 Sub|Function SomeName()[/FONT]
[FONT="]2 On Error GoTo Err_SomeName ' Initialize error handling.[/FONT]
[FONT="]3 ' Code to do something here.[/FONT]
[FONT="]4 Exit_SomeName: ' Label to resume after error.[/FONT]
[FONT="]5 Exit Sub|Function ' Exit before error handler.[/FONT]
[FONT="]6 Err_SomeName: ' Label to jump to on error.[/FONT]
[FONT="]7 MsgBox Err.Number & Err.Description ' Place error handling here.[/FONT]
[FONT="]8 Resume Exit_SomeName ' Pick up again and quit.[/FONT]
[FONT="]9 End Sub|Function
[FONT="] [/FONT]
Which I did and it works fine. Then it asks to create a table which can log errors call tLogErrors and to have a global function:
[FONT="]Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _[/FONT]
[FONT="] strCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean[/FONT]
[FONT="]On Error GoTo Err_LogError[/FONT]
[FONT="] ' Purpose: Generic error handler.[/FONT]
[FONT="] ' Logs errors to table "tLogError".[/FONT]
[FONT="] ' Arguments: lngErrNumber - value of Err.Number[/FONT]
[FONT="] ' strErrDescription - value of Err.Description[/FONT]
[FONT="] ' strCallingProc - name of sub|function that generated the error.[/FONT]
[FONT="] ' vParameters - optional string: List of parameters to record.[/FONT]
[FONT="] ' bShowUser - optional boolean: If False, suppresses display.[/FONT]
[FONT="] ' Author: Allen Browne, allen@allenbrowne.com[/FONT]
[FONT="] [/FONT]
[FONT="] Dim strMsg As String ' String for display in MsgBox[/FONT]
[FONT="] Dim rst As DAO.Recordset ' The tLogError table[/FONT]
[FONT="] [/FONT]
[FONT="] Select Case lngErrNumber[/FONT]
[FONT="] Case 0[/FONT]
[FONT="] Debug.Print strCallingProc & " called error 0."[/FONT]
[FONT="] Case 2501 ' Cancelled[/FONT]
[FONT="] 'Do nothing.[/FONT]
[FONT="] Case 3314, 2101, 2115 ' Can't save.[/FONT]
[FONT="] If bShowUser Then[/FONT]
[FONT="] strMsg = "Record cannot be saved at this time." & vbCrLf & _[/FONT]
[FONT="] "Complete the entry, or press <Esc> to undo."[/FONT]
[FONT="] MsgBox strMsg, vbExclamation, strCallingProc[/FONT]
[FONT="] End If[/FONT]
[FONT="] Case Else[/FONT]
[FONT="] If bShowUser Then[/FONT]
[FONT="] strMsg = "Error " & lngErrNumber & ": " & strErrDescription[/FONT]
[FONT="] MsgBox strMsg, vbExclamation, strCallingProc[/FONT]
[FONT="] End If[/FONT]
[FONT="] Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly)[/FONT]
[FONT="] rst.AddNew[/FONT]
[FONT="] rst![ErrNumber] = lngErrNumber[/FONT]
[FONT="] rst![ErrDescription] = Left$(strErrDescription, 255)[/FONT]
[FONT="] rst![ErrDate] = Now()[/FONT]
[FONT="] rst![CallingProc] = strCallingProc[/FONT]
[FONT="] rst![UserName] = CurrentUser()[/FONT]
[FONT="] rst![ShowUser] = bShowUser[/FONT]
[FONT="] If Not IsMissing(vParameters) Then[/FONT]
[FONT="] rst![Parameters] = Left(vParameters, 255)[/FONT]
[FONT="] End If[/FONT]
[FONT="] rst.Update[/FONT]
[FONT="] rst.Close[/FONT]
[FONT="] LogError = True[/FONT]
[FONT="] End Select[/FONT]
[FONT="] [/FONT]
[FONT="]Exit_LogError:[/FONT]
[FONT="] Set rst = Nothing[/FONT]
[FONT="] Exit Function[/FONT]
[FONT="] [/FONT]
[FONT="]Err_LogError:[/FONT]
[FONT="] strMsg = "An unexpected situation arose in your program." & vbCrLf & _[/FONT]
[FONT="] "Please write down the following details:" & vbCrLf & vbCrLf & _[/FONT]
[FONT="] "Calling Proc: " & strCallingProc & vbCrLf & _[/FONT]
[FONT="] "Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf & vbCrLf & _[/FONT]
[FONT="] "Unable to record because Error " & Err.Number & vbCrLf & Err.Description[/FONT]
[FONT="] MsgBox strMsg, vbCritical, "LogError()"[/FONT]
[FONT="] Resume Exit_LogError[/FONT]
[FONT="]End Function[/FONT]
Now I created a module called error handling and put the above function in it and call the module ErrorHandling.
Now i tested this in a form that was generating errors. The error message comes up fine but it doesnt log errors into the table. What am I doing wrong? Why are the errors not being logged?