Error Handler not logging errors

SShafeeq

Registered User.
Local time
Today, 18:51
Joined
Jan 4, 2011
Messages
32
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=&quot]1 Sub|Function SomeName()[/FONT]
[FONT=&quot]2 On Error GoTo Err_SomeName ' Initialize error handling.[/FONT]
[FONT=&quot]3 ' Code to do something here.[/FONT]
[FONT=&quot]4 Exit_SomeName: ' Label to resume after error.[/FONT]
[FONT=&quot]5 Exit Sub|Function ' Exit before error handler.[/FONT]
[FONT=&quot]6 Err_SomeName: ' Label to jump to on error.[/FONT]
[FONT=&quot]7 MsgBox Err.Number & Err.Description ' Place error handling here.[/FONT]
[FONT=&quot]8 Resume Exit_SomeName ' Pick up again and quit.[/FONT]
[FONT=&quot]9 End Sub|Function
[/FONT]
[FONT=&quot] [/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=&quot]Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _[/FONT]
[FONT=&quot] strCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean[/FONT]
[FONT=&quot]On Error GoTo Err_LogError[/FONT]
[FONT=&quot] ' Purpose: Generic error handler.[/FONT]
[FONT=&quot] ' Logs errors to table "tLogError".[/FONT]
[FONT=&quot] ' Arguments: lngErrNumber - value of Err.Number[/FONT]
[FONT=&quot] ' strErrDescription - value of Err.Description[/FONT]
[FONT=&quot] ' strCallingProc - name of sub|function that generated the error.[/FONT]
[FONT=&quot] ' vParameters - optional string: List of parameters to record.[/FONT]
[FONT=&quot] ' bShowUser - optional boolean: If False, suppresses display.[/FONT]
[FONT=&quot] ' Author: Allen Browne, allen@allenbrowne.com[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] Dim strMsg As String ' String for display in MsgBox[/FONT]
[FONT=&quot] Dim rst As DAO.Recordset ' The tLogError table[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot] Select Case lngErrNumber[/FONT]
[FONT=&quot] Case 0[/FONT]
[FONT=&quot] Debug.Print strCallingProc & " called error 0."[/FONT]
[FONT=&quot] Case 2501 ' Cancelled[/FONT]
[FONT=&quot] 'Do nothing.[/FONT]
[FONT=&quot] Case 3314, 2101, 2115 ' Can't save.[/FONT]
[FONT=&quot] If bShowUser Then[/FONT]
[FONT=&quot] strMsg = "Record cannot be saved at this time." & vbCrLf & _[/FONT]
[FONT=&quot] "Complete the entry, or press <Esc> to undo."[/FONT]
[FONT=&quot] MsgBox strMsg, vbExclamation, strCallingProc[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] Case Else[/FONT]
[FONT=&quot] If bShowUser Then[/FONT]
[FONT=&quot] strMsg = "Error " & lngErrNumber & ": " & strErrDescription[/FONT]
[FONT=&quot] MsgBox strMsg, vbExclamation, strCallingProc[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly)[/FONT]
[FONT=&quot] rst.AddNew[/FONT]
[FONT=&quot] rst![ErrNumber] = lngErrNumber[/FONT]
[FONT=&quot] rst![ErrDescription] = Left$(strErrDescription, 255)[/FONT]
[FONT=&quot] rst![ErrDate] = Now()[/FONT]
[FONT=&quot] rst![CallingProc] = strCallingProc[/FONT]
[FONT=&quot] rst![UserName] = CurrentUser()[/FONT]
[FONT=&quot] rst![ShowUser] = bShowUser[/FONT]
[FONT=&quot] If Not IsMissing(vParameters) Then[/FONT]
[FONT=&quot] rst![Parameters] = Left(vParameters, 255)[/FONT]
[FONT=&quot] End If[/FONT]
[FONT=&quot] rst.Update[/FONT]
[FONT=&quot] rst.Close[/FONT]
[FONT=&quot] LogError = True[/FONT]
[FONT=&quot] End Select[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]Exit_LogError:[/FONT]
[FONT=&quot] Set rst = Nothing[/FONT]
[FONT=&quot] Exit Function[/FONT]
[FONT=&quot] [/FONT]
[FONT=&quot]Err_LogError:[/FONT]
[FONT=&quot] strMsg = "An unexpected situation arose in your program." & vbCrLf & _[/FONT]
[FONT=&quot] "Please write down the following details:" & vbCrLf & vbCrLf & _[/FONT]
[FONT=&quot] "Calling Proc: " & strCallingProc & vbCrLf & _[/FONT]
[FONT=&quot] "Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf & vbCrLf & _[/FONT]
[FONT=&quot] "Unable to record because Error " & Err.Number & vbCrLf & Err.Description[/FONT]
[FONT=&quot] MsgBox strMsg, vbCritical, "LogError()"[/FONT]
[FONT=&quot] Resume Exit_LogError[/FONT]
[FONT=&quot]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?
 
What does you errorhandler for that sub looks like, You have to Call LogError function and pass along Err.Number, Err.Description and the name which Sub/Function that generated the error.

ex.

Code:
Function MyFunction()
On Error Goto ErrorHandel
...  some code
 
ExitPoint:
'CleanUp
Exit Function
 
ErrorHandel:
      MsgBox "Error " & Err.Number & " Description: " & Err.Description
      [B]Call LogError(Err.Number, Err.Description, "MyFunction()")[/B]
      Resume ExitPoint
End Function

JR
 
d'oh! I feel like such an idiot! Thanks for that JANR, just tested it and it works fine.

Another question, can I write a universal function for "on error call "universal_function"" rather then type a "on error goto..." before each sub?
 
Even if you can call an universal errorhandler, it is best to handle the error in the caller sub/function. Some anticipated errors you can handle locally and just log unanticipated errors.

You don't have to include en errorhandel in every sub in your project, but include it in mission critical subs.

JR
 

Users who are viewing this thread

Back
Top Bottom