Results 1 to 5 of 5
  1. #1
    tfulmer is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Location
    Austin, Texas
    Posts
    19

    Update multiple records selected from list box

    Not sure if this should be in the programming forum or this forum. I have seen similar postings in both forums.

    I am a novice trying to design a form that will update a field in multiple selected records from a list box. The update data comes from a text box on the same form.

    I am using code behind a command button to try to accomplish this. The idea is that once the user 1) inputs the desired text data into the text box; 2) selects multiple records from the list box; 3) clicks the command button - the updates are completed.

    My problem is successfully updating only the selected records, versus all of the records that are in the table.



    I would appreciate any assistance.

    Tom in Austin, TX

    Here is my code:
    Option Compare Database
    Option Explicit
    Private Sub Command6_Click()
    Dim frm As Form
    Dim ctl As Control
    Dim varitem As Variant
    Dim strsql As String

    Set frm = Forms!form2
    Set ctl = frm!List4

    If Forms!form2!List4.ItemsSelected.Count <> 0 Then
    For Each varitem In ctl.ItemsSelected
    strsql = "UPDATE checks2 SET " & _
    "checks2.adv3_cr = forms!form2!cr_number.value " & _
    "WHERE 'checks2.check_number' = " & _
    "'forms!form2!List4.itemdata(varitem)'; "

    DoCmd.RunSQL strsql
    Next varitem

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

    End Sub

  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,521
    Try

    strsql = "UPDATE checks2 SET " & _
    "checks2.adv3_cr = '" & forms!form2!cr_number.value & "' " & _
    "WHERE checks2.check_number = '" & _
    forms!form2!List4.itemdata(varitem) & "'; "

    This should help:

    http://www.baldyweb.com/ImmediateWindow.htm
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Need to concatenate variables in the construction of SQL string. Treat reference to controls on form as variables.

    strsql = "UPDATE checks2 SET adv3_cr = '" & forms!form2!cr_number & "'" & _
    " WHERE check_number = '" & forms!form2!List4.itemdata(varitem) & "'"

    If adv3_cr or check_number are number fields, remove apostrophe delimiters - date data would use # as delimiter.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    tfulmer is offline Novice
    Windows XP Access 2003
    Join Date
    Jul 2011
    Location
    Austin, Texas
    Posts
    19
    Thank you for the suggestions and help - they work great. I can't say enough for experienced users like yourselves who are willing to assist novices on this forum.

  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,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 18
    Last Post: 01-27-2012, 12:53 PM
  2. Replies: 2
    Last Post: 01-05-2012, 11:52 AM
  3. Deleting multiple records selected in a Listbox
    By ankitmehtta in forum Access
    Replies: 4
    Last Post: 11-08-2011, 01:20 PM
  4. Subform requery after multiple records selected
    By AccessBlues in forum Forms
    Replies: 8
    Last Post: 08-04-2011, 11:03 AM
  5. Replies: 4
    Last Post: 06-16-2011, 09:30 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