Connecting Tech Pros Worldwide Forums | Help | Site Map

Filter Report on multiple selections in a list box

arthur-e
Guest
 
Posts: n/a
#1: Nov 12 '05
I can filter a query for a report on a field of a subform BUT now....
I'd like to be able to select more than one item in a multi-list box
to select all the records.

ALSO
to use two fields (or more) fields to filter a query based on
selections in two or more fields:

All the records with NAME = all the names selected in a list box
BETWEEN StartDate and EndDate

HELPPP please.

-warning e-mail address altered- arthureNOSPACE@

Allen Browne
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Filter Report on multiple selections in a list box


If possible, use the WhereCondition of the OpenReport action instead of
filtering in the query.

This assumes you have a form that contains the listbox and textboxes used
for filtering. The form will have a command button to open the report. The
OpenReport action has a WhereCondition. Build up a string consisting of the
values you wish to filter by, and then use that as the WhereCondition of the
OpenReport action.

To create the string from the items in a multi-select list box, your code
will need to loop through the ItemsSelected collection of the list box. Use
ItemData. Remember to include the quote delimiters if the field being
filtered is of type Text (not Number). After concatenating the items
together, use the IN operator. The resultant string will be something like
this:
"[Surname] IN (""Jones"", ""Smith"", ""O'Brien"")"

For multiple conditions, include an AND in the WhereCondition string.

For an example of how to work with a start date and end date, see:
Limiting a Report to a Date Range
at:
http://allenbrowne.com/casu-08.html

--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"arthur-e" <arthure@ix.netcom.com> wrote in message
news:dup1nvgoa2f117upamieqdmevnah23674l@4ax.com...[color=blue]
> I can filter a query for a report on a field of a subform BUT now....
> I'd like to be able to select more than one item in a multi-list box
> to select all the records.
>
> ALSO
> to use two fields (or more) fields to filter a query based on
> selections in two or more fields:
>
> All the records with NAME = all the names selected in a list box
> BETWEEN StartDate and EndDate[/color]


arthur-e
Guest
 
Posts: n/a
#3: Nov 12 '05

re: Filter Report on multiple selections in a list box


Thanks, Allen - yes, I'd like to use the WhereCondition not really a
filter - but I always have trouble with the syntax -
I thought the WhereCondition would be a concantenation with "OR"
between the selected items.

Do you know of an example somewhere on the web I could look at?

Arthur

"Allen Browne" <abrowne1_SpamTrap@bigpond.net.au> wrote:
[color=blue]
>If possible, use the WhereCondition of the OpenReport action instead of
>filtering in the query.
>
>This assumes you have a form that contains the listbox and textboxes used
>for filtering. The form will have a command button to open the report. The
>OpenReport action has a WhereCondition. Build up a string consisting of the
>values you wish to filter by, and then use that as the WhereCondition of the
>OpenReport action.
>
>To create the string from the items in a multi-select list box, your code
>will need to loop through the ItemsSelected collection of the list box. Use
>ItemData. Remember to include the quote delimiters if the field being
>filtered is of type Text (not Number). After concatenating the items
>together, use the IN operator. The resultant string will be something like
>this:
> "[Surname] IN (""Jones"", ""Smith"", ""O'Brien"")"
>
>For multiple conditions, include an AND in the WhereCondition string.
>
>For an example of how to work with a start date and end date, see:
> Limiting a Report to a Date Range
>at:
> http://allenbrowne.com/casu-08.html[/color]


-warning e-mail address altered- arthureNOSPACE@
Allen Browne
Guest
 
Posts: n/a
#4: Nov 12 '05

re: Filter Report on multiple selections in a list box


If you use the OR operator, all the records that are named in the list box
will be included. Any records between the dates will also be included
(regardless of whether they match the listbox), since they only have to met
one condition or the other.

If you use the AND operator, only the records that are named in the list box
and also fall in the date range will be returned.

The basic idea for looping through the items of the list box is this:

Dim vItem As Variant
Dim lngLen as Long

With Me.MyListbox
For Each vItem In .ItemsSelected
If Not IsNull(vItem) Then
strWhere = strWhere & """" & .ItemData(vItem) & """, "
End If
Next
End With
lngLen = Len(strWhere) - 2 'Without trailing comma and space.
If lngLen > 0 Then
strWhere = "[MyField] IN (" & Left$(strWhere, lngLen) & ")"
End If


--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Tips for Access users - http://allenbrowne.com/tips.html

"arthur-e" <arthure@ix.netcom.com> wrote in message
news:7jv1nv847bksqin7mcmt8u4bdsq9alb59f@4ax.com...[color=blue]
> Thanks, Allen - yes, I'd like to use the WhereCondition not really a
> filter - but I always have trouble with the syntax -
> I thought the WhereCondition would be a concantenation with "OR"
> between the selected items.
>
> Do you know of an example somewhere on the web I could look at?
>
> Arthur
>
> "Allen Browne" <abrowne1_SpamTrap@bigpond.net.au> wrote:
>[color=green]
> >If possible, use the WhereCondition of the OpenReport action instead of
> >filtering in the query.
> >
> >This assumes you have a form that contains the listbox and textboxes used
> >for filtering. The form will have a command button to open the report.[/color][/color]
The[color=blue][color=green]
> >OpenReport action has a WhereCondition. Build up a string consisting of[/color][/color]
the[color=blue][color=green]
> >values you wish to filter by, and then use that as the WhereCondition of[/color][/color]
the[color=blue][color=green]
> >OpenReport action.
> >
> >To create the string from the items in a multi-select list box, your code
> >will need to loop through the ItemsSelected collection of the list box.[/color][/color]
Use[color=blue][color=green]
> >ItemData. Remember to include the quote delimiters if the field being
> >filtered is of type Text (not Number). After concatenating the items
> >together, use the IN operator. The resultant string will be something[/color][/color]
like[color=blue][color=green]
> >this:
> > "[Surname] IN (""Jones"", ""Smith"", ""O'Brien"")"
> >
> >For multiple conditions, include an AND in the WhereCondition string.
> >
> >For an example of how to work with a start date and end date, see:
> > Limiting a Report to a Date Range
> >at:
> > http://allenbrowne.com/casu-08.html[/color]
>
>
> -warning e-mail address altered- arthureNOSPACE@[/color]


Closed Thread