I would like to make a form that filters a report by Supervisor and by
starting and ending date.
I have the supervisors in a combo box, and that works.
However, I do not know how to code to let the user enter a starting
and ending date and only show the records between those dates.
The code that I have so far does not work for the dates, but I'm
including it anyway so you show me what to change.
Thanks so much! :)
Here is my code (the supervisor combo box works, the start and ending
date text boxes do not):
Option Compare Database
Option Explicit
Private Sub cmdApplyFilter_Click()
Dim strSupervisor As String
Dim strFilter As String
Dim strStart As String
Dim strEnd As String
' Check that the report is open
If SysCmd(acSysCmdGetObjectState, acReport, "Salary data &
SS#-Current") <> acObjStateOpen Then
MsgBox "You must open the report first."
Exit Sub
End If
' Build criteria string for Supervisor field
If IsNull(Me.cboSupervisor.Value) Then
strSupervisor = "Like '*'"
Else
strSupervisor = "='" & Me.cboSupervisor.Value & "'"
End If
' Build criteria string for Starting date field
If IsNull(Me.Start.Value) Then
strStart = "Like '*'"
Else
strStart = "='" & Me.Start.Value & "'"
End If
' Build criteria string for Ending date field'
If IsNull(Me.End.Value) Then
strEnd = "Like '*'"
Else
strEnd = "='" & Me.End.Value & "'"
End If
' Combine criteria strings into a WHERE clause for the filter
strFilter = "[supervisor_f] " & strSupervisor & " AND [start_date]
" & strStart & " AND [end_date] " & strEnd
' strFilter = "[supervisor_f] " & strSupervisor
' Apply the filter and switch it on
With Reports![Salary data & SS#-Current]
.Filter = strFilter
.FilterOn = True
End With
End Sub
Private Sub cmdRemoveFilter_Click()
On Error Resume Next
' Switch the filter off
Reports![Salary data & SS#-Current].FilterOn = False
End Sub