设置一个查询窗体如下:
□编号:□‘textbox
□名称:□'textbox
□地址:□,textbox
□性质:□,combobox
...............
............前面是check,后面是textbox,我的问题是,当前面的check选中后,在后面的textbox 输入查询条件,可以任意组合查询条件,该怎么写SQL语句,不知道下面这样写为什么没有用呢。if check1.value=1 or check2.value=1 or check3.value=1 then
   rs.open"select * from table where 编号='"+text1.text+"'and 名称='"+text2.text+"' and 地址='"+text3.text+"' and 性质='"+combo1.text+"'"
endif
为什么查询不出来呢。是不是哪里有错呀,当要任意组合查询时,该怎么办呢。

解决方案 »

  1.   

    dim strsql as string
     strsql="select * from table where 编号='" & text1.text &"'"
    if check2.value=1 then
      strsql=strsql+"and 名称='"&text2.text &"'"
    elseif check3.value=1 then
      strsql=strsql+"and 地址='"&text3.text &"'"
    elseif combo1.text<>empty then
      strsql=strsql+"and 性质='" & combo1.text &"'"
    endif
    rs.open strsql,连接,游标类型,锁类型
      

  2.   

    //*========================================
    if check2.value=1 then
      strsql=strsql+"and 名称='"&text2.text &"'"
    elseif check3.value=1 then
      strsql=strsql+"and 地址='"&text3.text &"'"
    elseif combo1.text<>empty then
      strsql=strsql+"and 性质='" & combo1.text &"'"
    endif
    ==========================*//每次只能选择一个来组合,不能实现楼主的要求的修改为:
    dim strsql as string
     strsql="select * from table where "if check1.value=1 then
      strsql=strsql & "编号='" & text1.text &"'"
    end ifif check2.value=1 then
      strsql=strsql & " and 名称='"&text2.text &"'"
    end ifif check3.value=1 then
      strsql=strsql & " and 地址='"&text3.text &"'"
    end ifif combo1.text<>"" then
      strsql=strsql & " and 性质='" & combo1.text &"'"
    endifrs.open strsql,3,3
      

  3.   

    用  like  语句 一切都可以解决(执行效率低了点)
      

  4.   

    回qiqunet(瑞旗·广东) 的问题:
      你这样写,如果我第一次没有选择编号呢,比如说我第一次选名称,那SQLSTRING不用成了"and 名称=''& text2.text &''"嘛,那rs.open sqlstring,不就出错了吗-----------------------那倒底该怎么解决呢,可惜我没分。。大家帮忙吧
      

  5.   

    你可以在SQL语句前面加上1=1,然后再按qiqunet(瑞旗·广东)方法,就没有你所说的问题了
      

  6.   

    strsql="select * from table where 1=1 "
    然后再按qiqunet(瑞旗·广东)方法,就没有你所说的问题了
      

  7.   

    可以这样:dim strsql as string
     strsql="select * from table where "if check1.value=1 then  strsql=strsql & "编号='" & text1.text &"'"if check2.value=1 then  strsql=strsql & " and 名称='"&text2.text &"'"if check3.value=1 then  strsql=strsql & " and 地址='"&text3.text &"'"if combo1.text<>"" then  strsql=strsql & " and 性质='" & combo1.text &"'"'下面这三种方法,可根据你的需要选其一
    if strsql<>"select * from table where " then rs.open strsql,3,3'或者换成(选择所有数据)
    if strsql="select * from table where " then strsql="select * from table where 1=1"
    rs.open strsql,3,3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
    if strsql="select * from table where " then strsql="select * from table where 1=0"
    rs.open strsql,3,3
      

  8.   

    Dim strsql As String, LenStr As Integer
     strsql = "select * from table where"
    LenStr = Len(strsql)
    If check1.Value = 1 Then strsql = strsql & " 编号='" & text1.Text & "'"
    If check2.Value = 1 Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 名称='" & text2.Text & "'"
        Else
            strsql = strsql & " 名称='" & text2.Text & "'"
        End If
    End If
    If check2.Value = 1 Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 地址='" & text3.Text & "'"
        Else
            strsql = strsql & " 地址='" & text3.Text & "'"
        End If
    End If
    If check3.Value = 1 Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 性质='" & combo1.Text & "'"
        Else
            strsql = strsql & " 性质='" & combo1.Text & "'"
        End If
    End If
    If combo1.Text <> "" Then
        If Len(strsql) = LenStr Then
            strsql = strsql & " and 名称='" & text2.Text & "'"
        Else
            strsql = strsql & " 名称='" & text2.Text & "'"
        End If
    End If'下面这三种方法,可根据你的需要选其一'如果用户没有选择就执行这段代码,那么就不做任何操作或结合“Msgbox”告诉用户要选择一个条件才能查询。
    If Len(strsql) <> LenStr Then rs.open strsql, 3, 3'或者换成(选择所有数据)
    If Len(strsql) = LenStr Then strsql = "select * from table where 1=1"
    rs.open strsql, 3, 3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
    If Len(strsql) = LenStr Then strsql = "select * from table where 1=0"
    rs.open strsql, 3, 3
      

  9.   


    '上面又错了,多了一个并且错了位!Dim strsql As String, LenStr As Integer
     strsql = "select * from table where"
    LenStr = Len(strsql)
    If check1.Value = 1  and text1<>"" Then strsql = strsql & " 编号='" & text1.Text & "'"
    If check2.Value = 1  and text2<>"" Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 名称='" & text2.Text & "'"
        Else
            strsql = strsql & " 名称='" & text2.Text & "'"
        End If
    End If
    If check3.Value = 1 and text3<>"" Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 地址='" & text3.Text & "'"
        Else
            strsql = strsql & " 地址='" & text3.Text & "'"
        End If
    End If
    If combo1.Text <> "" Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 性质='" & combo1.Text & "'"
        Else
            strsql = strsql & " 性质='" & combo1.Text & "'"
        End If
    End If'下面这三种方法,可根据你的需要选其一'如果用户没有选择就执行这段代码,那么就不做任何操作或结合“Msgbox”告诉用户要选择一个条件才能查询。
    If Len(strsql) <> LenStr Then rs.open strsql, 3, 3'或者换成(选择所有数据)
    If Len(strsql) = LenStr Then strsql = "select * from table where 1=1"
    rs.open strsql, 3, 3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
    If Len(strsql) = LenStr Then strsql = "select * from table where 1=0"
    rs.open strsql, 3, 3
      

  10.   

    1=1,1=0,是什么意思呀lenstr 是什么??
      

  11.   

    建议你分类时把使用text控件的放在一起,把使用combobox控件的放在一起。
    然后使用控件数组。让check也成为数组,从上至下依次排列。
    然后:
    dim x as integer      '循环变量
    dim sel as string          '保存select语句
    sel="select * from xxx where "    ’写出select语句头
    for x=0 to text控件数组最大下标
    if check1(x).value=1 then sel=sel & check1(x).caption & "='" & text1(x).text & "' and "         '判断是否选中,选中后添加 “字段名='值' and ”这个字符串
    next x
    for x=text控件数组最大下标+1 to check控件数组最大下标+text控件数组最大下标
    if check1(x).value=1 then sel=sel & check1(x).caption & "='" & combobox1(x).text & "' and "         '同上
    next x
    之后将sel字符串的后4个字符去掉,就是你想要的.要去掉最后添加的" and"
      

  12.   

    又是问这个。。试试:
    Private Sub Command1_Click()
    Dim first As Boolean
    first = False
    Dim sqlid, sqlname, sqladdress, sqlpro, sqlstr As StringIf Check1.Value = 1 Then
    sqlid = " where 编号='" + Trim(Text1.Text) + "'   "
    first = True
    Else
    sqlid = ""
    End If
    If Check2.Value = 1 Then
        If first = False Then
            sqlname = "  where 名称='" + Trim(Text2.Text) + "'"
            first = True
        Else
            sqlname = "  and  名称='" + Trim(Text2.Text) + "'"
        End If
    Else
    sqlname = ""
    End If
    If Check3.Value = 1 Then
        If first = False Then
            sqladdress = "  where 地址='" + Trim(Text3.Text) + "'"
            first = True
        Else
            sqladdress = "  and  地址='" + Trim(Text3.Text) + "'"
        End If
    Else
    sqladdress = ""
    End If
    If Check4.Value = 1 Then
        If first = False Then
            sqlpro = "  where 性质='" + Trim(Text4.Text) + "'"
            first = True
        Else
            sqlpro = "  and  性质='" + Trim(Text4.Text) + "'"
        End If
    Else
    sqlpro = ""
    End Ifsqlstr = "select * from table  " + sqlid + sqlname + sqladdress + sqlpro + ";  "
    Print sqlstr
    End Sub上面是一个简单的例子,可以得到四个条件的任意组合sql查询语句。。实际编程时还要检测被选中的textbox中是否有输入内容
      

  13.   

    哦,最后一个条件你用的是combo,我还是用了textbox不过原理一样。只需要把check4的代码改为下面:
    If Check4.Value = 1 Then
        If first = False Then
            sqlpro = "  where 性质='" + Trim(combo1.Text) + "'"
            first = True
        Else
            sqlpro = "  and  性质='" + Trim(combo1.Text) + "'"
        End If
    Else
    sqlpro = ""
    End If
      

  14.   


    Private Sub Command1_Click()
        Dim strsql As String
         strsql = "select * from table where"
        If check1.Value = 1 Then strsql = strsql & " 编号='" & text1.Text & "' and "
        If check2.Value = 1 Then strsql = strsql & " 名称='" & text2.Text & "' and "
        If check3.Value = 1 Then strsql = strsql & " 地址='" & text3.Text & "' and "
        If combo1.Text <> "" Then strsql = strsql & " 性质='" & combo1.Text & "' and "
        If Right(strsql, 4) = "and " Then
            strsql = Left(strsql, Len(strsql) - 5) '删除最后面的字符“ and ”
            rs.open strsql, 3, 3
        Else
            MsgBox "你没有选择任何的条件。"
        End If
    End Sub
      

  15.   

    Dim strsql As String, LenStr As Integer
     strsql = "select * from table where"
    LenStr = Len(strsql)
    If check1.Value = 1  and text1<>"" Then strsql = strsql & " 编号='" & text1.Text & "'"
    If check2.Value = 1  and text2<>"" Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 名称='" & text2.Text & "'"
        Else
            strsql = strsql & " 名称='" & text2.Text & "'"
        End If
    End If
    If check3.Value = 1 and text3<>"" Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 地址='" & text3.Text & "'"
        Else
            strsql = strsql & " 地址='" & text3.Text & "'"
        End If
    End If
    If combo1.Text <> "" Then
        If Len(strsql) <> LenStr Then
            strsql = strsql & " and 性质='" & combo1.Text & "'"
        Else
            strsql = strsql & " 性质='" & combo1.Text & "'"
        End If
    End If'下面这三种方法,可根据你的需要选其一'如果用户没有选择就执行这段代码,那么就不做任何操作或结合“Msgbox”告诉用户要选择一个条件才能查询。
    If Len(strsql) <> LenStr Then rs.open strsql, 3, 3'或者换成(选择所有数据)
    If Len(strsql) = LenStr Then strsql = "select * from table where 1=1"
    rs.open strsql, 3, 3'或者换成(什么数据也不选,不过选择这个数据的表结构形式)
    If Len(strsql) = LenStr Then strsql = "select * from table where 1=0"
    rs.open strsql, 3, 3
    =================================================================
    各位大哥,昨晚按瑞旗.广东上面的办法做了一下,以现遗漏了一个问题,当条件成立时,where和and 中间没有空格,导出SQL语法错误,因此下面的语句
    strsql = strsql & " and 性质='" & combo1.Text & "'"要更改成
    strsql = strsql &" "& " and 性质='" & combo1.Text & "'"好像才行。不过呢,问题已经解决了,谢谢各位大哥相助,可惜我分不够,只有10郁闷
      

  16.   

    可以用控件数组,然后遍历一下,若为checked,则加入查询字符串