简体   繁体   中英

Combining where and IIF statements in Access

I have a form with three buttons relating to three yes/no variables that are not mutually exclusive in a subform. By default Access sets up the relationship using "and" so if I select two of the three buttons it shows me all the observations that have "yes" for both, but I want the buttons to have an "or" relationship so that if I select two buttons I want all the observations where it says yes to either of the two checked variables.

I'm trying to write a query that will do this, but Access doesn't support SQL case function and I can't get it to work with the IIF statement.

I want to code something along the lines of:

select var1,var2,var3 
from table 
where iif(form!form1!var1=yes and form!form1!var2=no and form!form1!var3=yes,table.var1=yes or table.var3=yes and table.var2=no,...)

It seems you can only use iif in certain places. Any ideas how to do this?

The SQL shown below will return the proper rows if the following steps are taken:

SELECT Table1.Var1, Table1.Var2, Table1.Var3, Table1.ID
FROM Table1
WHERE ((([Forms]![frmChildTable]![bVar1] And [var1])=True)) 
    OR ((([Forms]![frmChildTable]![bVar2] And [var2])=True)) 
    OR ((([Forms]![frmChildTable]![bVar3] And [var3])=True));
  • Create 3 textboxes on the form named bVar1, bVar2, bVar3
  • Set the format to Yes/No
  • Have your existing three buttons toggle the controls

I assume your use of the word 'button' meant 'Command Button'. If you wanted to use 'Option Buttons', then no code necessary to toggle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM