Is "where If" Possible

modest

Registered User.
Local time
Today, 08:06
Joined
Jan 4, 2005
Messages
1,220
I've never used an IF clause in Access SQL. I've only used IF in Oracle to define functions and whatnot.

I have a form that has a listbox based on a query. This form is called twice depending on 2 textboxes from another form. I was wondering if I could do this so I wouldn't have 2 of the same forms with almost the same SQL statement.

I was wondering if I could do something similar to:

Code:
SELECT * 
FROM [Table Name]
WHERE
(
     IF ([Forms]![Form Name]![Text Box1].Value is null)
     THEN
            WHERE ([Table Name].[ID]=[Forms]![Form Name]![Text Box1].Value
     ELSE
            WHERE ([Table Name].[ID]=[Forms]![Form Name]![Text Box2].Value
);
Is something like this possible?

Otherwise I think this might work:
Code:
SELECT * 
FROM [Table Name]
WHERE
(
     (
          [Forms]![Form Name]![Text Box1].Value is null AND
          [Table Name].[ID]=[Forms]![Form Name]![Text Box1].Value
     )
     OR
     (
          [Forms]![Form Name]![Text Box1].Value is not null AND
          [Table Name].[ID]=[Forms]![Form Name]![Text Box2].Value
     )
);

..This way does work; I still haven't tested the first way yet (no time); maybe someone knows if you can do IF statements in a WHERE
 
Last edited:
have you thought about using a IIF statement.
 
Not at all, never had to use IIF or IF statements in queries.

I'll look into it though, thank you :) , but for right now my second query does work and I'm happy about that. Just, what would be faster: many where conditions or using IIF. Or would there be a difference?

-modest
 
Code:
SELECT * 
FROM [Table Name]
WHERE
(
     IF ([Forms]![Form Name]![Text Box1].Value is null)
     THEN
            WHERE ([Table Name].[ID]=[Forms]![Form Name]![Text Box1].Value
     ELSE
            WHERE ([Table Name].[ID]=[Forms]![Form Name]![Text Box2].Value
);

Try copying/pasting this into the SQL view for your form query:

Code:
SELECT *
FROM [Table Name]
WHERE (((IIf(IsNull([Forms]![Form Name]![Text Box1]),True,IIf([Table Name]![ID]
=[Forms]![Form Name]![Text Box2],True,False)))=True));

hth,
 

Users who are viewing this thread

Back
Top Bottom