str="select * from table where 1=1 "if text1.text <>"" then
str=str & " and id='" & text1.text & "' "
end ifif text2.text <>"" then
str=str & " and custcode='" & text2.text & "' "
end ifif text3.text <>"" then
str=str & " and date='" & text3.text & "' "
end if

解决方案 »

  1.   

    只要用复杂一点的if...else...肯定可以办到
      

  2.   

     If rs(Trim("流水号")) Like "*" & Trim(Txt1.Text) & "*" Then
     If rs(Trim("客户名")) Like "*" & Trim(Txt2.Text) & "*" Then
     If rs(Trim("日期")) Like "*" & Trim(Txt3.Text) & "*" Then
        With ListViewQuery
                 With ListViewQuery.ListItems.Add(, , 流水号)
                      If Not rs("流水号") = "" Then
                         .SubItems(1) = rs("流水号")
                      End If
                      If Not rs("客户名") = "" Then
                         .SubItems(2) = rs("客户名")
                       End If
                      If Not rs("日期") = "" Then
                        .SubItems(3) = rs("日期")
                      End If   
            End With
        rs.MoveNext
      End With
    End If
    End If
    End If
                             
      

  3.   

    各位,实际上我的查询条件不只上面三种,如果有9种,那按照第二位大哥的说法,我的sql
    要写,C1/9+C2/9+...+C9/9种,那不是很惨!当然我知道他的写法还是很先进的.
      

  4.   

    把查询条件作为一个查询字符串,给多少条件加多少段条件字符串,每段条件字符串前面加一个 and ,集中完毕后把查询字符串左边的 and 去掉,并加在 where 后面。一次查找。
      

  5.   

    同意楼上的,不过其实还可以where 1=1 然后加就行了嘛,就不用去掉第一个and了
      

  6.   

    注意:
    我的想法是我能随意的组合.即用户想查询1和2,就只需填1和2的条件
    查询2和3,就只需填2和3的条件,查询1,2和3,就只需填1,2和3的条件...
    (假设查询结果放在listview中)
    假设我的查询条件不只上面三种,如果有9种,那按照常规的的说法,我的sql
    要写,C1/9+C2/9+...+C9/9次.
    对于ldf先生的写法,我想他仍没有理解我的意思.
    我相信这是一个不会很难的问题,请大家给我指导一下,我真的很感谢!
      

  7.   

    select * from table where (1=@1 or 1='') and (2=@2 or 2='') and (3=@3 or 3='')
      

  8.   

    To: T2,我不明白你的1=@1是什么意思?如果说1=""(即text1.text="")表示该处没有取的话,1=@1应该是该处有取值是不是应该写成text1.text=@(text1.text)呢,请指教?真的好希望你再次回答我!
    To: ozw(沧浪客,你的主意很不错,能给我一个例子吗?谢谢 
      

  9.   

    你去看VB自带的例子VISDATA中的那个查询功能是怎么写的,看完就明白了。
      

  10.   

    Private Sub Option1_Click()
        If Option1.Value Then
            Text1.Enabled = True
            Text2.Enabled = True
            Text3.Enabled = False
        End If
    End SubPrivate Sub Option2_Click()
        If Option2.Value Then
            Text1.Enabled = False
            Text2.Enabled = True
            Text3.Enabled = True
        End If
    End SubPrivate Sub Option3_Click()
        If Option3.Value Then
            Text1.Enabled = True
            Text2.Enabled = True
            Text3.Enabled = True
        End If
    End Sub
    Private Sub Command1_Click()
        Dim strSQL As String
        Dim condition As String
        If Option1.Value Then
            condition = "流水号='" & Text1.Text & "' and 客户号='" & Text2.Text & "'"
        ElseIf Option2.Value Then
            condition = "客户号='" & Text2.Text & "' and 日期='" & Text3.Text & "'"
        ElseIf Option3.Value Then
            condition = "流水号='" & Text1.Text & "' and 客户号='" & Text2.Text & "' and 日期='" & Text3.Text & "'"
        End If
        strSQL = "select * from Table where " & condition
    End Sub
      

  11.   

    if len(text1.text)>0 then
        findstring=findstring+"var1 = "+ var1
    end if
    if len(text2.text)>0 then
       findstring=findstring+"var2 = "+ var2
    end if
    if len(text3.text)>0 then
       findstring=findstring+"var3 = "+ var3
    end if
    ......
    sqlstring + findstring是不是这样?
      

  12.   

    这样不行吗:
    str="select * from xx where 1=1"
    if text1.text="" then  
       str=str+" and xx like '%'"
    else
       str=str+" and xx like '"+trim(text1.text)+"'"
    end if
    if text2.text="" then  
       str=str+" and xx like '%'"
    else
       str=str+" and xx like '"+trim(text2.text)+"'"
    end if
    if text3.text="" then  
       str=str+" and xx like '%'"
    else
       str=str+" and xx like '"+trim(text3.text)+"'"
    end if
      

  13.   

    TO: cqq_chen(我是谁)    VISDATA那个例子,我觉得操作性太差,用户使用较麻烦,我想用一个比较平面的例子来解决!
      

  14.   

    回复人: painus(惭愧) (2001-9-27 13:24:29)  得0分 
    能不能给我一个例子  
    回复人: progame(呵呵呵(傻笑中) (2001-9-27 13:28:40)  得0分 
    str="select * from table where 1=1 "if text1.text <>"" then
    str=str & " and id='" & text1.text & "' "
    end ifif text2.text <>"" then
    str=str & " and custcode='" & text2.text & "' "
    end ifif text3.text <>"" then
    str=str & " and date='" & text3.text & "' "
    end if 
    回复人: anthyzhang(冰人) (2001-9-27 13:36:01)  得0分 
    答对了给分吗? 如果是的话, 我考虑给你一个例子, 不只是三个条件噢!  
    回复人: murphyxiao(头文字) (2001-9-27 13:37:23)  得0分 
    只要用复杂一点的if...else...肯定可以办到  
    回复人: mellow(阿呆) (2001-9-27 13:39:08)  得0分 
    If rs(Trim("流水号")) Like "*" & Trim(Txt1.Text) & "*" Then
    If rs(Trim("客户名")) Like "*" & Trim(Txt2.Text) & "*" Then
    If rs(Trim("日期")) Like "*" & Trim(Txt3.Text) & "*" Then
        With ListViewQuery
                With ListViewQuery.ListItems.Add(, , 流水号)
                      If Not rs("流水号") = "" Then
                        .SubItems(1) = rs("流水号")
                      End If
                      If Not rs("客户名") = "" Then
                        .SubItems(2) = rs("客户名")
                      End If
                      If Not rs("日期") = "" Then
                        .SubItems(3) = rs("日期")
                      End If  
            End With
        rs.MoveNext
      End With
    End If
    End If
    End If
                             
    回复人: painus(惭愧) (2001-9-27 13:44:41)  得0分 
    肯定有分,我是很愿意跟大家交朋友的!  
    回复人: painus(惭愧) (2001-9-27 14:11:17)  得0分 
    我的E_mail是[email protected]  
    回复人: painus(惭愧) (2001-9-27 20:25:08)  得0分 
    各位,实际上我的查询条件不只上面三种,如果有9种,那按照第二位大哥的说法,我的sql
    要写,C1/9+C2/9+...+C9/9种,那不是很惨!当然我知道他的写法还是很先进的.  
    回复人: kelly_leecn(kelly) (2001-9-28 9:12:54)  得0分 
    up  
    回复人: lou_df(ldf) (2001-9-28 10:27:39)  得0分 
    把查询条件作为一个查询字符串,给多少条件加多少段条件字符串,每段条件字符串前面加一个 and ,集中完毕后把查询字符串左边的 and 去掉,并加在 where 后面。一次查找。  
    回复人: wolfw(阿干) (2001-9-28 10:33:16)  得0分 
    同意楼上的,不过其实还可以where 1=1 然后加就行了嘛,就不用去掉第一个and了  
    回复人: painus(惭愧) (2001-9-28 10:44:25)  得0分 
    能不能给我看一个例子,我都想了一个星期了!  
    回复人: Geoson(快乐的浪子) (2001-9-28 11:22:23)  得0分 
    小问题,不过,太麻烦了  
    回复人: painus(惭愧) (2001-9-28 11:30:39)  得0分 
    注意:
    我的想法是我能随意的组合.即用户想查询1和2,就只需填1和2的条件
    查询2和3,就只需填2和3的条件,查询1,2和3,就只需填1,2和3的条件...
    (假设查询结果放在listview中)
    假设我的查询条件不只上面三种,如果有9种,那按照常规的的说法,我的sql
    要写,C1/9+C2/9+...+C9/9次.
    对于ldf先生的写法,我想他仍没有理解我的意思.
    我相信这是一个不会很难的问题,请大家给我指导一下,我真的很感谢!  
    回复人: painus(惭愧) (2001-9-28 13:06:17)  得0分 
    那位帮我?  
    回复人: T2(無藥可救) (2001-9-28 13:10:30)  得0分 
    select * from table where (1=@1 or 1='') and (2=@2 or 2='') and (3=@3 or 3='')  
    回复人: ozw(沧浪客) (2001-9-28 13:47:11)  得0分 
    用单选钮选择查询方式,然后激活相应的输入文本框就行了  
    回复人: painus(惭愧) (2001-9-28 14:06:06)  得0分 
    To: T2,我不明白你的1=@1是什么意思?如果说1=""(即text1.text="")表示该处没有取的话,1=@1应该是该处有取值是不是应该写成text1.text=@(text1.text)呢,请指教?真的好希望你再次回答我!
    To: ozw(沧浪客,你的主意很不错,能给我一个例子吗?谢谢   
    回复人: cqq_chen(我是谁) (2001-9-28 14:29:58)  得0分 
    你去看VB自带的例子VISDATA中的那个查询功能是怎么写的,看完就明白了。  
    回复人: ozw(沧浪客) (2001-9-28 14:36:13)  得0分 
    Private Sub Option1_Click()
        If Option1.Value Then
            Text1.Enabled = True
            Text2.Enabled = True
            Text3.Enabled = False
        End If
    End SubPrivate Sub Option2_Click()
        If Option2.Value Then
            Text1.Enabled = False
            Text2.Enabled = True
            Text3.Enabled = True
        End If
    End SubPrivate Sub Option3_Click()
        If Option3.Value Then
            Text1.Enabled = True
            Text2.Enabled = True
            Text3.Enabled = True
        End If
    End Sub
    Private Sub Command1_Click()
        Dim strSQL As String
        Dim condition As String
        If Option1.Value Then
            condition = "流水号='" & Text1.Text & "' and 客户号='" & Text2.Text & "'"
        ElseIf Option2.Value Then
            condition = "客户号='" & Text2.Text & "' and 日期='" & Text3.Text & "'"
        ElseIf Option3.Value Then
            condition = "流水号='" & Text1.Text & "' and 客户号='" & Text2.Text & "' and 日期='" & Text3.Text & "'"
        End If
        strSQL = "select * from Table where " & condition
    End Sub
     
    回复人: qhdsfh(飞翔的鹰) (2001-9-28 14:36:55)  得0分 
    有例子了吗?  
    回复人: lou_df(ldf) (2001-9-28 14:45:18)  得0分 
    if len(text1.text)>0 then
        findstring=findstring+"var1 = "+ var1
    end if
    if len(text2.text)>0 then
      findstring=findstring+"var2 = "+ var2
    end if
    if len(text3.text)>0 then
      findstring=findstring+"var3 = "+ var3
    end if
    ......
    sqlstring + findstring是不是这样?  
    回复人: crope() (2001-9-28 14:48:48)  得0分 
    这样不行吗:
    str="select * from xx where 1=1"
    if text1.text="" then  
      str=str+" and xx like '%'"
    else
      str=str+" and xx like '"+trim(text1.text)+"'"
    end if
    if text2.text="" then  
      str=str+" and xx like '%'"
    else
      str=str+" and xx like '"+trim(text2.text)+"'"
    end if
    if text3.text="" then  
      str=str+" and xx like '%'"
    else
      str=str+" and xx like '"+trim(text3.text)+"'"
    end if
     
    回复人: painus(惭愧) (2001-9-29 15:04:48)  得0分 
    TO: cqq_chen(我是谁)    VISDATA那个例子,我觉得操作性太差,用户使用较麻烦,我想用一个比较平面的例子来解决!  
      

  15.   

    思路:其实就是一个字符串操作的问题
    1.dim一个string array,与query条件的checkbox对应
    2.用户选取checkbox让他输入条件,存入与之对应的array item
    3.dim MySQLString as string,判断string array 的每一项,根据你的要求用" and "或者" or "连接,最后把MySQLString 的未尾" and " (" or ")去掉,大功告成。
      

  16.   

    把所有的条件放在一个数组里,遍历一遍,拼出SQL