Results 1 to 4 of 4
  1. #1
    avarusbrightfyre is offline Advanced Beginner
    Windows Vista Access 2003
    Join Date
    Sep 2009
    Posts
    60

    Multi-record additions using a list box not working right

    I was referred to an example database for adding multiple table entries using a list box. I have it all set up, and this is the code I'm using for the command button to add the entries:

    Private Sub Command9_Click()

    Dim strSQL As String
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim ctl As Control
    Dim varItem As Variant
    On Error GoTo ErrorHandler
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Marine Gig Count", dbOpenDynaset, dbAppendOnly)
    'make sure a selection has been made
    If Me.ListMarines.ItemsSelected.Count = 0 Then
    MsgBox "Must select at least 1 employee"
    Exit Sub
    End If
    'add selected value(s) to table
    Set ctl = Me.ListMarines
    For Each varItem In ctl.ItemsSelected
    rs.AddNew


    rs!Rank = ctl.Column(1)
    rs!LastName = ctl.Column(2)
    rs!FirstName = ctl.Column(3)
    rs!MI = ctl.Column(4)
    rs!EventName = Me.EventName
    rs!EventType = Me.EventType
    rs!EventDate = Me.EventDate
    rs.Update
    Next varItem
    ExitHandler:
    Set rs = Nothing
    Set db = Nothing
    Exit Sub
    ErrorHandler:
    Select Case Err
    Case Else
    MsgBox Err.Description
    DoCmd.Hourglass False
    Resume ExitHandler
    End Select

    End Sub

    What it ends up doing is just adding the information for the last item in the list box I clicked, and doing it for the number of entries I clicked on (i.e. I click five names and I get five duplicate entries with the info of the last person I clicked on). Is this a programming thing, or is there some setting I need to change on the list box?

  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
    Here's an example:

    http://www.baldyweb.com/MultiselectAppend.htm

    The source of your problem is that you never refer to the selected row in the loop, like this from my example:

    rs!EmpID = ctl.ItemData(varItem)

    For a multi-column situation like yours it looks like:

    ctl.Column(x, varItem)

    where x is the desired column.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    avarusbrightfyre is offline Advanced Beginner
    Windows Vista Access 2003
    Join Date
    Sep 2009
    Posts
    60
    Perfect! That solved my problem nicely! Thanks a bunch!

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problemo!
    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. 2 multi select list boxes on one search form
    By woodey2002 in forum Forms
    Replies: 2
    Last Post: 11-05-2010, 12:44 PM
  2. Problem creating Multi level list boxes - pleas help!
    By AccessConfused in forum Access
    Replies: 6
    Last Post: 10-24-2010, 09:30 PM
  3. Multi-select List Boxes
    By Rawb in forum Programming
    Replies: 6
    Last Post: 09-21-2010, 09:02 AM
  4. Multi-Query List Box Combinations?
    By BizIntelGuy in forum Access
    Replies: 3
    Last Post: 07-20-2010, 03:20 PM
  5. Replies: 1
    Last Post: 03-09-2006, 12:12 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