A
ATSBC03
I have a report being filtered by multiple combo boxes (cboSite, cboDept,
cboType, etc.) and 2 date fields (txtEndDate and txtStartDate). The combo
boxes and date fields are on an unbound form that allows the user to select
any or all of the criteria to sort the report.
The problem I am having is trying to code the date fields. In my underlying
table there are two possible dates ([Date of Occurence] and [Date Reported])
I need the filter to check and allow the records between either date range
because sometimes the occurrence happens months before it is reported. Below
is my attempt at coding the on click command using information gathered from
the discussion group and Allen Browne's website. I have been unable to
incorporate the dates. I have been getting a type mismatch error and i'm not
sure where it is.
The other question I have is how to get the information gathered in the
unbound form to print in the header of the report. I would like to use if
statements due to the ability to not choose. For Example, if there is a site
and no type then i would like the header to print the site name and then
print "All types" to inform the user that no type was selected.
Any help would be appreciated. Thanks.
Private Sub cmdOK_Click()
Dim strDocName As String
Dim strWhere As String
Dim strOccDate As String
Dim strRptDate As String
Const conDateFormat = "\#mm\/dd\/yyyy\#"
strWhere = "1=1"
stDocName = "OccurrenceReportSpecSiteDept"
strOccDate = "[Date of Occurence]"
strRptDate = "[Date Reported]"
If IsNull(Me.txtStartDate) Then
If Not IsNull(Me.txtEndDate) Then 'End Date, no start Date
strWhere = strWhere & " AND ( strOccDate & " <= " &
Format(Me.txtEndDate, conDateFormat)" _
& " Or " & strRptDate & " <= " & Format(Me.txtEndDate,
conDateFormat) & " )"
End If
Else
If IsNull(Me.txtEndDate) Then ' Start Date, no end date
strWhere = strWhere & " AND ( strOccDate & " >= " &
Format(Me.txtEndDate, conDateFormat)" _
& " Or " & strRptDate & " >= " & Format(Me.txtEndDate,
conDateFormat) & " )"
Else 'Both start and End Date
strWhere = strWhere & " AND ( strOccDate & "" Between "" &
Format(Me.txtStartDate, conDateFormat)" _
& "" And "" & Format(Me.txtEndDate, conDateFormat) _
& "" Or "" & strRptDate & " Between " &
Format(Me.txtStartDate, conDateFormat) _
& "" And "" & Format(Me.txtEndDate, conDateFormat) & " )"
Debug.Print strWhere
End If
End If
If Not IsNull(Me.cboSite) Then
strWhere = strWhere & " AND [Site] = """ & _
Me.cboSite & """"
End If
If Not IsNull(Me.cboType) Then
strWhere = strWhere & " AND [Type] = """ & _
Me.cboType & """"
End If
If Not IsNull(Me.cboDept) Then
strWhere = strWhere & " AND ( [Dept/Specialty] = """ & _
Me.cboDept & """ OR [Dept/Specialty2] = """ & _
Me.cboDept & """ OR [Dept/Specialty3] = """ & _
Me.cboDept & """ ) "
End If
Debug.Print strWhere
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
DoCmd.Close acForm, "frmSiteDept"
End Sub
cboType, etc.) and 2 date fields (txtEndDate and txtStartDate). The combo
boxes and date fields are on an unbound form that allows the user to select
any or all of the criteria to sort the report.
The problem I am having is trying to code the date fields. In my underlying
table there are two possible dates ([Date of Occurence] and [Date Reported])
I need the filter to check and allow the records between either date range
because sometimes the occurrence happens months before it is reported. Below
is my attempt at coding the on click command using information gathered from
the discussion group and Allen Browne's website. I have been unable to
incorporate the dates. I have been getting a type mismatch error and i'm not
sure where it is.
The other question I have is how to get the information gathered in the
unbound form to print in the header of the report. I would like to use if
statements due to the ability to not choose. For Example, if there is a site
and no type then i would like the header to print the site name and then
print "All types" to inform the user that no type was selected.
Any help would be appreciated. Thanks.
Private Sub cmdOK_Click()
Dim strDocName As String
Dim strWhere As String
Dim strOccDate As String
Dim strRptDate As String
Const conDateFormat = "\#mm\/dd\/yyyy\#"
strWhere = "1=1"
stDocName = "OccurrenceReportSpecSiteDept"
strOccDate = "[Date of Occurence]"
strRptDate = "[Date Reported]"
If IsNull(Me.txtStartDate) Then
If Not IsNull(Me.txtEndDate) Then 'End Date, no start Date
strWhere = strWhere & " AND ( strOccDate & " <= " &
Format(Me.txtEndDate, conDateFormat)" _
& " Or " & strRptDate & " <= " & Format(Me.txtEndDate,
conDateFormat) & " )"
End If
Else
If IsNull(Me.txtEndDate) Then ' Start Date, no end date
strWhere = strWhere & " AND ( strOccDate & " >= " &
Format(Me.txtEndDate, conDateFormat)" _
& " Or " & strRptDate & " >= " & Format(Me.txtEndDate,
conDateFormat) & " )"
Else 'Both start and End Date
strWhere = strWhere & " AND ( strOccDate & "" Between "" &
Format(Me.txtStartDate, conDateFormat)" _
& "" And "" & Format(Me.txtEndDate, conDateFormat) _
& "" Or "" & strRptDate & " Between " &
Format(Me.txtStartDate, conDateFormat) _
& "" And "" & Format(Me.txtEndDate, conDateFormat) & " )"
Debug.Print strWhere
End If
End If
If Not IsNull(Me.cboSite) Then
strWhere = strWhere & " AND [Site] = """ & _
Me.cboSite & """"
End If
If Not IsNull(Me.cboType) Then
strWhere = strWhere & " AND [Type] = """ & _
Me.cboType & """"
End If
If Not IsNull(Me.cboDept) Then
strWhere = strWhere & " AND ( [Dept/Specialty] = """ & _
Me.cboDept & """ OR [Dept/Specialty2] = """ & _
Me.cboDept & """ OR [Dept/Specialty3] = """ & _
Me.cboDept & """ ) "
End If
Debug.Print strWhere
DoCmd.OpenReport stDocName, acViewPreview, , strWhere
DoCmd.Close acForm, "frmSiteDept"
End Sub