Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing Items From ListBox 1

Status
Not open for further replies.

wtmckown

Programmer
Mar 19, 2003
121
US
I am unable to remove multiple items from a listbox. Here's what I've tried in Access2002/Windows2000:

**Note** lstTo is the name of the listbox control
Dim iX As Integer

For iX = lstTo.ListCount - 1 To 0 Step -1

If lstTo.Selected(iX) Then lstTo.RemoveItem iX

Next iX

This only removes the last selected item. It seems to set all other selected items to a non-selected state after removing the last selected item in the list.

Also tried this:

Dim varI as variant

For Each varI In lstTo.ItemsSelected()
lstTo.RemoveItem lstTo.ListIndex
Next

Same result.......Only removes the first item in ItemsSelected and then jumps the loop as if no other items are selected. If I step this code without the RemoveItem line then I can loop through all items that are selected. Any assistance will be greatly appreciated.
 
create a one-dimensional array and populate it with the listindex values of the selected rows on the listbox. then remove items from the listbox using an index based on the array, which will not reset itself when the first item is removed.

here is an example where i pull items out of one listbox and add them to another (used in many user interfaces where the user can select any combination of items from a finite list of choices):

Option Compare Database
Option Explicit

'///////////////////////////////////////////////////////////////////////////////////////////////

Sub subMoveListItems(FromList As ListBox, ToList As ListBox)

' If item(s) have been selected:

If FromList.ItemsSelected.Count <> 0 Then

' Declarations:

Dim strItem As String ' hold data of item being moved
Dim i As Variant ' counter
Dim n As Integer ' counter

ReDim ItemArray(FromList.ItemsSelected.Count - 1) ' array to list selected item(s)

' Add item(s) to ToList:

For Each i In FromList.ItemsSelected

' Set strItem = to the item selected to move:

strItem = FromList.ItemData(i)

' Add row to the list the item is being moved to:

ToList.AddItem strItem

' Add index number of item i to ItemArray (adjusted for removal of items from FromList -
' and it's index - by subtracting n)

ItemArray(n) = i - n

' Increase n by one:

n = n + 1

Next i

' Remove Item(s) from FromList:

For n = 0 To UBound(ItemArray)

' Remove item with index = n

FromList.RemoveItem ItemArray(n)

Next n

End If

End Sub

'///////////////////////////////////////////////////////////////////////////////////////////////

 
to make using that peice of code really simple, you can just call it from whatever event you are now using to remove items from your list like so:

Call subMoveListItems(me.lstTo, [list to add items to])

if you don't need to add items to any list you can always remove the additem-related code and second argument from subMoveListFiles, calling it like so:

Call subMoveListItems(me.lstTo)
 
When you remove an item from the from listbox based on the index of the array then doesn't the listbox index change(shift by one position) making additional removes in the same loop get 'out of sync' with the rest of the array that is based on the positions of the listbox items before any removal?
 
yes it does. this is why i have the line

ItemArray(n) = i - n

instead of

ItemArray(n) = i

the &quot; - n &quot; part compensates for the shift in listindex values as items are removed. i've tested this and use it in a couple of production forms and it works exactly as it should. (by the way, i beleive you could avoid even that little piece of compensatory code if you run the removal process from the ubound of the array down to zero instead of the other way around, but that requires specifying a different step (-1 instead of 1) size from the default and writing something more like For i = ubound(itemarray) to 0 ... all the rest of the code. just felt like throwing that in for the sake of being complete)
 
You were right about removing the items in descending order to avoid the index reset. Thanks for your help. I only needed to be able to remove items from a single listbox so I used a variation of your code. I might as well post it for others...........

Dim iX As Integer
Dim iY As Integer
Dim aListBoxIndex() As Integer

If lstTo.ItemsSelected.Count = 0 Then GoTo Exit_cmdFrom_Click

For iX = 0 To lstTo.ListCount - 1
If lstTo.Selected(iX) Then

If iY > 0 Then
ReDim Preserve aListBoxIndex(UBound(aListBoxIndex) + 1)
Else
ReDim aListBoxIndex(0)
End If

aListBoxIndex(iY) = iX
iY = iY + 1

End If
Next


For iX = UBound(aListBoxIndex) To 0 Step -1
lstTo.RemoveItem aListBoxIndex(iX)
Next

Erase aListBoxIndex
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top