Hello All, I have two multi-select listboxes, 'listFrom' and 'listTo'. I want to be able to copy mulitple selections from 'listfrom' to 'listTo' and delete those selections from 'listfrom'.
To copy over multiple items is no problem but when i introduce the 'removeitem' line to delete the selection, it deletes only the first selection encountered and exits the loop - In debug mode, after it deletes the item in current index, it deselects the other items selected and in the next run of the loop finds no selected item and it just exits. Below is the code i am working with - Any help with this will be much appreciated!:
----------------------------------------------------------
Dim i As Long
Dim lstCount As Long
lstCount = lstFrom.ListCount - 1

For i = lstCount To 0 Step -1
If lstFrom.Selected(i) = True Then

'Add to default list
lstTo.AddItem lstFrom.Column(0, i)

'Remove item from source list
lstFrom.RemoveItem i
End If
Next i
---------------------------------------------------------------

I forgot to mention, the final application will be run on Access 2003 and above ... i have referenced other sources that use the ".list" function e.g. 'lstTo.Additem lstFrom.list(i)' which seems to be absent under this version of access.

Recommended Answers

All 2 Replies

Hi,

Try this:

Dim i As Long
For i = (lstFrom.ListCount - 1) To 0 Step -1
If lstFrom.Selected(i) = True Then
   lstTo.AddItem lstFrom.Column(0, i)
   lstFrom.RemoveItem i
   If lstFrom.ListCount = 0 Then Exit For
End If
Next i

Dont assign Listcount to a variable and loop.. That's because, Once an Item is removed from the list, ListCount reduces by one..

Regards
Veena

I finally cracked it. This was how i solved the problem.
I had a section of the code to allow adding of the items selected to the destination list while storing the indices of those selections in an array.
The array size i got from the number of items from the source list. Initially i was thinking of setting this to a high-number but this is not best practice.
Anyway, here is the code for all to see:
'---------------------------------------------------------------------
'Author: Tom a.k.a Memnac
'Date: 1/07/2008
'URL Access: http://www.daniweb.com/forums/post638130.html#post638130
'---------------------------------------------------------------------
Dim lstFrom As ListBox 'source list
Dim lstTo As ListBox 'Destination list
Dim i As Long
Dim lstCount As Long

'assign variable to number of list items (assuming Zero-based indexing)
lstCount = lstFrom.ListCount - 1

'Declare and assign to the array a size corresponding to the number of list items
Dim vart() As Variant
ReDim vart(lstCount)


'DUMMY VARIABLES
Dim vindex As Long
Dim vindx2 As Long
Dim vindx3 As Long
Dim vtotal As Long


'initialize variables
vindex = 0
vindx2 = 0
vtotal = 0

'Check for selected items
For i = 0 To lstCount
If lstFrom.Selected(i) = True Then

'Store index of selected item
vart(vindex) = i

'Add to destination list from source list
lstTo.AddItem lstFrom.Column(0, i)

vindex = vindex + 1

'Store index value here. This will be used to check the condition in the Do loop section below
vtotal = vindex
End If
Next i

'Reset index
vindex = 0

'Perform at least one run of the code
Do
'Check the following condition at this stage otherwise in the event you have but one item in the list to remove --
'It will either return a subscript, out-of-range error or an invalid operator at the line calling 'removeitem'
If vindex = (vtotal) Then
Exit Do
End If

'Calculate index to reference selected item
vindx3 = vart(vindex) - vindx2

'Remove the item
lstFrom.RemoveItem vindx3

'increment both index variables
vindex = vindex + 1
vindx2 = vindx2 + 1
Loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.