I have a module named modErrorHandler that has the below error handler. It save the error successfully to my table named tblLogErrors. But the the only problem is that on my On_click event to close the form still executes and then presents the error message.
I had another database that I practiced putting this code in, albeit on After Update event.
Here is the code for the Click Event
Here is the code for the Function stored in a module
Any help would be greatly appreciated. I'm not sure what I'm doing wrong.
I've tried playing around with On Error Resume Next. I guess I need something to cancel out the events in the code above?
I had another database that I practiced putting this code in, albeit on After Update event.
Here is the code for the Click Event
Code:
Private Sub cmdOpenCharts_Click()
On Error GoTo Err_Proc
'When there is an error jump to Proc_Err
DoCmd.Close
strFormName = "frmCharts"
DoCmd.OpenForm (strFormedName)
Exit_Proc:
Exit Sub
Err_Proc:
'Calls LogError function from modErrorHandler and passes the Error number,
'description and procedure name to function LogError.
Call LogError(Err.Number, Err.Description, "cmdOpenCharts_Click()")
Resume Exit_Proc
'To leave the program we must resume after this message by moving to Exit
End Sub
Here is the code for the Function stored in a module
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
Dim strMsg As String
Dim rst As DAO.Recordset
Select Case lngErrNumber
Case 0
Debug.Print strCallingProc & " called error 0."
Case 2501
Case 3314, 2101, 2115
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("tblLogError", , 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 & vrCrLf & Err.Description
MsgBox strMsg, vbCritical, "LogError()"
Resume Exit_LogError
End Function
Code:
Public strFormName As String
Any help would be greatly appreciated. I'm not sure what I'm doing wrong.
I've tried playing around with On Error Resume Next. I guess I need something to cancel out the events in the code above?