vba cycle through rows code example
Example 1: VBA Loop through recordset
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Contacts")
If Not (rs.EOF And rs.BOF) Then
Do Until rs.EOF = True
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
Example 2: vba loop through recordset
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM Contacts")
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
rs.Edit
rs!VendorYN = True
rs("VendorYN") = True
rs.Update
sContactName = rs!FirstName & " " & rs!LastName
rs.MoveNext
Loop
Else
MsgBox "There are no records in the recordset."
End If
MsgBox "Finished looping through records."
rs.Close
Set rs = Nothing