Thank you very much for replying. I put a list box using the toolbox
and
selected the fields. It then just lists all of the entries in the
table
for
those fields. Am I starting off right? Will I be selecting the query
off
the list box itself or through applying the code. Don't know too much
about
programming, butI think I can manage if I get a bigger picture?
:
Hi Confused
You need to put a multi-select listbox on your form containing all the
possible values that can be selected. Then, when the time comes to
apply
your filter, you create a filter string from the selected values in
your
listbox.
For example, you have a multiselect listbox named lstSelectCities with
this
RowSource:
SELECT distinct City from Customers;
This will list all the possible cities - only one instance of each.
Now, you need to make a comma-separated list of the selected cities.
You
can do that like this:
Dim vItem As Variant, sList As String
Const DQ = """" ' one double-quote
With lstSelectCities
For Each vItem In .ItemsSelected
sList = sList & DQ & .ItemData(vItem) & DQ & ","
Next
sList= Left(sList, Len(sList) - 1) ' remove last comma
End If
End With
This will give you, for example: "St Louis","Miami","Omaha"
You can use the string you have constructed here to form part of your
WHERE
condition or filter string:
City In ("St Louis","Miami","Omaha")
Do a similar thing with your products. Here, you will probably want
to
use
a subquery to select the customers who have ordered a product:
CustomerID In (Select Order.Customer from Orders inner join
OrderItems
on Order.OrderNumber = OrderItems.OrderNumber where
OrderItems.ProductID
In
(5,42,99))
The part that comes from your listbox is the three ProductIDs that
have
been
selected:
5,42,99
Then put all of these WHERE fragments together:
SELECT * from Customers
WHERE (City In ("St Louis","Miami","Omaha"))
AND (CustomerID In (Select Order.Customer from Orders inner join
OrderItems
on Order.OrderNumber = OrderItems.OrderNumber where
OrderItems.ProductID
In
(5,42,99)))
--
Good Luck
Graham Mandeno [Access MVP]
Auckland, New Zealand
I want to run a query on a form that is bound to a Customers Table.
Combo
box
doesn't seem to do it. How do I select multiple criteria for the
same
field. E.g., Show me the customers that are in St Louis, Miami,
and
Omaha-Along with the customer that order product X and Z. etc.
Is
this
possible?
I then want to email the customers as a result of the query through
the
Contacts Table.