我的窗体上有四个textbox控件,分别是text1、text2、text3、text4
一个Button控件Button1
我想在Button1_click事件中实现以下功能
判断四个textbox控件哪些非空,然后根据非空的textbox的内容生成一个SQL语句
例如:
如果判断出text1、text2非空就生成select * from [doc] where condition1=text1.text and condition2=text2.text
如果判断出text2、text3非空就生成select * from [doc] where condition2=text2.text and condition4=text4.text请问各位大侠该怎么实现?谢谢!VB6环境下

解决方案 »

  1.   


       Dim SQL As String
       SQL = ""
       If Len(Text1.Text) > 0 And Len(Text2.Text) > 0 Then
          SQL = SQL & " And condition1=" & Text1.Text & " And condition2=" & Text2.Text
       End If
       If Len(Text2.Text) > 0 And Len(Text3.Text) > 0 Then
          SQL = SQL & " And condition2=" & Text2.Text & " And condition4=" & Text4.Text
       End If
       If Len(SQL) > 0 Then
          SQL = Right(SQL, Len(SQL) - 4)
          SQL = " where" & SQL
       End If
       SQL = "select * from [doc]" & SQL
       MsgBox SQL, 64, "SQL语句"
      

  2.   


    dim tmp1 as string,tmp2 as string,tmp3 as string,tmp4 as string
    if len(text1.text)<>0 then
        tmp1=" and condition1='"& text1.text &"'"
    end ifif len(text2.text)<>0 then
        tmp2=" and condition2='"& text2.text &"'"
    end ifif len(text3.text)<>0 then
        tmp3=" and condition3='"& text3.text &"'"
    end ifif len(text4.text)<>0 then
        tmp4=" and condition4='"& text4.text &"'"
    end ifdim sql as string
    sql="select * from [doc] where 1=1" & tmp1 & tmp2 & tmp3 & tmp4
      

  3.   

    谢谢!
    但是我想实现的是判断四个textbox当中非空的情况是不一的
    比如说只出现text1非空也有可能的
      

  4.   

    '假如TEXT1和TEXT2里是数值可以:
    if len(text1)<>0 and len(text2)<>0 then
       sqlStr="select * from [doc] where condition1=" & text1.text & " and condition2=" & text2.text
    endif'假如TEXT1和TEXT2里是字符可以:
    if len(text1)<>0 and len(text2)<>0 then
       sqlStr="select * from [doc] where condition1='" & text1.text & "' and condition2='" & text2.text & "'"
    endif
      

  5.   


    dim sql as stringsql="select * from [doc] where 1=1 "if trim(text1.text)<>"" then sql=sql & " and condition1='" & Text1.Text & "'"if trim(text2.text)<>"" then sql=sql & " and condition2='" & Text2.Text & "'"if trim(text3.text)<>"" then sql=sql & " and condition3='" & Text3.Text & "'"if trim(text4.text)<>"" then sql=sql & " and condition4='" & Text4.Text & "'"
      

  6.   

    你自己的题目有问题
    你自己读一下你的问题的含义例如: 
    如果判断出text1、text2非空就生成select * from [doc] where condition1=text1.text and condition2=text2.text 
    如果判断出text2、text3非空就生成select * from [doc] where condition2=text2.text and condition4=text4