'******************* กรณีมีครบทุกเงื่อนไข...................
sql ="SELECT * FROM ตาราง " & _
"WHERE (([ประเภทรถ] ='" & a1 & "') AND ([ยี่ห้อ] = '" & a2 & "') AND ([วันที่] between #" & a3 & "# and #" & a4 & "#) AND ([ราคา] between " & a5 & " and " & a6 & "))"
'************************ กรณีขาดไปเงื่อนไขใดเงื่อนไขหนึ่ง **********************
function WhereNot(sq)
if instr(1,lcase(sq),"where")>1 then
WhereNot =sq & ") AND ("
else
WhereNot = sq & " WHERE ("
end if
sql = "SELECT * FROM ตาราง"
if not isnull(a1) then sql = WhereNot(sql) & "[ประเภทรถ] ='" & a1 & "'
if not isnull(a2) then sql = WhereNot(sql) & "[ยี่ห้อ] ='" & a2 & "'
if not isnull(a3) then
sql = WhereNot(sql) & [วันที่] between #" & a3 & "# and #"
if a4 <a3 or isnull(a4) then a4=a3
sql = sql & a4 & "#"
end if
if not isnull(a5) then
sql = WhereNot(sql) & [วันที่] between " & a5 & " and "
if a6 <a5 or isnull(a6) then a6=a5
sql = sql & a6 & "#"
end if
if instr(1,lcase(sq),"where")>1 then sql = sql & ")"
Function WhereNot(sq)
if instr(1,lcase(sq),"where")>1 then
WhereNot =sq & ") AND ("
else
WhereNot = sq & " WHERE ("
end if
End Function
sql = "SELECT * FROM ตาราง"
if not isnull(a1) then sql = WhereNot(sql) & "[ประเภทรถ] ='" & a1 & "'
if not isnull(a2) then sql = WhereNot(sql) & "[ยี่ห้อ] ='" & a2 & "'
if not isnull(a3) then
sql = WhereNot(sql) & [วันที่] between #" & a3 & "# and #"
if a4 <a3 or isnull(a4) then a4=a3
sql = sql & a4 & "#"
end if
if not isnull(a5) then
sql = WhereNot(sql) & [วันที่] between " & a5 & " and "
if a6 <a5 or isnull(a6) then a6=a5
sql = sql & a6
end if
if instr(1,lcase(sq),"where")>1 then sql = sql & ")"