Private Sub Command1_Click()
Dim strSQL As String
Dim strCon(7) As String
Dim intCount As Integer
Dim i As Integer
intCount = 0
'获得所有得查询条件
If Text1.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(1) = "船型 like '%" & Text1.Text & "%'"
    Else
        strCon(1) = "船型='" & Text1.Text & "'"
    End If
Else
    strCon(1) = ""
End If
If Text2.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(2) = "产品号 like '%" & Text2.Text & "%'"
    Else
        strCon(2) = "产品号='" & Text2.Text & "'"
    End If
Else
    strCon(2) = ""
End If
If Text3.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(3) = "图号 like '%" & Text3.Text & "%'"
    Else
        strCon(3) = "图号='" & Text3.Text & "'"
    End If
Else
    strCon(3) = ""
End If
If Text4.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(4) = "零件号 like '%" & Text4.Text & "%'"
    Else
        strCon(4) = "零件号='" & Text4.Text & "'"
    End If
Else
    strCon(4) = ""
End If
If Text5.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(5) = "校管状态 like '%" & Text5.Text & "%'"
    Else
        strCon(5) = "校管状态='" & Text5.Text & "'"
    End If
Else
    strCon(5) = ""
End If
If Text6.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(6) = "校管日期 like '%" & Text6.Text & "%'"
    Else
        strCon(6) = "校管日期='" & Text6.Text & "'"
    End If
Else
    strCon(6) = ""
End If
If Text7.Text <> "" Then
    If Check1.Value = 1 Then
        strCon(7) = "校管施工人 like '%" & Text7.Text & "%'"
    Else
        strCon(7) = "校管施工人='" & Text7.Text & "'"
    End If
Else
    strCon(7) = ""
End IfIf strCon(1) = "" And strCon(2) = "" And strCon(3) = "" And strCon(4) = "" And strCon(5) = "" And strCon(6) = "" And strCon(7) = "" Then
    strSQL = "select * from xinxi"
Else
   strSQL = "select * from xinxi where "
    For i = 1 To 7
       If strCon(i) <> "" Then
          intCount = intCount + 1
            If intCount = 1 Then
                strSQL = strSQL + strCon(i)
            Else
                strSQL = strSQL + " and " + strCon(i)
            End If
       End If
   Next
End If
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False"
Adodc1.CursorLocation = adUseClient
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = strSQL
Adodc1.Refresh
End Sub
Private Sub Command2_Click()
Unload Me
End SubPrivate Sub Form_Load()
    Dim db As New ADODB.Connection
    Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False"
    db.ConnectionString = Adodc1.ConnectionString
    If db.State <> ADODB.ObjectStateEnum.adStateClosed Then db.Close
    db.Open
       
End Sub
程序如上所示,麻烦大家看下什么问题。结果不能再DATAGIRD中显示出来。查询的条件是7个文本框

解决方案 »

  1.   

    Adodc1.CommandType = adCmdText**************************改成Adodc1.CommandType =adCmdUnknown 
      

  2.   

    Private Sub Command1_Click() 
    Dim strSQL As String 
    Dim strCon(7) As String 
    Dim intCount As Integer 
    Dim i As Integer 
    intCount = 0 
    '获得所有得查询条件 
    If Text1.Text <> "" Then 
        If Check1.Value = 1 Then 
            strCon(1) = "船型 like '%" & Text1.Text & "%'" 
        Else 
            strCon(1) = "船型='" & Text1.Text & "'" 
        End If 
    Else 
        strCon(1) = "" 
    End If 
    If Text2.Text <> "" Then 
        If Check1.Value = 1 Then 
            strCon(2) = "产品号 like '%" & Text2.Text & "%'" 
        Else 
            strCon(2) = "产品号='" & Text2.Text & "'" 
        End If 
    Else 
        strCon(2) = "" 
    End If 
    If Text3.Text <> "" Then 
        If Check1.Value = 1 Then 
            strCon(3) = "图号 like '%" & Text3.Text & "%'" 
        Else 
            strCon(3) = "图号='" & Text3.Text & "'" 
        End If 
    Else 
        strCon(3) = "" 
    End If 
    If Text4.Text <> "" Then 
        If Check1.Value = 1 Then 
            strCon(4) = "零件号 like '%" & Text4.Text & "%'" 
        Else 
            strCon(4) = "零件号='" & Text4.Text & "'" 
        End If 
    Else 
        strCon(4) = "" 
    End If 
    If Text5.Text <> "" Then 
        If Check1.Value = 1 Then 
            strCon(5) = "校管状态 like '%" & Text5.Text & "%'" 
        Else 
            strCon(5) = "校管状态='" & Text5.Text & "'" 
        End If 
    Else 
        strCon(5) = "" 
    End If 
    If Text6.Text <> "" Then 
        If Check1.Value = 1 Then 
            strCon(6) = "校管日期 like '%" & Text6.Text & "%'" 
        Else 
            strCon(6) = "校管日期='" & Text6.Text & "'" 
        End If 
    Else 
        strCon(6) = "" 
    End If 
    If Text7.Text <> "" Then 
        If Check1.Value = 1 Then 
            strCon(7) = "校管施工人 like '%" & Text7.Text & "%'" 
        Else 
            strCon(7) = "校管施工人='" & Text7.Text & "'" 
        End If 
    Else 
        strCon(7) = "" 
    End If If strCon(1) = "" And strCon(2) = "" And strCon(3) = "" And strCon(4) = "" And strCon(5) = "" And strCon(6) = "" And strCon(7) = "" Then 
        strSQL = "select * from xinxi" 
    Else 
      strSQL = "select * from xinxi where " 
        For i = 1 To 7 
          If strCon(i) <> "" Then 
              intCount = intCount + 1 
                If intCount = 1 Then 
                    strSQL = strSQL + strCon(i) 
                Else 
                    strSQL = strSQL + " and " + strCon(i) 
                End If 
          End If 
      Next 
    End If 
    Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False" 
    Adodc1.CursorLocation = adUseClient 
    Adodc1.CommandType = adCmdUnknown 
    Adodc1.RecordSource = strSQL 
    Adodc1.Refresh 
    End Sub 
    Private Sub Command2_Click() 
    Unload Me 
    End Sub Private Sub Form_Load() 
        Dim db As New ADODB.Connection 
        Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb;Persist Security Info=False" 
        db.ConnectionString = Adodc1.ConnectionString 
        If db.State <> ADODB.ObjectStateEnum.adStateClosed Then db.Close 
        db.Open 
          
    End Sub 
      

  3.   

        If strCon(1) = "" And strCon(2) = "" And strCon(3) = "" And strCon(4) = "" And strCon(5) = "" And strCon(6) = "" And strCon(7) = "" Then
            strSQL = "select * from xinxi"
        Else
          strSQL = "select * from xinxi where "
            For i = 1 To 7
              If strCon(i) <> "" Then
                  intCount = intCount + 1
                    If intCount = 1 Then
                        strSQL = strSQL + strCon(i)
                    Else
                        strSQL = strSQL + " and " + strCon(i)
                    End If
              End If
          Next
        End If
    *****************************************************简化一下      strSQL = "select * from xinxi where 1=1 "
          
          For i = 1 To 7
              If strCon(i) <> "" Then
                   strSQL = strSQL + " and " + strCon(i)
              End If
          Next
      

  4.   

    Adodc1.Refresh
    '加上这两句
    Set DBGrid1.DataSource = Adodc1
    DBGrid1.ReBind
      

  5.   

    Adodc1.CommandType = adCmdText 
    这句不要,然后
    Adodc1.Refresh 下面加一句看看有多少条记录
    debug.print Adodc1.recordset.recordcount