用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有没有更好的思路呢
你贴出的代码也感觉有问题,是对获取的记录集进行判断还是对显示于DataGrid内的值进行判断.
Next i若Adodc1.Recordset.RecordCount大于20,是循环不到记录集合终点的.
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
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
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
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
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
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
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