Allen,
Thanks for the help.
I tried to do what you said, and I started off small (ie
just trying to get the listbox for priority to work
first). I created the form with the priority listbox and
command button for the report.
When I click on the command button, I get prompted to
enter Priority, and I get nothing in the report.
What have I done wrong?
Here is the code behind the command button for the report
preview:
Private Sub ProjectListReportPreview_Click()
Dim lst As ListBox
Dim varItem As Variant
Dim strOut As String
Dim strWhere As String
Dim lngLen As Long
'LISTBOX ITEMS
Set lst = Me.[PriorityList]
Select Case lst.ItemsSelected.Count
Case 0
'Nothing selected.
Case 1
strWhere = strWhere & "([Priority] = """ &
lst.ItemData(lst.ItemsSelected(0)) & """) AND "
Case Else
'Multiple selection: loop through ItemsSelected.
For Each varItem In lst.ItemsSelected
If Not IsNull(varItem) Then
strOut = strOut & """" & lst.ItemData
(varItem) & ""","
End If
Next
strOut = Left$(strOut, Len(strOut) - 1)
'Remove trailing comma.
strWhere = strWhere & "([Priority] IN (" & strOut
& ")) AND "
End Select
'DATES
'If IsNull(Me.txtStartDate) Then
' If Not IsNull(Me.txtEndDate) Then
'End, no start.
'strWhere = strWhere & "([Completion Date] <= "
& _
'Format (Me.txtEndDate, "\#mm\/dd\/yyyy\#")
& ") AND "
'End If
'Else
' If IsNull(Me.txtEndDate) Then
'Start, no end.
'strWhere = strWhere & "([Completion Date] = "
& _
'Format
(Me.txtStartDate, "\#mm\/dd\/yyyy\#") & ") AND "
' Else
'Both start and end.
' strWhere = strWhere & "([Completion Date]
Between " & _
' Format (Me.txtEndDate, "\#mm\/dd\/yyyy\#")
& " And " & _
' Format
(Me.txtStartDate, "\#mm\/dd\/yyyy\#") & ") AND "
' End If
'End If
'and so on for other controls
lngLen = Len(strWhere) - 5 'without trailing " AND "
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If
DoCmd.OpenReport "Project Listing", acViewPreview, ,
strWhere
Set lst = Nothing
End Sub
-----Original Message-----
Create an unbound form, with the controls for filtering the report:
- a list box, with its mutli-select property set to Simple so the user can
select more than one;
- a pair of text boxes for entering the starting date
and
ending date;
- a combo based on a Value List, where the user can select Open, Closed, or
Both.
- a command button to open the report.
The example below shows how to build up a string from
the
non-null text
boxes, to use as the WhereCondition for the OpenReport action. It picks up
the items from the multi-select listbox by looping through its ItemsSelected
collection, and then handles the dates. In each case,
it
adds a trailing "
AND ", so you can build up as many conditions as you need. Then at the end
it chops off the trailing AND.
Private cmdPreview_Click()
Dim lst As Listbox
Dim varItem as Variant
Dim strOut As String
Dim strWhere As String
Dim lngLen As Long
'LISTBOX ITEMS
Set lst = Me.[NameOfYourListBoxHere]
Select Case lst.ItemsSelected.Count
Case 0
'Nothing selected.
Case 1
strWhere = strWhere & "([Priority] = """ &
lst.ItemData(lst.ItemsSelected(0)) & """) AND "
Case Else
'Multiple selection: loop through ItemsSelected.
For Each varItem In lst.ItemsSelected
If Not IsNull(varItem) Then
strOut = strOut & """" & lst.ItemData (varItem) & ""","
End If
Next
strOut = Left$(strOut, Len(strOut) -
1) 'Remove
trailing comma.
strWhere = strWhere & "([Priority] IN (" &
strOut
& ")) AND "
End Select
'DATES
If IsNull(Me.txtStartDate) Then
If Not IsNull(Me.txtEndDate) Then 'End, no start.
strWhere = strWhere & "([Completion Date] <= " & _
Format
(Me.txtEndDate, "\#mm\/dd\/yyyy\#")
& ") AND "
End If
Else
If IsNull(Me.txtEndDate) Then 'Start, no end.
strWhere = strWhere & "([Completion Date]
= " & _
Format
(Me.txtStartDate,"\#mm\/dd\/yyyy\#") & ") AND "
Else 'Both start and end.
strWhere = strWhere & "([Completion Date] Between " & _
Format
(Me.txtEndDate, "\#mm\/dd\/yyyy\#")
& " And " & _
(Me.txtStartDate, "\#mm\/dd\/yyyy\#") & ") AND "
End If
End If
'and so on for other controls
lngLen = Len(strWhere) - 5 'without trailing " AND "
If lngLen > 0 Then
strWhere = Left$(strWhere, lngLen)
End If
DoCmd.OpenReport "MyReport", acViewPreview, , strWhere
Set lst = Nothing
End Sub
Whats the easiest way to allow report filtering?
For example I have a table which has the following fields:
Priority (input values are A,B,C,D,E)
Leader (names)
Completion Date
Status (Open, Closed) [actually this field is calculated
by a query which looks at the completion date and compares
it to the current date]
I'd like my users to be able to enter the priority (A
through E, or all, or a combination like A+B), Completion
date range, and open or closed. And then have the report
only generate this data.
How do I do this?