removing multiple selected items in a listbox - code only recognising first selection
Asked Answered
M

4

5

I have two listboxes and am trying to add items from List 1 to List 2, and then be able to remove multiple items from List 2 at once. Note that List 1 stays stagnant (this is how it is supposed to be).

I have the adding items working right:

'Add the selected items to List 2
Dim i As Integer

If lst1.ItemsSelected.Count > 0 Then
    i = 0
    While i < lst1.ListCount
        If lst1.Selected(i) Then
            lst2.AddItem (lst1.ItemData(i) & ";" & lst1.Column(1, i) & ";")
            lst1.Selected(i) = False
        End If
        i = i + 1
    Wend
End If

However, when I try to remove the items from List 2 in a similar way, it only recognises the first selected item as selected and skips over the other items that I have selected. This is the problem. Here is my code:

'Remove the selected items from List 2
Dim i As Integer

If lst2.ItemsSelected.Count > 0 Then
    i = lst2.ListCount - 1
    While i >= 0
       If lst2.Selected(i) Then
           lst2.RemoveItem (i)
           lst2.Selected(i) = False
       End If
        i = i - 1
    Wend
End If

How can I get this working correctly?

Manutius answered 6/2, 2013 at 23:9 Comment(2)
Which MS Office application are you using?Wilbourn
Access 2010 - sorry, I should have included this in the description!Manutius
W
8

As far as I can tell, as soon as you remove one item, all items become unselected, so:

Dim itm As Variant
Dim srem As String
Dim asrem As Variant

    For Each itm In lst2.ItemsSelected
        srem = srem & "," & itm
    Next

    asrem = Split(Mid(srem, 2), ",")
    For i = UBound(asrem) To 0 Step -1
        lst2.RemoveItem lst2.ItemData(asrem(i))
    Next

Note also that this is Access and you are dealing with a value list, so Replace on the text of Row Source will also work.

Wilbourn answered 7/2, 2013 at 12:42 Comment(4)
Thank you! Sorry if this is a silly question, but what should I be declaring each variable as? I assumed itm As Object and srem and asrem as String, but then I get an error highlighting UBound and expecting an array.Manutius
...and now I get a runtime error when it hits the first for loop. I feel like I'm missing something completely obvious.Manutius
The only changes I made were to declare each variable and then to change "lst2" to the actual name of the list box in my form. Everything else remains the same. I'm not sure what you mean by "event", I'm still learning as I go!Manutius
Ah yes of course I have that, the code executes on click of the "Remove" button, but then throws a runtime error which the debugger tells me happens when it hits the first For loop.Manutius
L
7

Try using a for/next loop instead of While?

Something like this works in PPT/XLS, should be similar in Access I think.

For i = lst2.ListCount - 1 to 0 step -1
    If lst2.Selected(i) = True Then
        lst2.RemoveItem(i)
    End If
Next
Laoighis answered 7/2, 2013 at 7:8 Comment(5)
I like this. When looping through collection (especially deletes) you should go backwards.Acetum
When deleting multiple items, you must go backwards to do it properly :) For example consider a list like ["Jim", "Steve", "Bob", "Mary"] For i = 1 to 4 lst(i).Delete Next This will work when i = 1, Jim will be deleted. But it resizes the list and now when When i = 2 that will delete Bob. The original second item in the list, Steve, will never be deleted if you're stepping forwards. And once you get to i = 3, you will get an error since at that point the list consists of only 2 remaining items (Steve, Mary)Laoighis
I voted you up for backwards, but unfortunately, I do not think it will work in this case.Wilbourn
I see your answer -- if the RemoveItem de-selects all of the other selected items then you are right, this method will not work (in other applications RemoveItem does not de-select the other items).Laoighis
@DavidZemens I tried your code in Excel and it works perfectly. RemoveItem does not de-select any other item in excelBarbabra
L
0

Same Concept as @Fionnuala with the exception that I used a collection which I generally find more flexible:

Dim i As Integer
Dim listColl As Collection
Dim Item As Variant

Set listColl = New Collection
With Me.listAvailable
    ' Add Selected Items to Collection (MultiSelect Listbox)
     For i = 0 To .ListCount - 1
        If .Selected(i) Then
            listColl.Add .ItemData(i)
        End If
    Next i
End With

For Each Item In listColl
    Me.listSelected.AddItem Item
    Me.listAvailable.RemoveItem Item
Next Item
Lussi answered 24/8, 2017 at 18:43 Comment(0)
G
0

I used this Code it works fine

Dim ArraySlctd() As Variant  'this Arry is to save what row is selected because when remove any row all selected are getting false
ReDim ArraySlctd(0 To Me.List1.ListCount - 1)
For lp = 0 To Me.List1.ListCount - 1 '
ArraySlctd(lp) = Me.List1.Selected(lp) 'work in the same range as the selected property
Next lp 

Now its simple to use

For lp = 0 To Me.List1.ListCount - 1
If ArraySlctd(lp) = True Then
'Remove Or Change(by remove and AddIten with the same Index)
End If

Next lp
Garvey answered 9/7, 2020 at 22:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.