是这样的,我的数据库有比较多的字段,为实现同时多条件的动态查询,我做了多个combobox用来当做选择查询的条件,如果3-4个还好,可以用 if... then 解决,combobox多了就麻烦了,请问如何解决。

解决方案 »

  1.   

    dim tmp1 as string
    dim tmp2 as string
     ...
    dim strsql as stringif trim(combobox1.text)="" then
        tmp1=""
    else
        tmp1=" and 字段1='"& combobox1.text &"'"
    end if
    if trim(combobox2.text)="" then
        tmp2=""
    else
        tmp2=" and 字段2='"& combobox2.text &"'"
    end if
    ...'构造SQL语句 
    strsql=" select * from tablename where 1=1 " & tmp1 & tmp2 
    if rs.state=adstateopen then rs.close
    rs.open strsql,cn,adopenkeyset,adlockreadonly
      

  2.   

    dim sqlstr as string
    sqlstr="select * from table where 1=1"if combo1.text<>"" then
       sqlstr=sqlstr+" and columnname='"&combo1.text&"'"
    endifif combo2.text<>"" then
       sqlstr=sqlstr+" and columnname='"&combo2.text&"'"
    endif...............................
    要不就是用控件数组
    case 1
    sqlstr=sqlstr+" and columnname='"&combo1.text&"'"case 2
    sqlstr=sqlstr+" and columnname='"&combo2.text&"'"
      

  3.   

    谢谢,不过我的意思不是全部的条件我都要选,而是选个别的(几个以上的),主要问题就是SQL里有一个"AND" 比较难处理,请教请教.
      

  4.   

    DIM  bAndNeed  AS BOOLEANIf Trim(A.Text) <> "" Then
        StrSql = IIf(bAndNeed = True, StrSql & " AND ", StrSql & " WHERE ")
        StrSql = StrSql & " s_kigen_cd ='" & A.TEXT & "'"
        bAndNeed = True
    End If
            
      

  5.   

    还是不行,如果我只选最后一个条件就出现SQL的语法错误,SQL中多了一个"And".
      

  6.   

    用控件控件数组处理吧!
    你参考一下我在这个贴子中的回复,应该对你的问题有帮助
    http://expert.csdn.net/Expert/topic/1653/1653028.xml?temp=.7921106
      

  7.   


    谢谢上边的师傅,但我不是说要全部的条件都要!比如说我只要第一、第四呢?这样不就多了一个“and" 了吗?(我的条件是装在datacombobox里的,有好多个),查全部的条件当然没问题,但如果要挑选个别的就不知道如何写了。请教各位高手。
      

  8.   

    先置一临时变量dim StrSql as string
    StrSql = StrSql & IIf(StrSql="", & " where ", " and  ") & 条件    
      

  9.   

    你仔细看一下我在
    http://expert.csdn.net/Expert/topic/1653/1653028.xml?temp=.7921106
    发表的东西,它是不会存在你所说的多一个and的问题的
      

  10.   

    为什么不这样写
    if trim(combobox1.text)="" then
        tmp1="1=1"
    else
        tmp1=" and 字段1='"& combobox1.text &"'"
    end ifif trim(combobox2.text)="" then
        tmp2="1-1"
    else
        tmp2=" and 字段2='"& combobox2.text &"'"
    end if...如果条件某个条件没选就变成了... And 1=1 and ...  ,问题就过了
      

  11.   

    sSql= "Select * From Table Where cKey='??'
    if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'"
    if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'"
    if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'"
    if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'"
    关键是  Where cKey='??' 必须要添一个字段 cKey='??' 比如说 某个关键   cKey is Not Null  不过这样可能会影响速度
    完全满足你的要求
     
      

  12.   

    sSql= "Select * From Table Where 1=1"
    if 条件1 then sSql=sSql + " And 字段1='" & combo1.text & "'"
    if 条件2 then sSql=sSql + " And 字段2='" & combo1.text & "'"
    if 条件3 then sSql=sSql + " And 字段3='" & combo1.text & "'"
    if 条件4 then sSql=sSql + " And 字段4='" & combo1.text & "'"