简体   繁体   中英

SQL query based on criteria

it's working if all the feilds is entered by user. i need a code that combine all the sql command. user may enter in the one field or two field or all the three fields. i need to search the database with one field query or two feild query or three feilds query.

i have try it with where help is a table & search-test is form & contract no,username & date of feild in database and forms.

where the help(table).cont_no(field) is equal or not equal to search-test(form name).cont_no(text box field)

SELECT * 
FROM   help 
WHERE  ( forms ! [search-test] ! cont_no = '' 
          OR help.cont_no = forms ! [search-test] ! cont_no ) 
       AND ( forms ! [search-test] ! username = '' 
              OR help.username = forms ! [search-test] ! username ) 
       AND ( forms ! [search-test] ! cbo_date = '' 
              OR help.DATE = forms ! [search-test] ! cbo_date );

I think what you mean is that you want a record included in the result set if the record fields contain a match to the parameters supplied. If a specific parameter is not supplied, then the corresponding field should always be considered a positive match.

That is, if no parameters were supplied, the entire table should be returned.

As for a solution, just a guess (I'm not an MS Access expert, is this for MS Access?), but can you use the iif function to force a match instead? Again, I'm not sure about the syntax, but the underlying logic should work.

Change your WHERE clause to

WHERE

(IIF(forms![search-test]!username is not null, forms![search-test]!username, help.cont_no) = help.cont_no

AND

(IIF(forms![search-test]!cont_no is not null, forms![search-test]!cont_no, help.username) = help.username

AND

(IIF(forms![search-test]!cbo_date is not null, forms![search-test]!cbo_date, help.dbo_date) = help.dbo_date

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