A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » General Discussion
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Cycle Through Records in a Table using VBA



 
 
Thread Tools Display Modes
  #1  
Old February 11th, 2005, 05:11 PM
RC-
external usenet poster
 
Posts: n/a
Default Cycle Through Records in a Table using VBA

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!!


  #2  
Old February 11th, 2005, 05:54 PM
fredg
external usenet poster
 
Posts: n/a
Default

On Fri, 11 Feb 2005 10:11:26 -0700, RC- wrote:

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

--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.
  #3  
Old February 11th, 2005, 06:12 PM
RC-
external usenet poster
 
Posts: n/a
Default

Perfect, that's what I was looking for!

Thanks much.
RC-

"fredg" wrote in message
...
On Fri, 11 Feb 2005 10:11:26 -0700, RC- wrote:

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

--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Merging Records from one table to another on different databases Mike Smith General Discussion 5 November 3rd, 2004 07:02 PM
Table Wizard Does Not Set Relationship if Foreign Key and Primary Key Name Do Not Match Exactly in Case. HDW Database Design 3 October 16th, 2004 03:42 AM
New records can't be seen rleblanc Using Forms 6 August 14th, 2004 02:43 PM
Table design BillT New Users 11 May 25th, 2004 03:41 PM


All times are GMT +1. The time now is 11:35 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.