Code:
Const cInvalidDateError As String = "You have entered an invalid date."
Dim strWhere As String
Dim strError As String
strWhere = "1=1"
' If Assigned To
If Not IsNull(Me.AssignedTo) Then
'Create Predicate
strWhere = strWhere & " AND " & "Table1.[Assigned To] = " & Me.AssignedTo & ""
End If
' If Opened By
If Not IsNull(Me.OpenedBy) Then
'Add the predicate
strWhere = strWhere & " AND " & "Table1.[Opened By] = " & Me.OpenedBy & ""
End If
' If Status
If Nz(Me.Status) <> "" Then
'Add it to the predicate - exact match
strWhere = strWhere & " AND " & "Table1.Status = '" & Me.Status & "'"
End If
' If Priority
If Nz(Me.Priority) <> "" Then
'Add it to the predicate - exact match
strWhere = strWhere & " AND " & "Table1.Priority = '" & Me.Priority & "'"
End If
' If Opened Date From
If IsDate(Me.OpenedDateFrom) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Table1.[Submitted] >= " & GetDateFilter(Me.OpenedDateFrom)
ElseIf Nz(Me.OpenedDateFrom) <> "" Then
strError = cInvalidDateError
End If
' If Opened Date To
If IsDate(Me.OpenedDateTo) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Table1.[Submitted] <= " & GetDateFilter(Me.OpenedDateTo)
ElseIf Nz(Me.OpenedDateTo) <> "" Then
strError = cInvalidDateError
End If
' If Due Date From
If IsDate(Me.ResolvedDateFrom) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Table1.[Resolved] >= " & GetDateFilter(Me.ResolvedDateFrom)
ElseIf Nz(Me.ResolvedDateFrom) <> "" Then
strError = cInvalidDateError
End If
' If Due Date To
If IsDate(Me.ResolvedDateTo) Then
' Add it to the predicate - exact
strWhere = strWhere & " AND " & "Table1.[Resolved] <= " & GetDateFilter(Me.ResolvedDateTo)
ElseIf Nz(Me.ResolvedDateTo) <> "" Then
strError = cInvalidDateError
End If
' If Title
If Nz(Me.Title) <> "" Then
' Add it to the predicate - match on leading characters
strWhere = strWhere & " AND " & "Table1.Title Like '*" & Me.Title & "*'"
End If
If strError <> "" Then
MsgBox strError
Else
DoCmd.OpenReport "Searched", , , strWhere, acFormEdit, acWindowNormal
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
But it keeps just jumping directly to printing the report instead of just generating it on screen to view?