有一个查询数据库的操作,假设为
select a,b,c from table
但是有6-7个选项条件(可以全部不选,也可多选、全选),要求根据用户的选择来查询,难道只能用穷举法来写select语句吗,有没有什么好的方法

解决方案 »

  1.   

    sql="select a,b,c from table where 1=1"if 条件1 then
    sql=sql & " and "
    endifif 条件2 then
    sql=sql & " and "
    endif
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 
    http://feiyun0112.cnblogs.com/
      

  2.   

    楼主的意思可能是他查询的字段有很多,比如有A,B,C,D,E,F,G等让客户自定义查哪几个字段的用checkbox做个数组,每个CHECKBOX的名字就是字段的名字
    Private Sub Command2_Click()
    Dim strSql As String
    Dim i As Integer
    For i = 0 To 2
        If Check1(i).Value = 1 Then
            strSql = strSql & Check1(i).Caption & ","
        End If
    Next
    strSql = Mid(strSql, 1, Len(strSql) - 1)
    End Subsql="select " & strSql  & " from TableName"