我的界面上有一datagrid显示数据库中表的内容,我要实现多项查询,有多个条件,如果哪个条件为空的话,就是那一项不是查询条件了,下面试我的一些代码,希望你们能帮 我添加一些.if combo1.text为空,则.....Private Sub cmdOK_Click()
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.Open "DSN=收费系统DSN;UID=sa;PWD=2212;"
cnn.CursorLocation = adUseClient
Dim sq1 As Stringsq1 = "select * from 收费 where 收费站 = '" & Trim(Combo1.Text) & "'and 收费员= '" & Trim(Combo2.Text) ......(一些条件)"End Sub
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.Open "DSN=收费系统DSN;UID=sa;PWD=2212;"
cnn.CursorLocation = adUseClient
Dim sq1 As Stringsq1 = "select * from 收费 where 收费站 = '" & Trim(Combo1.Text) & "'and 收费员= '" & Trim(Combo2.Text) ......(一些条件)"End Sub
if Combo1.listindex<>-1 then
if strWhere <>"" then strWhere =strWhere & " and "
strWhere =strWhere & " 收费站 = '" & Trim(Combo1.Text) & "'"
end ifif Combo2.listindex<>-1 then
if strWhere <>"" then strWhere =strWhere & " and "
strWhere =strWhere & " 收费员 = '" & Trim(Combo2.Text) & "'"
end if…………………………if strWhere<>"" then sql=sql & " where " & strWhere
rs.filter进行筛选
select case 0
rs.filter=combo1.text
case 1
rs.filter =combo1.text
..........
dim Tmp_text2 as string
dim Tmp_text3 as string
...
if trim(text1.text)="" then
tmp_text1=""
else
tmp_text1=" and 字段名='"& trim(text1.text) &"'"
end if
if trim(text2.text)="" then
tmp_text2=""
else
tmp_text2=" and 字段名='"& trim(text2.text) &"'"
end if
if trim(text3.text)="" then
tmp_text3=""
else
tmp_text3=" and 字段名='"& trim(text3.text) &"'"
end if
...'SQL语句
strsql=" select * from tablename where 1=1 " & tmp_text1 & tmp_text2 & tmp_text3
可以这样加吗?还是要单引号阿.
所有收费站是combox1的值
还有,我的conbox2是开始日期,combox3是结束日期,
strWhere = strWhere & " 收费日期 >'" & Trim(Combo2.Text) & "'"and " 收费日期 <'" & Trim(Combo3.Text) & "'"
我这样写可以吗?