用ADODC来绑定ACCESS数据库,用DataGrid来显示数据库内容 
数据库中有20条记录,5个字段。按指定条件判断循环记录条, 如果,存在符合条件的当前行就停止循环。 
有过给代码实例的程序,但是很不稳定。 
如 
Private Sub Command1_Click() 
    Dim i As Integer 
    Dim j As Integer 
    Dim strTemp As String 
    
    With DataGrid1 
        For i = 0 To Adodc1.Recordset.RecordCount - 1 
            .Row = i 
            strTemp = "" 
            
            For j = 1 To 3 
                .Col = j 
                strTemp = strTemp & .Text 
            Next j 
            
            If Not InStr(1, strTemp, Text2) > 0 Then 
                Text1.Text = i 
                Exit For 
            End If 
        Next i 
        
    End With 
    
End Sub 有没有更好的思路呢 

解决方案 »

  1.   

    Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer
        Dim blnHave As Boolean
        
        With Adodc1
            .Recordset.MoveFirst
            
            While Not .Recordset.EOF
                For j = 0 To .Recordset.Fields.Count - 1
                    If InStr(1, .Recordset.Fields(j), Text2.Text) > 0 Then
                        Text1.Text = i
                        blnHave = True
                        .Recordset.MoveLast
                        Exit For
                    End If
                Next j
                
                i = i + 1
                .Recordset.MoveNext
            Wend
        End With
        
        If blnHave Then
            With DataGrid1
                .Row = i 
                .Col = j
                
                .SetFocus
            End With
        End If
        
    End Sub
      

  2.   

    给段代码参考:Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer
        Dim strTemp As String
        Adodc1.Recordset.MoveFirst
        For i = 0 To 100 'Adodc1.Recordset.RecordCount - 1
            strTemp = ""
            For j = 0 To 2 '总列数减1
                strTemp = strTemp & Adodc1.Recordset(j)
            Next j
            If Not InStr(1, strTemp, Text2) > 0 Then
                Text1.Text = i
                Exit For
            End If
            Adodc1.Recordset.MoveNext
        Next i
    End Sub
      

  3.   

    Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer    With Adodc1
            .Recordset.MoveFirst        Do
                For j = 0 To .Recordset.Fields.Count - 1
                    If InStr(1, .Recordset.Fields(j), Text2.Text) > 0 Then
                        MsgBox .Recordset.Fields(j).Value
                        Text1.Text = i
                        Text3 = DataGrid1.Row
                        .Recordset.MoveNext
                        Exit Do
                    End If
                Next j            i = i + 1
                .Recordset.MoveNext
            Loop Until .Recordset.EOF
        End WithEnd Sub
      

  4.   


    Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer
        'Dim strTemp As String
        Adodc1.Recordset.MoveFirst
        For i = 0 To 100 'Adodc1.Recordset.RecordCount - 1
            'strTemp = ""
            For j = 0 To 2 '总列数减1
                If Adodc1.Recordset(j) ="" Then
                    Text1.Text = i
                    Exit Sub
                 End If
            Next j
            Adodc1.Recordset.MoveNext
        Next i
    End Sub
            
      

  5.   

    Private Sub Command1_Click() 
        Dim i As Integer 
        Dim j As Integer 
        Dim strTemp As String 
        
        adodc1.refresh
            
                For j = 0 To .Recordset.Fields.Count - 1
                    If InStr(1, .Recordset.Fields(j), Text2.Text) > 0 Then
                        MsgBox .Recordset.Fields(j).Value
                        Text1.Text = i
                        Text3 = DataGrid1.Row
                        .Recordset.MoveNext
                        Exit for                
                       End If
                Next j            i = i + 1
                .Recordset.MoveNext
            Loop Until .Recordset.EOF
        End WithEnd Sub
    试一下看行不
      

  6.   

    此问题没有人正确解答
    上面程序中If InStr(1, .Recordset.Fields(j), Text2.Text) > 0 Then 这一句错了,要这样赋值instr 前面要加not, 还有不能 大于0 ,要等于0还有没有达到高效而精简的代码程序和正确的逻辑思维。