Write selected record from multi-select listbox to a table

I have a listbox on a form which is set to be simple multi-select.  When the user clicks on one of the records in the listbox I want the PK value of that record to be written to a separate table.  

Here is what I have so far in the onclick event of the list box.  But it errors out (Item not found in this collection) on the line:  

!ID = SelectedValue


Private Sub ListSelections_Click()

Dim RS As DAO.Recordset
Dim SelectedValue As String
Dim varItem As Variant

    Set RS = CurrentDb.OpenRecordset("Select * from tblClientAssessmentTechniquesUsed where ClientID = " & Me.txtClientID.Value)
    With RS
    
        If .RecordCount = 0 Then
            MsgBox "This appears to be a new selection which does not already exist in the client table.  A new selection is being created."
            SelectedValue = ListSelections.ItemData(varItem)
            .AddNew
        Else
            MsgBox "This selection already exists and will be edited if you have made changes."
            .Edit
        End If
            SelectedValue = ListSelections.ItemData(varItem)
            !ClientID = Me.txtClientID
            !ID = SelectedValue
            .Update
    End With

End Sub

Open in new window


Am I even close with the code?
SteveL13Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hamed NasrRetired IT ProfessionalCommented:
In your example use liat box with single item selection.

 SelectedValue = Me.ListSelections.value
Rey Obrero (Capricorn1)Commented:
what is the RowSource of the listbox?

is the PK a number or Text data?

if it is Number, change

Dim SelectedValue As String

with

Dim SelectedValue as Long
SelectedValue = Me.ListSelections.value
John TsioumprisSoftware & Systems EngineerCommented:
Try this
Dim ListBoxItem As Variant
For Each ListBoxItem In Me.YourListBox.ItemsSelected
'         YourCode here using YourListBox.ItemData(ListBoxItem)

Open in new window

SteveL13Author Commented:
Here is what I have now.  Getting an error... "Update or cancelupdate without AddNew or Edit. ???

Dim RS As DAO.Recordset
Dim SelectedValue As Long
Dim varItem As Variant

    Set RS = CurrentDb.OpenRecordset("Select * from tblClientAssessmentTechniquesUsed where ClientID = " & Me.txtClientID.Value)
    With RS
    
        If .RecordCount = 0 Then
            MsgBox "This appears to be a new selection which does not already exist in the client table.  A new selection is being created."
            !ClientID = Me.txtClientID
            !SelectedValue = Me.ListSelections.Value
            .AddNew
        Else
            MsgBox "This selection already exists and will be edited if you have made changes."
            .Edit
            !ClientID = Me.txtClientID
            !SelectedValue = Me.ListSelections.Value
            .Update
            End If
    End With

Open in new window

Rey Obrero (Capricorn1)Commented:
move line 12 after line 8
and replace line 12 with .Update
       If .RecordCount = 0 Then
           .Addnew
           !ClientID = Me.txtClientID
            !SelectedValue = Me.ListSelections.Value
           .Update

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.