Access Code to loop through table records and update

Hello Experts.

I need your assistance with the following code.  For some reason, the code only updates the first record and will not repeat steps for following records in table.

Would you please assist?

Private Sub cmd_UpdateCheckFields_Click()

Dim MyDB As DAO.Database
Dim MyRst As DAO.Recordset

Set MyRst = CurrentDb.OpenRecordset("tbl_TEMP_RFI")

MyRst.MoveFirst

Do Until MyRst.EOF
    Me.chk_ProductInfo_NDC = chk0(Me.ProductInfo_NDC)
    Me.chk_ProductInfo_ExpectedLaunchDate = chk1(Me.ProductInfo_ExpectedLaunchDate)
    Me.chk_ProductInfo_PkgSize = chk2(Me.ProductInfo_PkgSize)
    
    MyRst.MoveNext
    
Loop
       
MyRst.Close
Set MyRst = Nothing

End Sub

Open in new window


Thank you.
dhempleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pdebaetsCommented:
You'll probably need the .edit and .update methods in there. Try this

Private Sub cmd_UpdateCheckFields_Click()

Dim MyDB As DAO.Database
Dim MyRst As DAO.Recordset

Set MyRst = CurrentDb.OpenRecordset("tbl_TEMP_RFI")

MyRst.MoveFirst

Do Until MyRst.EOF
    MyRst.Edit
    Me.chk_ProductInfo_NDC = chk0(Me.ProductInfo_NDC)
    Me.chk_ProductInfo_ExpectedLaunchDate = chk1(Me.ProductInfo_ExpectedLaunchDate)
    Me.chk_ProductInfo_PkgSize = chk2(Me.ProductInfo_PkgSize)
    MyRst.Update

    MyRst.MoveNext
    
Loop
       
MyRst.Close
Set MyRst = Nothing

End Sub

Open in new window

0
dhempleAuthor Commented:
Thank you for your quick response. I've entered the Edit and Update commands you suggested and now I'm getting a run time error when the code tries to perform the steps after MyRst.Edit.  "You can't assign a value to this object."  

I was able to update the fields prior to entering the Edit and Update commands.

Any other thoughts? Anyone.  Thank you.
0
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
<<Do Until MyRst.EOF
    Me.chk_ProductInfo_NDC = chk0(Me.ProductInfo_NDC)
    Me.chk_ProductInfo_ExpectedLaunchDate = chk1(Me.ProductInfo_ExpectedLaunchDate)
    Me.chk_ProductInfo_PkgSize = chk2(Me.ProductInfo_PkgSize)
   
    MyRst.MoveNext
   
Loop>>

  Your referring to the controls/fields on the form, which is pointing to a single record.  You want to refer to the fields in the recordset that you opened.

Do Until MyRst.EOF

    MyRst.Edit
    MyRst![<field>] = chk0(Me.ProductInfo_NDC)
    MyRst![<field>]  = chk1(Me.ProductInfo_ExpectedLaunchDate)
    MyRst![<field>]  = chk2(Me.ProductInfo_PkgSize)
    MyRst.Update
   
    MyRst.MoveNext
   
Loop

 And as Peter said, the .edit/.update are needed as this is a DAO record set.

 This is assuming you want to update the records in the record set itself.

Jim.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pdebaetsCommented:
Good catch Jim. I missed the fact that his code was updating form fields instead of the recordset.
0
dhempleAuthor Commented:
Here's what worked for me.

Private Sub cmd_UpdateCheckFields_Click()

Dim MyDB As DAO.Database
Dim MyRst As DAO.Recordset

Set MyRst = CurrentDb.OpenRecordset("tbl_TEMP_RFI")

MyRst.MoveFirst

Do Until MyRst.EOF

    MyRst.Edit
    MyRst![chk_ProductInfo_NDC] = chk0(MyRst![ProductInfo_NDC])
    MyRst![chk_ProductInfo_ExpectedLaunchDate] = chk1(MyRst![ProductInfo_ExpectedLaunchDate])
    MyRst![chk_ProductInfo_PkgSize] = chk2(MyRst![ProductInfo_PkgSize])
    MyRst.Update
    
    MyRst.MoveNext
    
Loop
       
MyRst.Close
Set MyRst = Nothing

End Sub

Open in new window

0
dhempleAuthor Commented:
Thanks for your help on this.  I have another issue that surfaced as a result of this solution.  I'll create another question in hopes of a solution.  Thanks again!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.