Combo box selection - criteria for query

P

PC

I have a query on which a report is based. The criteria for the query is
selected from a Combo Box on a form and a command botton on the form is used
to run the report. How would I specify that if the selection in the combo
box is blank that all records in the criteria column are selected and
visible in the report.

The code for the query is a follows:

SELECT Table.[Comapny Name], Table.Address, Table.Phone, Table.Category
FROM Table
WHERE (((Table.Category)=[Forms]![Form3]![Combo0]));

TIA

pc..
 
T

Tom Ellison

Dear PC:

SELECT [Comapny Name], Address, Phone, Category
FROM Table
WHERE Category =
IIf(Nz([Forms]![Form3]![Combo0], "") = "", Category,
[Forms]![Form3]![Combo0]);

This says that, if the combo box is NULL or empty, then filter by
Category = Category which will always be true. Thus, no value showing
in the combo box means do not filter.

I have a query on which a report is based. The criteria for the query is
selected from a Combo Box on a form and a command botton on the form is used
to run the report. How would I specify that if the selection in the combo
box is blank that all records in the criteria column are selected and
visible in the report.

The code for the query is a follows:

SELECT Table.[Comapny Name], Table.Address, Table.Phone, Table.Category
FROM Table
WHERE (((Table.Category)=[Forms]![Form3]![Combo0]));

TIA

pc..

Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts
 
J

JL

You can do one of the two things.

1) Create two queries and decide which one to use.

sub whichone ()
if isnull(me![combofield]) then
docmd.runquery "AllQuery"
else
docmd.runquery "Query1"
end if
end sub

AllQuery: select fields, ... from tables;

Query1: select fields, ... from tables where fields =
formfields;

2) Using "like", which will return everyting that
partially match your criteria.

SELECT fields, ....
FROM Tables
WHERE Table.Category like iif(isnull([Forms]![Form3]!
[Combo0]), "*", [Forms]![Form3]![Combo0] & "*");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top