Several months have passed. The code I figured out worked great, but now I need it to reference date fields on a SUBREPORT.
Can someone please help me?
I believe I referenced the subreport correctly, but it continues to open the subreport for ALL records and ignoring the limitations of the dates. What am I doing wrong?
Here's the code I'm using:
Code:
Private Sub cmdrptAllItems_Summary_SelectDates_Click()
On Error GoTo Err_Handler
'Purpose: Filter a report to a date range.
'Documentation: http://allenbrowne.com/casu-08.html
'Note: Filter uses "less than the next day" in case the field has a time component.
Dim strReport As String
Dim strDateField As String
Dim strWhere As String
Dim lngView As Long
Const strcJetDate = "\#mm\/dd\/yyyy\#"
'DO set the values in the next 3 lines.
strReport = "rptAllItems_Summary_SelectDates"
strDateFieldStart = "Reports![rptAllItems_Summary_SelectDates]![rptAllItems_Summary_Done].Report![ItemStartDate]"
strDateFieldEnd = "Reports![rptAllItems_Summary_SelectDates]![rptAllItems_Summary_Done].Report![ItemEndDate]"
lngView = acViewPreview
strWhere = "(" & "(" & strDateFieldStart & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")" & " AND " & "(" & strDateFieldEnd & " >=" & Format(Me.txtStartDate, strcJetDate) & ")" & ")" & " OR " & "(" & strDateFieldEnd & "IS NULL" & ")"
If IsNull(Me.txtEndDate) Then
Me.txtEndDate = DateAdd("d", 7, Me.txtStartDate)
strWhere = "(" & "(" & strDateFieldStart & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")" & " AND " & "(" & strDateFieldEnd & " >= " & Format(Me.txtStartDate, strcJetDate) & ")" & ")" & " OR " & "(" & strDateFieldEnd & "IS NULL" & ")"
End If
If IsNull(Me.txtStartDate) Then
Me.txtStartDate = Date
strWhere = "(" & "(" & strDateFieldStart & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")" & " AND " & "(" & strDateFieldEnd & " >= " & Format(Me.txtStartDate, strcJetDate) & ")" & ")" & " OR " & "(" & strDateFieldEnd & "IS NULL" & ")"
End If
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.close acReport, strReport
End If
'Open the report.
Debug.Print strWhere
DoCmd.OpenReport strReport, lngView, , strWhere
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then
MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
End If
Resume Exit_Handler
End Sub