So I found a good function by Allen Browne that records any errors to a table. I copied everything exactly and put the following code into a global module I have in my database:
I created the table to the exact specs (field names, types) he listed and put this code at the bottom of all my subs:
I like this method because it logs the errors in a table and I can view them as the program gets beta tested.
I tried testing it by coding a string variable to null when a form opens thus should trip the LogError event for "invalid use of null" error but when I open the form, instead I get the error 438 (Object doesn't support this property or method). when I hit debug, it highlights this line:
Anyone help me with this. I can't understand why it's saying this. I tried to see if it's something in the actual function but it doesn't even run the function.
The website that describes this is:
http://allenbrowne.com/ser-23a.html
Thanks.
Code:
Function LogError(ByVal lngErrNumber As Long, ByVal strErrDescription As String, _
strCallingProc As String, Optional vParameters, Optional bShowUser As Boolean = True) As Boolean
On Error GoTo Err_LogError
' Purpose: Generic error handler.
' Logs errors to table "tLogError".
' Arguments: lngErrNumber - value of Err.Number
' strErrDescription - value of Err.Description
' strCallingProc - name of sub|function that generated the error.
' vParameters - optional string: List of parameters to record.
' bShowUser - optional boolean: If False, suppresses display.
' Author: Allen Browne, [EMAIL="allen@allenbrowne.com"]allen@allenbrowne.com[/EMAIL]
Dim strMsg As String ' String for display in MsgBox
Dim rst As DAO.Recordset ' The tLogError table
Select Case lngErrNumber
Case 0
Debug.Print strCallingProc & " called error 0."
Case 2501 ' Cancelled
'Do nothing.
Case 3314, 2101, 2115 ' Can't save.
If bShowUser Then
strMsg = "Record cannot be saved at this time." & vbCrLf & _
"Complete the entry, or press <Esc> to undo."
MsgBox strMsg, vbExclamation, strCallingProc
End If
Case Else
If bShowUser Then
strMsg = "Error " & lngErrNumber & ": " & strErrDescription
MsgBox strMsg, vbExclamation, strCallingProc
End If
Set rst = CurrentDb.OpenRecordset("tLogError", , dbAppendOnly)
rst.AddNew
rst![ErrNumber] = lngErrNumber
rst![ErrDescription] = Left$(strErrDescription, 255)
rst![ErrDate] = Now()
rst![CallingProc] = strCallingProc
rst![UserName] = CurrentUser()
rst![ShowUser] = bShowUser
If Not IsMissing(vParameters) Then
rst![Parameters] = Left(vParameters, 255)
End If
rst.Update
rst.Close
LogError = True
End Select
Exit_LogError:
Set rst = Nothing
Exit Function
Err_LogError:
strMsg = "An unexpected situation arose in your program." & vbCrLf & _
"Please write down the following details:" & vbCrLf & vbCrLf & _
"Calling Proc: " & strCallingProc & vbCrLf & _
"Error Number " & lngErrNumber & vbCrLf & strErrDescription & vbCrLf & vbCrLf & _
"Unable to record because Error " & Err.Number & vbCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function
Code:
ExitProc:
Exit Sub
errhandler:
Call LogError(Err.Number, Err.descripton, "Incidents - check_values")
Resume ExitProc
I tried testing it by coding a string variable to null when a form opens thus should trip the LogError event for "invalid use of null" error but when I open the form, instead I get the error 438 (Object doesn't support this property or method). when I hit debug, it highlights this line:
Code:
Call LogError(Err.Number, Err.descripton, "Incidents - Form_Open")
The website that describes this is:
http://allenbrowne.com/ser-23a.html
Thanks.