简体   繁体   中英

MS Access SQL If Then statement for check boxes

I am trying to get a search query to run. It is as follows. The last two lines of code do not work. It is a check box. If the checkboxYes is checked then pull only the items in the YES/NO field of the database that are checked. If the checkboxNo is checked then pull only the items in the YEs/No field of the database that are checked. If BOTH boxes are checked, then pull all items. I cannot get either scenario to work. All other fields except the check boxes work correctly.

 SELECT *
 FROM NLog
 Where
(([Forms]![FrmSearch]![Combo13] is Null) OR ((NLog.State)=[Forms]![FrmSearch]![Combo13])) and

(([Forms]![FrmSearch]![daterecsrt] is Null) or (((NLog.DateRec) Between [Forms]![FrmSearch]![daterecsrt] And [Forms]![FrmSearch]![daterecend]))) and

(([Forms]![FrmSearch]![datesrttxt3] is Null) or (((NLog.DateRec) Between [Forms]![FrmSearch]![datesrttxt3] And [Forms]![FrmSearch]![dateendtxt3]))) and

(([Forms]![FrmSearch]![checkyes] is Null) or  ((NLog.Compchk)=True)) and

(([Forms]![FrmSearch]![checkNo] is Null) or  ((NLog.Compchk)=False)) 

Thank you!

I am using MS Acess 2010. This is using the boxes on a form to run the query.

NLog is a table so why are you referencing fields in the table as [Forms]![FrmSearch]![Combo13] and etc. Those are controls on a form and not fields in your table. Shouldn't your query really be:

SELECT * FROM NLog WHERE
(NLog.State is Null OR NLog.State = Forms!FrmSearch!Combo13)
AND (NLog.DateRec is Null OR NLog.DateRec Between Forms!FrmSearch!daterecsrt And Forms!FrmSearch!daterecend
AND (NLog.DateRec is Null OR NLog.DateRec Between Forms!FrmSearch!datesrttxt3 And Forms!FrmSearch!dateendtxt3) 
AND (NLog.Compchk is Null OR  NLog.Compchk = True) 
AND (NLog.Compchk is Null OR NLog.Compchk = False) 

Also, the following will not give you any results:

AND (NLog.Compchk is Null OR NLog.Compchk = True) 
AND (NLog.Compchk is Null OR NLog.Compchk = False) 

NLog.Compchk can not be both True and False . It is going to be one or the other or Null .

You will need to check if the boxes are checked and build the last part of your query around that, (ie IIF() ).

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