做了个表单查询,用了很多的CHECK,查询数据库时如何结合CHECK来查询,有选有不选的情况下,用的是ADO连接,该怎么来做查询语句,最好提供点代码,谢谢

解决方案 »

  1.   

    使用参数化查询。参见http://support.microsoft.com/default.aspx?scid=kb;zh-cn;301075
      

  2.   

    '把Check设置为控件数组,把数组的各个元素的 Tag属性设置为数据表中对应的列名Private Sub cmdQuery_click()
        Dim strSql As String, i As Integer
        strSql = "select "
        For i = 0 To Check.Count - 1
            If Check(i).Value = 1 Then
                strSql = strSql & Check(i).Tag & ","
            End If
        Next
        strSql = Left(strSql, Len(strSql) - 1) & " from tableName"
    End Sub
      

  3.   

    如果牵涉到几张表,查询也可能在几张表中进行,TAG属性只要设置列名的话是否要把数据库中的相关查询到的表格列名是否设置成一样?
      

  4.   

    Tag属性需要设置得跟表里的列名一样,如果涉及多个表,最好连表的名称一起写上,如下:Check(0).Tag=Table1.Col1
    Check(1).Tag=Table1.Col2Check(2).Tag=Table2.Col1
    Check(3).Tag=Table3.Col2Check(4).Tag=Table3.Col3
    Check(5).Tag=Table3.Col4......
      

  5.   

    如果第一个CHECK是选择ACCESS中某张表,第二个CHECK是根据第一个的选择来确定表中的列名,两张表中的列名是一样,这样是否可行?谢谢
      

  6.   

    我还不大明白你所说的问题
    不过,你事先设置好Tag属性,就想上面说的一样,如果用户选择Check(0),表示需要查询Table1的Col1列,如果选择Check(3),表示需要查询Table2的Col2列
      

  7.   

    根据你的思路做了以后好象错了,查询后显示的是CHECK.TAG中的数据,我是想把CHECK后的内容作为查询条件来查询
      

  8.   

    我还以为Check是要查询的列呢,原来是作为查询条件,可以这样做(可能与你的具体要求有差距,自己改过来):Private Sub Command1_Click()
        Dim i%, strCondition As String, strSql As String, arrTmp() As String, strTable As String
        
        For i = 0 To Check1.Count - 1
            If Check1(i).Value = 1 Then
                '取得查询条件====================
                If Trim(strCondition) <> "" Then
                    strCondition = strCondition & " and " & Check1(i).Tag & "=" & Text1(i).Text
                Else
                    strCondition = Check1(i).Tag & "=" & Text1(i).Text
                End If
                '===============================
                '取得要查询的表==================
                arrTmp = Split(Check1(i).Tag, ".")
                If InStr(strTable, arrTmp(0)) = 0 Then
                    strTable = strTable & "," & arrTmp(0)
                End If
                '===============================
            End If
        Next
        strTable = Mid(Trim(strTable), 2)  '去掉第一个逗号
        strSql = "select * from " & strTable & " where " & strCondition
        Debug.Print strSql
    End Sub