用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.   

    同意1楼的观点,LZ未说清楚要求.按指定条件(符合哪个字段)判断循环记录条?你有5个字段呢!
    你贴出的代码也感觉有问题,是对获取的记录集进行判断还是对显示于DataGrid内的值进行判断.
      

  2.   

    LZ:试验表明你的    For i = 0 To Adodc1.Recordset.RecordCount - 1
        Next i若Adodc1.Recordset.RecordCount大于20,是循环不到记录集合终点的.
      

  3.   

    判断某个字段与你要找的相同,用下面程序Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer
        
        With Adodc1
            .Recordset.MoveFirst
            
            While Not .Recordset.EOF
                i = i + 1
                
                For j = 0 To .Recordset.Fields.Count - 1
                    If .Recordset.Fields(j) = Text2.Text Then
                        Text1.Text = i
                        .Recordset.MoveLast
                        Exit For
                    End If
                Next j
            Wend
        End With
        
    End Sub
      

  4.   

    呵呵,不好意思,漏了Private Sub Command1_Click()
        Dim i As Integer
        Dim j As Integer
        
        With Adodc1
            .Recordset.MoveFirst
            
            While Not .Recordset.EOF
                i = i + 1
                
                For j = 0 To .Recordset.Fields.Count - 1
                    If .Recordset.Fields(j) = Text2.Text Then
                        Text1.Text = i
                        .Recordset.MoveLast
                        Exit For
                    End If
                Next j
                
                .Recordset.MoveNext
            Wend
            
        End With
        
    End Sub
      

  5.   

    放个焦点
    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 .Recordset.Fields(j) = Text2.Text 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 - 1
                .Col = j
                
                .SetFocus
            End With
        End If
        
    End Sub
      

  6.   

    不要用 Row 属性,它仅仅针对可见行。Private Sub Command1_Click() 
        Dim i As Integer 
        Dim j As Integer 
        Dim strTemp As String 
        
        With ADODC1.Recordset 
            Do Until .EOF
                strTemp = "" 
                
                For j = 1 To 3 
                    strTemp = strTemp & .Fields(j)
                Next j 
                
                If Not InStr(1, strTemp, Text2) > 0 Then 
                    Text1.Text = .AbsolutePosition
                    Exit For 
                End If
                .MoveNext 
            Loop 
        End With 
        
    End Sub 
      

  7.   

    更正:
    Private Sub Command1_Click() 
        Dim i As Integer 
        Dim j As Integer 
        Dim strTemp As String 
        
        With ADODC1.Recordset 
            Do Until .EOF 
                strTemp = "" 
                
                For j = 1 To 3 
                    strTemp = strTemp & .Fields(j) 
                Next j 
                
                If Not InStr(1, strTemp, Text2) > 0 Then 
                    Text1.Text = .AbsolutePosition 
                    Exit Do 'here 
                End If 
                .MoveNext 
            Loop 
        End With 
        
    End Sub 
      

  8.   

    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 - 1
                .Col = j
                
                .SetFocus
            End With
        End If
        
    End Sub
      

  9.   

    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 - 1
                .Col = j
                
                .SetFocus
            End With
        End If
        
    End Sub