Private Sub Command1_Click()
    
    Dim str     As String
    
    str = vbNullString
    
    If Check1.Value = vbChecked Then
        str = "field1=" & Text1.Text
    End If
    
    If Check2.Value = vbChecked Then
        If Len(str) > 0 Then
            str = " and field2=" & Text2.Text
        Else
            str = "field2=" & Text2.Text
        End If
    End If
    
    If Check3.Value = vbChecked Then
        If Len(str) > 0 Then
            str = " and field3=" & Text3.Text
        Else
            str = "field3=" & Text3.Text
        End If
    End If
    
    str = "select * from mytable where " & str
    '...
    
End Sub

解决方案 »

  1.   

    楼上正解
    稍微改下呵呵 
    Private Sub Command1_Click()
        
        Dim str     As String
        
        str = vbNullString
        
        If Check1.Value = vbChecked Then
            str = "field1=" & Text1.Text
        End If
        
        If Check2.Value = vbChecked Then
          
                str = " and field2=" & Text2.Text    End If
        
        If Check3.Value = vbChecked Then
       
                str = " and field3=" & Text3.Text    End If
        
        str = "select * from mytable where 1=1 " & str
        '...
        
    End Sub
      

  2.   

    楼上正解
    稍微改下呵呵 
    Private Sub Command1_Click()
        
        Dim str     As String
        
        str = vbNullString
        
        If Check1.Value = vbChecked Then
            str = " and field1=" & Text1.Text
        End If
        
        If Check2.Value = vbChecked Then
          
                str = " and field2=" & Text2.Text    End If
        
        If Check3.Value = vbChecked Then
       
                str = " and field3=" & Text3.Text    End If
        
        str = "select * from mytable where 1=1 " & str
        '...
        
    End Sub
      

  3.   

    再改一下。
    Private Sub Command1_Click()
        
        Dim str     As String
        
        str = vbNullString
        
        If Check1.Value = vbChecked Then
            str = " and field1=" & Text1.Text
        End If
        
        If Check2.Value = vbChecked Then
            str = str & " and field2=" & Text2.Text
        End If
        
        If Check3.Value = vbChecked Then
            str = str & " and field3=" & Text3.Text
        End If
        
        str = "select * from mytable where 1=1 " & str
        '...
        
    End Sub
      

  4.   

    str = vbNullString
    ========================================
    为什么不是str=""  ?
    我认为vbNullString不是这么用的
    MSDN:
    vbNullString 值为 0 的字符串 用来调用外部过程;与长度为零的字符串 ("") 不同
      

  5.   

    关于 vbNullString 跟 "" 以及 vbNullChar 的区别网上资料很多,找点看看就明白了。