I borrowed some code to try to create a dynamic filter that can search through a table named Projects. The searchable criteria is Project Name, Status and also a start and end date. I would like to be able to search through the records based on a range of start or end dates. Here's the code:
Private Sub Command24_Click()
Const cInvalidDateError As String = "You have entered an invalid date."
Dim strWhere As String
Dim strError As String
strWhere = "1=1"
' If Project Name
If Not IsNull(Me.ProjectName) Then
'Create Predicate
strWhere = strWhere & " AND " & "[Project].[Project Name] = " & Me.ProjectName
End If
' If Status
If Not IsNull(Me.Status) Then
'Add the predicate
strWhere = strWhere & " AND " & "[Project].[Status] = " & Me.Status & ""
End If
' If Start Date From
If IsDate(Me.StartDateFrom) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Projects.[Start Date] >= " & GetDateFilter(Me.StartDateFrom)
ElseIf Nz(Me.StartDateFrom) <> "" Then
strError = cInvalidDateError
End If
' If Start Date To
If IsDate(Me.StartDateTo) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Projects.[Start Date] <= " & GetDateFilter(Me.StartDateTo)
ElseIf Nz(Me.StartDateTo) <> "" Then
strError = cInvalidDateError
End If
' If End Date From
If IsDate(Me.EndDateFrom) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Projects.[End Date] >= " & GetDateFilter(Me.EndDateFrom)
ElseIf Nz(Me.EndDateFrom) <> "" Then
strError = cInvalidDateError
End If
' If End Date To
If IsDate(Me.EndDateTo) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Projects.[End Date] <= " & GetDateFilter(Me.EndDateTo)
ElseIf Nz(Me.EndDateTo) <> "" Then
strError = cInvalidDateError
End If
If strError <> "" Then
MsgBox strError
Else
'DoCmd.OpenForm "Browse Issues", acFormDS, , strWhere, acFormEdit, acWindowNormal
If Not Me.FormFooter.Visible Then
Me.FormFooter.Visible = True
DoCmd.MoveSize Height:=Me.WindowHeight + Me.FormFooter.Height
End If
Me.Search_Projects_Subform.Form.Filter = strWhere
Me.Search_Projects_Subform.Form.FilterOn = True
End If
End Sub
Function GetDateFilter(dtDate As Date) As String
' Date filters must be in MM/DD/YYYY format
GetDateFilter = "#" & Format(dtDate, "MM/DD/YYYY hh:mm:ss AM/PM") & "#"
End Function
Right now I am getting an error on the
Me.Search_Projects_Subform.Form.Filter = strWhere
line. I have checked and the Subform name is not misspelled. Any Suggestions? I could definitely use some help!