Results 1 to 6 of 6
  1. #1
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36

    updating multiple rows in a loop

    The annoying thing about this code is that it performs a separate update for each item in the list box. How could I use the loop to accumulate the records to be updated and then perform all the updates at once (ask for confirmation just once instead of multiple times)?

    Dim SQLMasterUpdate As String
    Dim iRow As Variant

    If Me!RestaurantListBox.ItemsSelected.Count <> 0 Then
    For Each iRow In Me!RestaurantListBox.ItemsSelected

    SQLMasterUpdate = " (SQL Update statement) "

    DoCmd.RunSQL SQLMasterUpdate



    Next iRow

    Else
    MsgBox "Nothing was selected from the list", vbInformation
    Exit Sub 'Nothing was selected
    End If

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    If you use

    CurrentDb.Execute SQLMasterUpdate, dbFailOnError

    it won't ask for confirmations. You could also use SetWarnings before and after, but Execute is more efficient anyway.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    I kind of do want a confirmation though, just ONE. Any ideas for that?

  4. #4
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    See your other post, which I have replied. It contains the approach you can use.

    https://www.accessforums.net/queries...ror-17471.html

    Basically, you need to wrap it in a transaction. Then, prompt the user once with MsgBox, if user accepts, then you commit the transaction.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I'd probably code my own with a Yes/No message box, but to use the native one you'd build an IN clause for the update query:

    WHERE FieldName IN(1,4,7)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    looloo is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    36
    Thanks, stmoong and pbaldy. I ended up going with the Yes/No box and the gears seem to be turning smoothly. Now for some cleanup and we'll see what's next...

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

Similar Threads

  1. Query on multiple rows
    By TPH in forum Access
    Replies: 10
    Last Post: 09-02-2011, 12:52 PM
  2. Comnbining multiple rows into one row
    By Equalizer700 in forum Queries
    Replies: 1
    Last Post: 04-19-2011, 03:51 AM
  3. Concatenate Multiple Rows into One
    By Knarly555 in forum Queries
    Replies: 5
    Last Post: 11-12-2010, 06:51 PM
  4. Replies: 5
    Last Post: 12-10-2009, 10:33 PM
  5. Combine multiple rows into one cell
    By zarfx4 in forum Queries
    Replies: 8
    Last Post: 06-08-2009, 10:42 AM

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