Passing a null value to a query

P

PJFry

I am designing a form that allows a user to specify query conditions. I run
into trouble is the user does not want to limit on a particular item.

I have a form, fDrill that has three combo boxes, once of which is for the
customer name, cboCustName. The query that uses these conditions is qDrill.
The Customer Name field in qDrill has [Forms]![fDrill]![cboCustName] as the
condition. If the user does not choose a customer, then the query returns
nothing. How do I set either the form or the query to recognize that a combo
box with no value means that they do not want to add a condition to that
field and return all the customers?

Thanks!
PJ
 
K

Ken Snell \(MVP\)

Something like this:

SELECT *
FROM Tablename
WHERE FieldName = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL;

For three conditions:

SELECT *
FROM Tablename
WHERE (FieldName1 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL) AND
(FieldName2 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL)
(FieldName3 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL);
 
K

Ken Snell \(MVP\)

Sorry - typo in the "three condition" example -- forgot AND between second
and third conditions:

SELECT *
FROM Tablename
WHERE (FieldName1 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL) AND
(FieldName2 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL) AND
(FieldName3 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL);

--

Ken Snell
<MS ACCESS MVP>


Ken Snell (MVP) said:
Something like this:

SELECT *
FROM Tablename
WHERE FieldName = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL;

For three conditions:

SELECT *
FROM Tablename
WHERE (FieldName1 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL) AND
(FieldName2 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL)
(FieldName3 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL);

--

Ken Snell
<MS ACCESS MVP>


PJFry said:
I am designing a form that allows a user to specify query conditions. I
run
into trouble is the user does not want to limit on a particular item.

I have a form, fDrill that has three combo boxes, once of which is for
the
customer name, cboCustName. The query that uses these conditions is
qDrill.
The Customer Name field in qDrill has [Forms]![fDrill]![cboCustName] as
the
condition. If the user does not choose a customer, then the query
returns
nothing. How do I set either the form or the query to recognize that a
combo
box with no value means that they do not want to add a condition to that
field and return all the customers?

Thanks!
PJ
 
P

PJFry

That did it. It took me a while to nail it down because my form references
were not in order, I had a form reference followed by a few static query
conditions followed by the other form references and the SQL didn't like
that. Once I ordered the references and conditions, it worked great. Wicked
looking in design view. Glad I didn't try to figure it out there.

Thanks Ken!

PJ

Ken Snell (MVP) said:
Sorry - typo in the "three condition" example -- forgot AND between second
and third conditions:

SELECT *
FROM Tablename
WHERE (FieldName1 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL) AND
(FieldName2 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL) AND
(FieldName3 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL);

--

Ken Snell
<MS ACCESS MVP>


Ken Snell (MVP) said:
Something like this:

SELECT *
FROM Tablename
WHERE FieldName = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL;

For three conditions:

SELECT *
FROM Tablename
WHERE (FieldName1 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL) AND
(FieldName2 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL)
(FieldName3 = Forms!FormName!ComboBoxName
OR Forms!FormName!ComboBoxName IS NULL);

--

Ken Snell
<MS ACCESS MVP>


PJFry said:
I am designing a form that allows a user to specify query conditions. I
run
into trouble is the user does not want to limit on a particular item.

I have a form, fDrill that has three combo boxes, once of which is for
the
customer name, cboCustName. The query that uses these conditions is
qDrill.
The Customer Name field in qDrill has [Forms]![fDrill]![cboCustName] as
the
condition. If the user does not choose a customer, then the query
returns
nothing. How do I set either the form or the query to recognize that a
combo
box with no value means that they do not want to add a condition to that
field and return all the customers?

Thanks!
PJ
 

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