remove multiple listbox items (1 Viewer)

spinkung

Registered User.
Local time
Today, 08:59
Joined
Dec 4, 2006
Messages
267
Hi

i have an multi-select (extended) value list listbox which i want to be able to remove multiple items from using a button.

this code removes the first selected item but then deselects every other selected item so nothing else is removed.

can someone advise what i'm doing wrong?

Code:
 'loops through ListBox and removes items from list 2
    For lbx_Sel = 0 To lst2.ListCount - 1
        If lst2.Selected(lbx_Sel) = True Then
            lst2.RemoveItem lbx_Sel
        End If
    Next

thanks
 

jdraw

Super Moderator
Staff member
Local time
Today, 03:59
Joined
Jan 23, 2006
Messages
15,385
Just a suggestion, but try changing the order of the Loop. Go from top to bottom, so to speak.

For lbx_Sel = lst2.ListCount - 1 To 0 Step -1

I had a similar issue trying to remove attachments from emails in Outlook. Reversing the order of the Loop works -- I think because you have removed data on each loop, so the index gets "messed up".
 

spinkung

Registered User.
Local time
Today, 08:59
Joined
Dec 4, 2006
Messages
267
thanks

it removed the last selected item but still unselected everything else which was selected??
 

VilaRestal

';drop database master;--
Local time
Today, 08:59
Joined
Jun 8, 2011
Messages
1,046
You could do it with an array:

Code:
    Dim iSelectedIndexes() As Long
    Dim iSelCount As Long
    iSelCount = lst2.ItemsSelected.Count - 1
    ReDim iSelectedIndexes(iSelCount)
    Dim i As Long, j As Long
    For i = 0 To lst2.ListCount - 1
        If lst2.Selected(i) = True Then
            iSelectedIndexes(j) = i
            j = j + 1
        End If
    Next
    For i = 0 To iSelCount
        lst2.RemoveItem iSelectedIndexes(iSelCount - i)
    Next
 

VilaRestal

';drop database master;--
Local time
Today, 08:59
Joined
Jun 8, 2011
Messages
1,046
Or slightly improved version (only loops through selected items to build ip the array, not all items):

Code:
    Dim iSelectedIndexes() As Long
    Dim iSelCount As Long
    iSelCount = lst2.ItemsSelected.Count - 1
    ReDim iSelectedIndexes(iSelCount)
    Dim itm
    Dim i As Long
    For Each itm In lst2.ItemsSelected
        iSelectedIndexes(i) = itm
        i = i + 1
    Next
    For i = 0 To iSelCount
        lst2.RemoveItem iSelectedIndexes(iSelCount - i)
    Next
 

spinkung

Registered User.
Local time
Today, 08:59
Joined
Dec 4, 2006
Messages
267
perfect, that works fine.

just had to wrap it in this..

Code:
If lst2.ListCount > 0 Then
...
End If

to avoid an error if the list is empty.

Thanks
 

jdraw

Super Moderator
Staff member
Local time
Today, 03:59
Joined
Jan 23, 2006
Messages
15,385
Glad you got it working..
 

monfas

Registered User.
Local time
Today, 10:59
Joined
Jun 18, 2012
Messages
32
Hi,

I was trying to use this method to remove items from a list box, however an error message comes and say "run-time error 6014: the rowsource type property needs to be set to 'Value list' to use this method"

how do i set this rowsourcetype property? I tried in the listbox properties, and doesn't work... can anyone help me on this? What I am doing wrong here?

my code is

Private Sub selectVRF_FM_Click()
Dim iSelectedIndexes() As Long
Dim iSelCount As Long

iSelCount = L1_vrf.ItemsSelected.Count - 1
ReDim iSelectedIndexes(iSelCount)
Dim itm
Dim i As Long
For Each itm In L1_vrf.ItemsSelected
iSelectedIndexes(i) = itm
i = i + 1
Next
For i = 0 To iSelCount
L1_vrf.RemoveItem iSelectedIndexes(iSelCount - i)
Next

End Sub
 

VilaRestal

';drop database master;--
Local time
Today, 08:59
Joined
Jun 8, 2011
Messages
1,046
Yes, it's in the listbox properties on the data tab. It's not the default rowsource type and I don't think it will let you change it to Value List if the rowsource is a table or query.

To remove items from a Table/Query type listbox change the rowsource to a filtered version.
 

Users who are viewing this thread

Top Bottom