查询有四个条件:
厂家,状态,开始日期,结束日期
'--------------------------------------
strSql1 = "SELECT * FROM tbl_表"
If isnull(Me.厂家) and isnull(Me.状态)and isnull(Me.开始日期) and isnull(Me.结束日期)then
strSql1 = strSql1 & ";"
end if
If isnull(Me.厂家) and isnull(Me.状态)and isnull(Me.开始日期) and not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE 日期 =" & Me. 结束日期& ";"
end if
If isnull(Me.厂家) and isnull(Me.状态)and not isnull(Me.开始日期) and isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE 日期 =" & Me. 开始日期& ";"
end if
If isnull(Me.厂家) and not isnull(Me.状态)and isnull(Me.开始日期) and isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE Flag=" & Me.状态 & ";"
end if
If not isnull(Me.厂家) and isnull(Me.状态)and isnull(Me.开始日期) and isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE厂家=" & Me. 厂家& ";"
end if
If isnull(Me.厂家) and isnull(Me.状态)and not isnull(Me.开始日期) and not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期 & "# And #"& Me. 结束日期 &"#)"
end if
If isnull(Me.厂家) and isnull(Me.状态)and not isnull(Me.开始日期) and not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期 & "# And #"& Me. 结束日期 &"#)"
end if
If isnull(Me.厂家) and not isnull(Me.状态)and not isnull(Me.开始日期) and not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期 & "# And #"& Me. 结束日期 &"# Flag=" & Me.状态 & ";"
end if
If not isnull(Me.厂家) and not isnull(Me.状态)and not isnull(Me.开始日期) and not isnull(Me.结束日期)then
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期 & "# And #"& Me. 结束日期 &"# )and Flag=" & Me.状态 & " and 厂家=" & Me. 厂家& ";"
end if
if not isnull(Me.厂家) and not isnull(Me.状态)and isnull(Me.开始日期)and isnull(Me.结束日期)then
strSql1 & " WHERE Flag=” & Me.状态 & “and 厂家 =" & Me.厂家 & ";"
end if
'------------------------------------------------------------------
现在头都搞大了——稀里糊涂了
也不知到这种判断对不对
请求高手优化一下
if isnull(Me.厂家) then
If isnull(Me.状态)then
If isnull(Me.开始日期)then
If isnull(Me.结束日期)then
strSql1 = strSql1 & ";"
else
strSql1 = strSql1& " WHERE(日期 Between #"& Me. 开始日期 & "# And #"& Me. 结束日期 &"# )and Flag=" & Me.状态 & " and 厂家=" & Me. 厂家& ";"
end if
另弱弱的问一句老汉的SELECT * FROM tbl_表 where True
是否源自select * from table where 1=1
引自网络
'----------------------------------------------------------------------------------------------------------
where 1=1 的总结
为方便构造动态的多条件之不确定因素的复杂的正确的查询语句所采取的一种“江湖手段”。
此类方法,一般,在书籍上不常见,而在实际的应用中,人们得从现实角度考虑,即要保证能满足多条件查询、同时还要能应付不确定因素的灵活性,最后还要保证语句不出现任何语法错误。
该方法不失为一种好方法;但是,却由于这 where 1=1 不知道让多少新手,琢磨了多少次,始终不得要领,同时,还有可能会误导新手误入歧途;
'------------------------------------------------------------