I use this code in my form to check for errors, fix if it can and log if it cannot. It works sometimes and other times it crashes.
I am on Access 2007 and cannot find why it crashes. But here is a screenshot of the error:
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
If DLookup("[Logging]", "userTable", "[UserLogin] = '" & strUserLogin & "'") = True Then
On Error GoTo err_handler
If DLookup("[Advanced Logging]", "userTable", "[UserLogin] = '" & strUserLogin & "'") = True Then
LogActions "subfrmAppts-Form_Error"
End If
End If
Response = acDataErrContinue
If DataErr = 2113 Or DataErr = 2279 And Screen.ActiveControl.Name = "tbSchTime" Then
SystemError "You made a mistake when typing in the Scheduled Time. It needs to be in the hh:mm tt format. " & _
"Also, make sure that the hour does not exceed 12 and the minutes do not exceed 59."
'Me.tbSchTime = Null
ElseIf DataErr = 2113 Or DataErr = 2279 And Screen.ActiveControl.Name = "tbArrTime" Then
SystemError "You made a mistake when typing in the Arrival Time. It needs to be in the hh:mm tt format. " & _
"Also, make sure that the hour does not exceed 12 and the minutes do not exceed 59."
'Me.tbArrTime.Value = Null
ElseIf DataErr = 2113 Or DataErr = 2279 And Screen.ActiveControl.Name = "tbDepTime" Then
SystemError "You made a mistake when typing in the Departure Time. It needs to be in the hh:mm tt format. " & _
"Also, make sure that the hour does not exceed 12 and the minutes do not exceed 59."
'Me.tbDepTime.Value = Null
Else
FormError "subfrmAppts-Form_Error", DataErr
End If
Exit Sub
err_handler:
FormError "subfrmAppts-Form_Error", DataErr
End Sub
Code:
Sub SystemError(strMessage As String)
DoCmd.OpenForm "frmGeneralError"
Forms!frmGeneralError.Form.lblError.Caption = strMessage
End Sub
Code:
Sub FormError(strSub As String, lngErrCode As Integer, Optional strControl As String)
If strControl = "" Then
LogError strSub, lngErrCode, "Not Logged"
Else
LogError strSub, lngErrCode, strControl
End If
DoCmd.OpenForm "frmGeneralError"
Forms!frmGeneralError.Form.lblError.Caption = strSub & " - " & lngErrCode
End
End Sub
Code:
Sub LogError(strSub As String, lngErrCode As Integer, strErrMessage As String, Optional strControl As String)
Dim cnn As ADODB.Connection
Dim strSQL As String
Set cnn = CurrentProject.Connection
strSQL = "INSERT INTO tblLog (ErrorNum, ErrMessage, UserName, ErrTime, BuildNum, CurrentSub, CurrentControl, Server) "
strSQL = strSQL & " VALUES ( " & lngErrCode & " "
strSQL = strSQL & " , '" & strErrMessage & "' "
strSQL = strSQL & " , '" & strUserLogin & "' "
strSQL = strSQL & " , #" & Format(Now, "MM/DD/YYYY HH:NN:SS") & "# " ' Make sure now is in MM/DD/YYYY HH:NN:SS format to prevent issues
strSQL = strSQL & " , '" & DLookup("[VersionNum]", "tblVersion", "[VersionID] = 1")
strSQL = strSQL & Format(DLookup("[VersionMinNum]", "tblVersion", "[VersionID] = 1"), ".00")
strSQL = strSQL & Format(DLookup("[BuildNo]", "tblVersion", "[VersionID] = 1"), ".00") & "' "
strSQL = strSQL & " , '" & strSub & "' "
strSQL = strSQL & " , '" & strControl & "' "
strSQL = strSQL & " ," & Chr(34) & GetServerName() & Chr(34) & ")"
'strSQL = strSQL & "VALUES ( " & lngErrCode & ", '" & strUserLogin _
& "', #" & Now & "#, '" & DLookup("[VersionNum]", "tblVersion", "[VersionID] = 1") & _
Format(DLookup("[VersionMinNum]", "tblVersion", "[VersionID] = 1"), ".00") & _
Format(DLookup("[BuildNo]", "tblVersion", "[VersionID] = 1"), ".00") & "', '" & strSub & "', '" & _
strControl & Chr(34) & GetServerName() & Chr(34) "' ) "
cnn.Execute strSQL, , adExecuteNoRecords
End Sub
I am on Access 2007 and cannot find why it crashes. But here is a screenshot of the error: