开发者

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 ) 
       A开发者_运维知识库ND ( 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜