Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIF query using OR operators not working

I'm trying to run the following as criteria in an MS access query. Basically what I want to do is:

  • If checkbox = True then all records, including those with blank or Null fields are shown (the default value in my form's combo box [combo9] is "*")
  • If checkbox = False then only records that match the value in Combo9 are shown

My current expression doesn't give any errors, but also doesn't produce any results! The TRUE and FALSE halves of the expression work fine on their own, but don't work when combined into the iif expression.

Like IIf([Forms]![F_leg_reg]![Check25]=True,Like [Forms]![F_leg_reg]![Combo9] Or "" Or Is Null,Like [Forms]![F_leg_reg]![Combo9])

Can someone please tell me what I'm doing wrong here? Thanks in advance.

like image 681
Reigncloud Avatar asked Feb 16 '12 11:02

Reigncloud


People also ask

What is the difference between IF and IIF?

Both the IF and IIF first check if the test is true; but the IIF then tests if the value is False. IF doesn't test for the False component – it treats everything not True in the same way. IIF handles those items that aren't True or False (or Unknown) differently to IF.

How many arguments does the IIF function have?

The IIF function contains three parts, or arguments: A logical test, which is a value or formula, that can be evaluated as true or false. The value that is returned if the logical test is true.

What is the definition of the IIF immediate IF function?

In computing, IIf (an abbreviation for Immediate if) is a function in several editions of the Visual Basic programming language and ColdFusion Markup Language (CFML), and on spreadsheets that returns the second or third parameter based on the evaluation of the first parameter.


2 Answers

Not sure you need so many likes, you are using the or logical operator in the return value which doesn't make sense

IIF (condition, value-if-true, value-if-false)

so..

Like IIF ([forms]![foo].[text1] = "1" OR [forms]![foo].[text2] = "1", 'bar', 'foobar')

if either conditions are met IIF will return 'bar' else it will return 'foobar'

you can nest it if you want so

Like IIF ([forms]![foo].[text1] = "1" OR [forms]![foo].[text2] = "1", 'bar', 
        IIF ([forms]![foo].[text1] = "2" AND [forms]![foo].[text2] = "2", 'foobar', null)
    )

if either conditions are met IIF will return 'bar' else we check to see if text1 and text2 are "2" if so return 'foobar' else we return null

hope this helps

like image 155
T I Avatar answered Sep 30 '22 21:09

T I


You're repeating the keyword, Like, within the IIf() expression. That won't work. You could fix the IIf(), but I would use a different approach ... which fits my brain better.

This will returns all rows when the checkbox is checked, but no rows when unchecked.

SELECT a_field, another_field, field_2_search 
FROM YourTable
WHERE [Forms]![F_leg_reg]![Check25]=True;

When the checkbox is unchecked, limit the rows to those where the field_2_search values contain the combo's value.

SELECT a_field, another_field, field_2_search
FROM YourTable
WHERE
    [Forms]![F_leg_reg]![Check25]=False
    AND field_2_search Like "*" & [Forms]![F_leg_reg]![Combo9] & "*";

But you want only one query, not two, so combine those WHERE clauses with an OR.

SELECT a_field, another_field, field_2_search 
FROM YourTable
WHERE
    [Forms]![F_leg_reg]![Check25]=True
    OR (
    [Forms]![F_leg_reg]![Check25]=False
    AND field_2_search Like "*" & [Forms]![F_leg_reg]![Combo9] & "*"
    );
like image 27
HansUp Avatar answered Sep 30 '22 21:09

HansUp