• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1019
  • Last Modified:

copy to clipboard all selected items from a listbox in access 2003 and paste them in a table that is opened in datasheet mode

Hello , i would like to ask if theres a way to copy all items that i multiselect from a listbox in an access form and paste them in a table that is opened in datasheet mode.

Actually the problem is , that when i multiselect the items and copy them with ctrl-c to clipboad it only pastes tha last record of my bounded column of the listbox ...
i really need the record of the bounded column but not just the last one ...
I suppose that what i does is that it copies each record separately  and appends the clipboard with next record by deleting the previous one . the result is that it pastes only the last one . Thank you
0
Anisoropos
Asked:
Anisoropos
1 Solution
 
TextReportCommented:
The Copy will not work for you as you have discovered, you can process the selected values and either paste this or use those to add the new records to the table through code, I would recoment the latter.

The code below will get the first column value, usually the ID into a string, this string can be used in a IN Clause for your INSERT Statement as shown later in the example. This is a command button on the form and you will have to adapt the code to match your field names and control names.

Cheers, Andrew
Private Sub cmdGetID_s_Click()
Dim strList As String
Dim cnt As Long
 
    If Me.lstMyListBox.ItemsSelected.Count > 0 Then
       For cnt = 0 To Me.lstMyListBox.ItemsSelected.Count - 1
           ' use this line if ID is a Number
           strList = strList & ", " & Me.lstMyListBox.Column(0, Me.lstMyListBox.ItemsSelected(cnt))
           ' use this line if ID is text
           strList = strList & ", " & Chr(39) & Me.lstMyListBox.Column(0, Me.lstMyListBox.ItemsSelected(cnt)) * Chr(39)
       Next cnt
       If strList <> "" Then strList = Mid(strList, 3)
       MsgBox strList
       
       strSQL = "INSERT INTO MyTable" & vbCrLf & _
                "   (ID, Field1, Field2)" & vbCrLf & _
                " SELECT ID" & vbCrLf & _
                "      , Field1" & vbCrLf & _
                "      , Field2" & vbCrLf & _
                " FROM MyTable" & vbCrLf & _
                " WHERE ID IN (" & strList & ")" & vbCrLf
        CurrentDb.Execute strSQL, dbFailOnError
    End If
End Sub

Open in new window

0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now