我最近做了个查询,不知道对你有没有用,查询作成模块的话,不好传参数,最好根据需求做一个好的界面,再根据界面做查询代码,不同类型字段在sql语句里的表达不一样,所以最后提交的sql语句要金国处理,我没有用中间表查询,用的是ACCESS数据库,如果我搞清楚的ADO连ACCESS代码如下:Dim sTemp As String
Dim s1 As Variant
Dim OrAnd As String
'On Error GoTo Err
cmdprint.Enabled = True
'用ADO建立数据连接
If Combo1.Text = "" Or Combo2.Text = "" Then
    MsgBox "请先选择要查询的库和表", , "警告"
    Exit Sub
End If
If rs.State = adStateOpen Or db.State = adStateConnecting Then
    rs.Close
    db.Close
End If
db.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=" _
& App.Path & sSetDBname & ";"
db.Open'设置SQL查询串
'如果字段值为空,就默认为查找出所选库中所有的记录
If Text1.Text = "" Then
    rsSql = "select * from " & sTable
    Querytable = Combo2.Text
    ShowData
    Exit Sub
End If'字段值不为空时
rsSql = "select * from " & sTable & " where"
s1 = Trim(Text1.Text)
sChoice1 = " " & Combo3.Text & logic4 & "'" & s1 & "'"
If Right$(Combo3.Text, 2) <> "时效" Or Right$(Combo3.Text, 2) <> "日期" Or Right$(Combo3.Text, 2) <> "利息" Or Right$(Combo3.Text, 2) <> "金额" Or Right$(Combo3.Text, 2) <> "利率" Then
    If logic4 = "=" Then logic4 = "like"
    sChoice1 = " " & Combo3.Text & " " & logic4 & " '" & s1 & "%'"
    'MsgBox sChoice1
End If
Select Case Right$(Combo3.Text, 2)
    Case "时效", "日期"
        s1 = Text1.Text
        If logic4 = "like" Then logic4 = "="
        sChoice1 = " " & Combo3.Text & logic4 & "#" & s1 & "#"
    Case "本金", "利息", "金额", "利率"
        s1 = Text1.Text
        If logic4 = "like" Then logic4 = "="
        sChoice1 = " " & Combo3.Text & logic4 & s1
End Select
'一次查询
If Option1.Value = True Then
    rsSql = rsSql & sChoice1
    'MsgBox rsSql, vbExclamation, "一次性查询"
    Querytable = Combo2.Text
    ShowData
    'Combo1.Text = ""
    Combo2.Clear
    Combo3.Clear
    Combo4.Text = "等于"
    Text1.Text = ""
Exit Sub
End If'多次查询
If Option2.Value = True Then
    If (Option3.Value = False) And (Option4.Value = False) Then
    trsSql = rsSql & sChoice1
    End If
    If Option3.Value = True Then
    OrAnd = " and"
    trsSql = trsSql & OrAnd & sChoice1
    End If
    
    If Option4.Value = True Then
    OrAnd = " or"
    trsSql = trsSql & OrAnd & sChoice1
    End If
    
    rsSql = trsSql
    'MsgBox rsSql, vbExclamation, "多次查询的SQL字符串"
    ShowData
    Option3.Enabled = True
    Option4.Enabled = True
End IfExit Sub
'Err:
'    ShowErr

解决方案 »

  1.   

    If List2.ListCount = 0 Then
      MsgBox "请选择查询条件!", 0 + 48, "查询提示"
    Else
    Dim Sqlstr As String
    Dim Str1 As String: Dim i As Integer
    For i = 0 To List2.ListCount - 1
     List2.ListIndex = i
     Str1 = Str1 & " " & List2.Text
    Next i
     Sqlstr = "select * from sq_hjb where" & " " & Str1
    Adodc1.CommandType = adCmdText
    Adodc1.RecordSource = Sqlstr
    Set DataGrid1.DataSource = Adodc1
    Adodc1.Refresh
    DataGrid1.ReBind
    Label2.Caption = Adodc1.Recordset.RecordCount
     If Adodc1.Recordset.RecordCount > 0 Then
        Label3.Visible = True
        Label2.Visible = True
        DataGrid1.Visible = True
        HScroll1.Visible = True
      Else
        Label3.Visible = False
        Label2.Visible = False
        DataGrid1.Visible = False
        HScroll1.Visible = False
      End If
    End If这是我的查询,strikerfy(胖茄子) 谢谢你,你的程序对我很有帮助。