ACC2002 - RemoveItem from MultiSelect ListBox

T

Tony_VBACoder

In Access 2002, does anyone know the best method for
removing selected items from a Multi-Select ListBox?

On my Form, I have one Multi-Select ListBox and one button
that allows the user to delete the selected item(s) from
the ListBox. I have tried to following 2 methods to
remove the selected items from the list box. However, in
my looping routine, when I issue the .RemoveItem command,
all the items that were selected in the Multi-Select list
box, become unselected, thereby leaving no items
selected. For instance, if the user has 5 items selected,
the first time the .RemoveItem code is issued, all the
items become deselected, leaving me with no items selected.

Private Sub btnRemoveTo_Click()
Dim vItem As Variant, iRow As Integer
' Remove all the items selected from the list box.
With Me
If .listSendTo.ItemsSelected.Count = 0 Then
Exit Sub
Else
' METHOD #1
For iRow = 0 To .listSendTo.ListCount - 1
If .listSendTo.Selected(iRow)
Then .listSendTo.RemoveItem iRow
Next

' METHOD #2
For Each vItem In .listSendTo.ItemsSelected
.listSendTo.RemoveItem vItem
Next
End If
End With

End Sub
 
T

Tony_VBACoder

I was able to get it to work by utilizing an Array where I
would store all the indexes of the items selected in the
list box. I would first loop through all the Items
Selected and store their indexes in the Array. Then I
would loop through the Array and remove each item from the
List Box, working from the bottom of the List Box to the
Top, based on the value in the Array. My updated Click
event is below. If someone knows of a better method,
please let me know.

Private Sub btnRemoveTo_Click()
Dim iRow As Integer
Dim arrayTo() As Integer, iIndex As Integer

' This will remove all the items selected from the list
box.
' First, check to see if any items were selected.
' Second, we will store all the items selected's index in
an Array
' Third, we will loop through the Array and remove each
item from the listbox.

With Me
If .listSendTo.ItemsSelected.Count = 0 Then
Exit Sub
Else
' Resize the Array to the number of selected items.
ReDim arrayTo(.listSendTo.ItemsSelected.Count)
' Loop through the items in the listbox and store
the
' position of the selected item in the Array
For iRow = 0 To .listSendTo.ListCount - 1
If .listSendTo.Selected(iRow) Then
arrayTo(iIndex) = iRow
iIndex = iIndex + 1
End If
Next
' Loop through the Array and remove the selected
Item(s) from the listbox.
' Start at the bottom of the listbox because when
we remove an
' item from the list box, its position will change
within the list.
For iIndex = .listSendTo.ItemsSelected.Count - 1
To 0 Step -1
.listSendTo.RemoveItem arrayTo(iIndex)
Next
End If
End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top