I can pull a report that is created using a StartDate & EndDate function. I freely admit that I got it from the net as I was struggling to write something myself, even so it works very nicely.
Now what I’d like to do is add an “If” statement to it. I presume this would be the right thing to do given what I'm trying to achieve.
My Customer forms have a text box entry named txtEECust and if there is a “Yes” in the box then I’d like the function to pull only the reports that are within the start and end dates along with the txtEECust = Yes. Any forms that are within the date selection criteria and txtEECust = No are excluded.
I tried adding If (Me.EECust = "Yes") Then..... to the below but it didn't work.
CODE
Private Sub Command12_Click()
On Error GoTo Err_Handler
'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 NOT change it to match your local settings.
strReport = "rptEEBoilerServiceLetter"
strDateField = "[NextBoilerServiceDate]"
lngView = acViewPreview
If IsDate(Me.txtEEBoilerSLStartDate) Then
strWhere = "(" & strDateField & " >= " & Format(Me.txtEEBoilerSLStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEEBoilerSLEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEEBoilerSLEndDate + 1, strcJetDate) & ")"
End If
'Close the report if already open: otherwise it won't filter properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
'Debug.Print strWhere 'Remove the single quote from the start of this line for debugging purposes.
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
On Error GoTo Err_Handler
'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 NOT change it to match your local settings.
strReport = "rptEEBoilerServiceLetter"
strDateField = "[NextBoilerServiceDate]"
lngView = acViewPreview
If IsDate(Me.txtEEBoilerSLStartDate) Then
strWhere = "(" & strDateField & " >= " & Format(Me.txtEEBoilerSLStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEEBoilerSLEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEEBoilerSLEndDate + 1, strcJetDate) & ")"
End If
'Close the report if already open: otherwise it won't filter properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
'Debug.Print strWhere 'Remove the single quote from the start of this line for debugging purposes.
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