Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    67

    Question Unanswered: Prob. W/adding records from listbox?

    I currently have a listbox that is populated via a query.
    I have the listbox set to multiple selections.
    I have a command buttom “Add Records” and its procedure is as follows:
    ==========================
    Private Sub CmdAdd2Select_Click()
    Dim myfrm As Form, myCtl As Control
    Dim Item As Variant
    Dim rst As DAO.Recordset
    Set rst = CurrentDb.OpenRecordset("tblSelected", dbOpenDynaset)

    Set myfrm = Me
    Set myCtl = Me.ListRandom

    With rst
    For Each Item In myCtl.ItemsSelected
    .AddNew
    .Fields("RandomID") = Me.ListRandom.Column(0)
    .Fields("PatientID") = Me.ListRandom.Column(1)
    .Fields("ContactID") = Me.ListRandom.Column(4)
    .Fields("reason") = "Random"
    .Fields("SelectDate") = Date
    .Update

    Next Item
    End With
    Set rst = Nothing
    MsgBox "Added to Selection Database", vbInformation, "OK......"
    End Sub

    The problem is; it adds that last record selected multiple times.
    If I select four records, it adds the last selected record 4-times.

    What am I doing wrong here?

    Thanks for all your help.

  2. #2
    Join Date
    May 2005
    Posts
    119
    .Fields("RandomID") = Me.ListRandom.Column(0)
    .Fields("PatientID") = Me.ListRandom.Column(1)
    .Fields("ContactID") = Me.ListRandom.Column(4)

    Try using
    .Fields("RandomID") = Me.Listrandom.Column(0, Item)
    .Fields("PatientID") = Me.ListRandom.Column(1, Item)
    .Fields("ContactID") = Me.ListRandom.Column(4, item)

    Your code was only looking at the last record selected. Adding the item will look at the specific item in your loop.

    Hope this helps!
    Krista

  3. #3
    Join Date
    Feb 2004
    Posts
    67
    Krista,
    Your an angel, I am still learning this code business.
    The code now works great.....Thanks Again...
    Chuck.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •