D
DeDBlanK
I having trouble understanding what I am doing wrong here. I have
added to Mr. Browne's code to pull some Listboxes, with multiselect
enabled, passing it through the filter option on a report.
Issue is that it was working, but I kept getting errors passing
through the WHERE clause on the report. Now I can't get anything to
show up other that the original code information.
If someone could please help by pointing me in the correct direction,
I would be greatly appreciative.
Thanks
***********************************code*******************************
Private Sub cmdReport_Click()
'On Error GoTo Err_Handler 'Remove the single quote from start of
this line once you have it working.
'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
Dim varItemShift As Variant
Dim varItemDept As Variant
Dim strWhereShift As String
Dim strWhereDept As String
Dim strCriteria As String
Dim loqd As QueryDef
Dim strShiftField As String
Dim strDeptField As String
Const strcJetDate = "\#mm\/dd\/yyyy\#" 'Do NOT change it to
match your local settings.
'DO set the values in the next 3 lines.
strReport = "rptDTSum" 'Put your report name in these
quotes.
strDateField = "[dtmDate]" 'Put your field name in the square
brackets in these quotes.
lngView = acViewPreview 'Use acViewNormal to print instead
of preview.
'Build the filter string.
If IsDate(Me.txtStartDate) Then
strWhere = "(" & strDateField & " >= " & Format
(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format
(Me.txtEndDate + 1, strcJetDate) & ")"
End If
'build filter string for Shift
strShiftField = "[strShift]"
If strWhereShift <> vbNullString Then
If strWhere <> vbNullString Then
With Me.lstShift
For Each varItemShift In .ItemsSelected
strWhereShift = strWhereShift & "(" &
strShiftField & " = '" & .ItemData(varItemShift) & "') OR "
Next varItemShift
End With
'Strip the " OR "
strWhereShift = " OR " & strWhereShift & Left
(strWhereShift, Len(strWhereShift) - 5)
Else
With Me.lstShift
For Each varItemShift In .ItemsSelected
strWhereShift = strWhereShift & strShiftField
& "= '" & .ItemData(varItemShift) & "' ,"
Next varItemShift
End With
'Strip the ", "
strWhereShift = strWhereShift & Left(strWhereShift, Len
(strWhereShift) - 2)
End If
End If
'build filter string for Dept
strDeptField = "[strResponsible]"
If strWhereDept <> vbNullString Then
If strWhere <> vbNullString Then
With Me.lstDept
For Each varItemDept In .ItemsSelected
strWhereDept = strWhereDept & strDeptField &
"= '" & .ItemData(varItemDept) & "') OR "
Next varItemDept
End With
strWhereDept = " OR " & strWhereDept & Left
(strWhereDept, Len(strWhereDept) - 5)
Else
With Me.lstDept 'if neither or null strip " OR "
For Each varItemDept In .ItemsSelected
strWhereDept = strWhereDept & strDeptField &
"= '" & .ItemData(varItemDept) & "' ,"
Next varItemDept
End With
'Strip the ", "
strWhereDept = strWhereDept & Left(strWhereDept, Len
(strWhereDept) - 2)
End If
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
'Open the report.
Debug.Print strWhereShift
Debug.Print strWhereDept
Debug.Print strWhere 'Remove the single quote from the
start of this line for debugging purposes.
DoCmd.OpenReport strReport, lngView, , strWhere &
strWhereShift & strWhereDept
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
*******************************end code*******************************
added to Mr. Browne's code to pull some Listboxes, with multiselect
enabled, passing it through the filter option on a report.
Issue is that it was working, but I kept getting errors passing
through the WHERE clause on the report. Now I can't get anything to
show up other that the original code information.
If someone could please help by pointing me in the correct direction,
I would be greatly appreciative.
Thanks
***********************************code*******************************
Private Sub cmdReport_Click()
'On Error GoTo Err_Handler 'Remove the single quote from start of
this line once you have it working.
'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
Dim varItemShift As Variant
Dim varItemDept As Variant
Dim strWhereShift As String
Dim strWhereDept As String
Dim strCriteria As String
Dim loqd As QueryDef
Dim strShiftField As String
Dim strDeptField As String
Const strcJetDate = "\#mm\/dd\/yyyy\#" 'Do NOT change it to
match your local settings.
'DO set the values in the next 3 lines.
strReport = "rptDTSum" 'Put your report name in these
quotes.
strDateField = "[dtmDate]" 'Put your field name in the square
brackets in these quotes.
lngView = acViewPreview 'Use acViewNormal to print instead
of preview.
'Build the filter string.
If IsDate(Me.txtStartDate) Then
strWhere = "(" & strDateField & " >= " & Format
(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format
(Me.txtEndDate + 1, strcJetDate) & ")"
End If
'build filter string for Shift
strShiftField = "[strShift]"
If strWhereShift <> vbNullString Then
If strWhere <> vbNullString Then
With Me.lstShift
For Each varItemShift In .ItemsSelected
strWhereShift = strWhereShift & "(" &
strShiftField & " = '" & .ItemData(varItemShift) & "') OR "
Next varItemShift
End With
'Strip the " OR "
strWhereShift = " OR " & strWhereShift & Left
(strWhereShift, Len(strWhereShift) - 5)
Else
With Me.lstShift
For Each varItemShift In .ItemsSelected
strWhereShift = strWhereShift & strShiftField
& "= '" & .ItemData(varItemShift) & "' ,"
Next varItemShift
End With
'Strip the ", "
strWhereShift = strWhereShift & Left(strWhereShift, Len
(strWhereShift) - 2)
End If
End If
'build filter string for Dept
strDeptField = "[strResponsible]"
If strWhereDept <> vbNullString Then
If strWhere <> vbNullString Then
With Me.lstDept
For Each varItemDept In .ItemsSelected
strWhereDept = strWhereDept & strDeptField &
"= '" & .ItemData(varItemDept) & "') OR "
Next varItemDept
End With
strWhereDept = " OR " & strWhereDept & Left
(strWhereDept, Len(strWhereDept) - 5)
Else
With Me.lstDept 'if neither or null strip " OR "
For Each varItemDept In .ItemsSelected
strWhereDept = strWhereDept & strDeptField &
"= '" & .ItemData(varItemDept) & "' ,"
Next varItemDept
End With
'Strip the ", "
strWhereDept = strWhereDept & Left(strWhereDept, Len
(strWhereDept) - 2)
End If
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
'Open the report.
Debug.Print strWhereShift
Debug.Print strWhereDept
Debug.Print strWhere 'Remove the single quote from the
start of this line for debugging purposes.
DoCmd.OpenReport strReport, lngView, , strWhere &
strWhereShift & strWhereDept
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
*******************************end code*******************************