Cycle Through Records in a Table using VBA

R

RC-

Hi all,
How can I cycle through all of the records in a table using VBA? Right
now, I have VB code that looks at the first record when the form opens but I
need to look at, and modify, all of the records in the table.

Any help will be great!!
 
F

fredg

Hi all,
How can I cycle through all of the records in a table using VBA? Right
now, I have VB code that looks at the first record when the form opens but I
need to look at, and modify, all of the records in the table.

Any help will be great!!

You can adapt this.
Add your own error handling.

Public Sub EditTable()
Dim Db As DAO.Database
Dim rs As DAO.Recordset
Set Db = CurrentDb
Set rs = Db.OpenRecordset("YourTableName")

rs.MoveFirst
Do While Not rs.EOF
rs.Edit
If rs!LastName = "Jones" Then
With rs
rs![LastName] = "Johnston"
.Update
End With
End If
rs.MoveNext
Loop

rs.Close
Db.Close
Set rs = Nothing
Set Db = Nothing
End Sub
 
R

RC-

Perfect, that's what I was looking for!

Thanks much.
RC-

fredg said:
Hi all,
How can I cycle through all of the records in a table using VBA?
Right
now, I have VB code that looks at the first record when the form opens
but I
need to look at, and modify, all of the records in the table.

Any help will be great!!

You can adapt this.
Add your own error handling.

Public Sub EditTable()
Dim Db As DAO.Database
Dim rs As DAO.Recordset
Set Db = CurrentDb
Set rs = Db.OpenRecordset("YourTableName")

rs.MoveFirst
Do While Not rs.EOF
rs.Edit
If rs!LastName = "Jones" Then
With rs
rs![LastName] = "Johnston"
.Update
End With
End If
rs.MoveNext
Loop

rs.Close
Db.Close
Set rs = Nothing
Set Db = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top