问题
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.
回答1:
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
回答2:
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] & "*"
);
回答3:
I am very late coming to this discussion, but I found this post when I encountered the same problem. I have since solved it, and want to share.
The problem is the location of your Iif statement. Since you are using Access, you are probably placing the Iif statement in the Criteria row on the query design view. Delete that. Instead, you need to make the Iif statement its own field in query.
You also need to be very careful with the oporators you use with Null and "*". The syntax is very touchy.
In a new query field, enter:
IIf([Combo_Box] Is Null,[Field1] Is Null Or [Field1] Like "*",[Field1]=[Combo_Box])
Then set the criteria for that field to be <> False.
The SQL would look like this:
SELECT [Field1], [Field2], etc
FROM Table1
WHERE (IIf([Combo_Box] Is Null,[Field1] Is Null Or [Field1] Like "*",[Field1]=[Combo_Box])<>False);
Hope this helps! Sorry for being so late to the party!.
来源:https://stackoverflow.com/questions/9310098/iif-query-using-or-operators-not-working