This is the code I am using to filter records on a form
there are 3 combo boxes, 3 textboxes, two date boxes and one check box.
this code is used to filter and then display the results of the records that match all of the criteria.
however it doesnt work.
what happens is for example I enter a criteria and then it displays "NAME?" in all the form fields and then I close the form and the next time i open it the correct filtered fields are there. but it seems as if the query is retaining the filter information as I than have to go into the query and delete the parameters before the form will display all records again!
Please Please Please help me!! I have been working on this for weeks!
here is my code:
Code:
Option Compare Database
Dim whichCabinet As Integer
Const csfixedSQL = "SELECT tblLog.Name, tblLog.[SAP Number], tblLog.[Job Number], tblLog.Site, " & _
"tblLog.[# ID], tblLog.Date, tblLog.[Incident reference], tblLog.[Cause of trip], " & _
"tblLog.[Contact with ABS], tblLog.Comment, tblLog.[Start Time], tblLog.[Finish Time], " & _
"tblLog.[Additional comment], tblLog.Report FROM tblLog "
Private Sub Chk_log_comment_Click()
Txt_log_comment.Enabled = Chk_log_comment
End Sub
Private Sub Chk_log_date_Click()
'DT_log_date1.Locked = Not Chk_log_date
'DT_log_date2.Locked = Not Chk_log_date
End Sub
Private Sub Chk_log_id_Click()
Cmb_TLA.Enabled = Chk_log_id
End Sub
Private Sub Chk_log_incident_Click()
Txt_log_incident.Enabled = Chk_log_incident
End Sub
Private Sub Chk_log_job_Click()
Txt_log_job.Enabled = Chk_log_job
End Sub
Private Sub Chk_log_name_Click()
Cmb_name.Enabled = Chk_log_name
End Sub
Private Sub Chk_log_report_Click()
Txt_log_report.Enabled = Chk_log_report
End Sub
Private Sub Chk_log_sap_Click()
Txt_log_sap.Enabled = Chk_log_sap
End Sub
Private Sub Chk_log_cause_Click()
Txt_log_cause.Enabled = Chk_log_cause
End Sub
Private Sub Cmd_apply_Click()
Dim strSQL As String
Dim strWhere As String
Dim qdf As QueryDef
strSQL = csfixedSQL
If ((Chk_log_comment) And Not (IsNull(Txt_log_comment.Value))) Then
strWhere = " AND tblLog.[Additional comment] Like """ & "*" & Txt_log_comment.Value & "*"""
End If
If ((Chk_log_cause) And Not (IsNull(Txt_log_cause.Value))) Then
strWhere = strWhere & " OR tblLog.[Cause of trip] Like """ & "*" & Txt_log_cause.Value & "*"""
End If
If ((Chk_log_name) And Not (IsNull(Cmb_name.Value))) Then
strWhere = " AND tblLog.Name = """ & Cmb_name.Value & """"
End If
If ((Chk_log_id) And Not (IsNull(Cmb_TLA.Value))) Then
strWhere = strWhere & " AND tblLog.[# ID] = """ & Cmb_TLA.Value & """"
End If
If ((Chk_log_sap) And Not (IsNull(Txt_log_sap.Value))) Then
strWhere = strWhere & " AND tblLog.[SAP Number] Like """ & "*" & Txt_log_sap.Value & "*"""
End If
If ((Chk_log_job) And Not (IsNull(Txt_log_job.Value))) Then
strWhere = strWhere & " AND tblLog.[Job Number] Like """ & "*" & Txt_log_job.Value & "*"""
End If
If ((Chk_log_incident) And Not (IsNull(Txt_log_incident.Value))) Then
strWhere = strWhere & " AND tblLog.[Incident reference] Like """ & "*" & Txt_log_incident.Value & "*"""
End If
If ((Chk_log_report) And Not (IsNull(Txt_log_report.Value))) Then
strWhere = strWhere & " AND tblLog.Report Like """ & "*" & Txt_log_report.Value & "*"""
End If
If (strWhere <> "") Then strWhere = "(" & Mid(strWhere, 6) & ")"
If (Chk_log_date) Then
If Not (IsNull(DT_log_date1.Value)) Then
strDate = " AND tblLog.Date >= " & _
"#" & Format$(DT_log_date1.Value, "mm/dd/yyyy") & "#"
End If
If Not (IsNull(DT_log_date2.Value)) Then
strDate = strDate & " AND tblLog.Date <= " & _
"#" & Format$(DT_log_date2.Value, "mm/dd/yyyy") & "#"
End If
End If
If (strWhere <> "") Then
strWhere = strWhere & strDate
Else
strWhere = Mid(strDate, 6)
End If
If (strWhere <> "") Then strSQL = strSQL & "WHERE " & strWhere
If Frm_sort.Value = 1 Then
strSQL = strSQL & " ORDER BY tblLog.[# ID] ASC, tblLog.Date DESC"
Else
strSQL = strSQL & " ORDER BY tblLog.Date DESC, tblLog.[# ID] ASC"
End If
'If (Chk_log_date) Then
' If Not (IsNull(DT_log_date1.Value)) Then
' strWhere = strWhere & " AND tblLog.Date >= " & _
' "#" & Format$(DT_log_date1.Value, "mm/dd/yyyy") & "#"
' End If
' If Not (IsNull(DT_log_date2.Value)) Then
' strWhere = strWhere & " AND tblLog.Date <= " & _
' "#" & Format$(DT_log_date2.Value, "mm/dd/yyyy") & "#"
' End If
'End If
'If (strWhere <> "") Then strSQL = strSQL & "WHERE " & Mid(strWhere, 6)
Set qdf = CurrentDb.QueryDefs("qryLog")
qdf.SQL = strSQL
qdf.Close
Forms!frmSiteLog!frmLog.Form.RecordSource = ""
Forms!frmSiteLog!frmLog.Form.RecordSource = "qryLog"
End Sub
Private Sub Cmd_clear_Click()
Chk_log_name.Value = 0
Cmb_name.Enabled = False
Chk_log_id.Value = 0
Cmb_TLA.Enabled = False
Chk_log_sap.Value = 0
Txt_log_sap.Enabled = False
Chk_log_job.Value = 0
Txt_log_job.Enabled = False
Chk_log_date.Value = 0
'DT_log_date1.Locked = False
'DT_log_date2.Locked = False
Chk_log_incident.Value = 0
Txt_log_incident.Enabled = False
Chk_log_comment.Value = 0
Txt_log_comment.Enabled = False
Chk_log_report.Value = 0
Txt_log_report.Enabled = False
Chk_log_cause.Value = 0
Txt_log_cause.Enabled = False
Cmd_apply_Click
End Sub
Private Sub Form_Open(Cancel As Integer)
Cmd_clear_Click
End Sub
Private Sub Cmd_save_Click()
temp = -2147483643
Cmd_save.ForeColor = temp
Cmb_name.BackColor = temp
With Forms!frmSiteLog!frmLog
!Cmb_name.BackColor = temp
!Cmb_ID.BackColor = temp
!Txt_sap.BackColor = temp
!Txt_job.BackColor = temp
!Txt_date.BackColor = temp
!Txt_start.BackColor = temp
!Txt_finish.BackColor = temp
!Txt_incident.BackColor = temp
!Txt_trip.BackColor = temp
!Txt_comment.BackColor = temp
!Txt_report.BackColor = temp
!Txt_trip.BackColor = temp
End With
Opt_change.Value = False
UpdateLocks
End Sub
Private Sub Form_Load()
Opt_change.Value = False
UpdateLocks
DT_log_date1 = Now
DT_log_date2 = Now
End Sub
Private Sub UpdateLocks()
frmLog.Locked = Not Opt_change.Value
End Sub
Private Sub Frm_sort_AfterUpdate()
Cmd_apply_Click
End Sub
Private Sub Opt_change_Click()
UpdateLocks
End Sub
Private Sub Cmd_close_Click()
On Error GoTo Err_Cmd_close_Click
DoCmd.Close
Exit_Cmd_close_Click:
Exit Sub
Err_Cmd_close_Click:
MsgBox Err.Description
Resume Exit_Cmd_close_Click
End Sub