我的界面上有一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

解决方案 »

  1.   

    dim strWhere as string
    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
      

  2.   

    up,用select case ,
       rs.filter进行筛选
     select case 0
       rs.filter=combo1.text
            case 1
       rs.filter =combo1.text
         ..........
      

  3.   

    提供一思路给你参考:dim Tmp_text1    as string
    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
      

  4.   

    sq1 = "select * from 收费 where 收费站 = '" & Combo1.Text & "%'and 收费员= '" & Combo2.Text & "%'"如果COMBO中為空,則用通配符%,查詢所有記錄
      

  5.   

    不好意思,我也想过用filter的,可惜不会,哪位可以详细一点吗?
      

  6.   

    if strWhere <>"所有收费站" then strWhere =strWhere & " and "
    可以这样加吗?还是要单引号阿.
    所有收费站是combox1的值
      

  7.   


    还有,我的conbox2是开始日期,combox3是结束日期,
    strWhere = strWhere & " 收费日期 >'" & Trim(Combo2.Text) & "'"and " 收费日期 <'" & Trim(Combo3.Text) & "'"
    我这样写可以吗?