Results 1 to 2 of 2
  1. #1
    IT_Guy is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2012
    Posts
    1

    Button to loop through records and update table fields

    Hi all, I'm new to the forums and this is my first post. I have searched these forums in hopes to find a solution to this issue but haven't come across it yet.
    I have a form "Cost Sheet" thats linked to its respective table. I have about 2700 records and calculated fields do not save to the table ( I know why, and i know its bad practice to save calculated info to your table but it's strickly through a test database).



    I wan't to be able to click the button on the form > have it loop through all the records > and assign data from the calculated fields in the form to the "Cost Sheet" table.
    Here's what i have so far, I don't get any errors but it's not changing anything in the table after i click.

    Code:
    Private Sub updateRecords_Click()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
    
        Set db = CurrentDb
        Set rs = db.OpenRecordset("SELECT * FROM [COST SHEET]")
        
        If Not rs.EOF Then rs.MoveFirst
        Do Until rs.EOF
        rs.Edit
        packTotalField1 = Text364
        packTotalField2 = Text366
        packTotalField3 = Text367
        packTotalField4 = Text368
        finalSubField1 = SUB_TOTAL_1
        finalSubField2 = SUB_TOTAL_2
        finalSubField3 = SUB_TOTAL_3
        finalSubField4 = SUB_TOTAL_4
        laborSubField1 = SUB_TOTAL1
        laborSubField2 = SUB_TOTAL2
        laborSubField3 = SUB_TOTAL3
        laborSubField4 = SUB_TOTAL4
        matSubTotal1 = MATERIAL_SUB_TOTAL1
        matSubTotal2 = MATERIAL_SUB_TOTAL2
        matSubTotal3 = MATERIAL_SUB_TOTAL3
        matSubTotal4 = MATERIAL_SUB_TOTAL4
        subletSubTotal1 = SUBLET_SUB_TOTAL1
        subletSubTotal2 = SUBLET_SUB_TOTAL2
        subletSubTotal3 = SUBLET_SUB_TOTAL3
        subletSubTotal4 = SUBLET_SUB_TOTAL4
        totalPerPiece1 = TOTAL_PER_PIECE_1
        totalPerPiece2 = TOTAL_PER_PIECE_2
        totalPerPiece3 = TOTAL_PER_PIECE_3
        totalPerPiece4 = TOTAL_PER_PIECE_4
        rs.Update
        rs.MoveNext
    Loop
    
    End Sub
    Any tips would be much appreciated.

    /EDIT
    Ok what it's actually doing is when i click this button, only the record i currently have selected in the form is getting updated. I need this to loop through all 2700 and update, any suggestions?

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    I'm wondering . . .

    When you do the rs.MoveNext . . . does your Form get refreshed to move to the next record? . . . because you're getting all the data for your fields from the Form - right?
    Are the Table fields you are updating from TextBoxes on the Form - all the calculated fields you mention?
    Is the Form getting Refreshed [using a 'Requery', for instance]?
    In other words - are the Values in the Text Boxes actually changing?

    Have you tried putting a Break Point in the Loop to step through a few records so you can see what is happening to the values in the Text Boxes?

    I'm not sure that that is the problem, but I would try stepping through two or three records [using a restricted loop . . . For i = 1 to 3 . . .] - to see what values are changing on the Form.

    I usually approach Updates a little differently, so I'm not exactly sure about the way you are doing it [which might be a better way than what I use].

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 12-20-2011, 07:33 AM
  2. Replies: 5
    Last Post: 03-20-2010, 08:30 AM
  3. programming button to update records
    By lstairs in forum Programming
    Replies: 5
    Last Post: 02-04-2010, 08:07 AM
  4. Replies: 5
    Last Post: 01-05-2010, 10:22 PM
  5. update table in loop based on IF statement
    By LALouw in forum Access
    Replies: 0
    Last Post: 07-26-2009, 08:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums