I'm trying to run the following as criteria in an MS access query. Basically what I want to do is:
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.
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.
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.
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.
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
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] & "*"
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With