Forum Discussion
Query with User Input Box
- Sep 18, 2022
Tony2021 Try below SQL with IIF() function in where clause. See the attached DB.
SELECT Table1.ProjID, Table1.DateStart, Table1.DateEnd, Table1.Description, Table1.Rate FROM Table1 WHERE (((Table1.Description) Like IIf(IsNull([Enter Criteria 1]),"*","*" & [Enter Criteria 1] & "*")) AND ((Table1.Rate)=[Enter DD No or enter for all]));Here when you will leave first inputbox blank then it will return just * by IIF() function. which will return all records of description field. Otherwise will return *Your Input Text*.
Tony2021 Try below SQL with IIF() function in where clause. See the attached DB.
SELECT Table1.ProjID, Table1.DateStart, Table1.DateEnd, Table1.Description, Table1.Rate
FROM Table1
WHERE (((Table1.Description) Like IIf(IsNull([Enter Criteria 1]),"*","*" & [Enter Criteria 1] & "*")) AND ((Table1.Rate)=[Enter DD No or enter for all]));Here when you will leave first inputbox blank then it will return just * by IIF() function. which will return all records of description field. Otherwise will return *Your Input Text*.
- Tony2021Sep 18, 2022Iron Contributor
HI Hasan, thank you for the response. I had to tweak a bit to give me exactly what I wanted but your code pushed me in the right direction.
this is what I ended up with (added an OR):WHERE (((tblBankAccounts.Beneficiary) Like IIf(IsNull([Enter Criteria 1]),"*","*" & [Enter Criteria 1] & "*")) AND ((tblPmtProposalALL.DDNo)=[Enter DD No or enter for all])) OR (((tblBankAccounts.Beneficiary) Like IIf(IsNull([Enter Criteria 1]),"*","*" & [Enter Criteria 1] & "*")));
I dont completely understand it myself. I did trial and error until I got what I wanted. This was more complicated than I thought. thank you. FYI: I am not testing for nulls since there would not be a case of any nulls.
thank you both!
- Harun24HRSep 19, 2022Bronze ContributorGlad to know!. Then you should tick mark my response instead of your reply.