removeitem in listbox

G

Guest

Hi ALL I need some help please

I am trying to use removeitem method in a listbox. It is a extended list
and I need to be able to remove the selected Items (Not clear the listbox). I
got the additem to work fine in the listbox. I have tried something like
the code below but it only deletes the last value. I know this has something
to due with the listbox array but just do not know how to solve. Example
code is appreciated
Thanks in advance.


Dim varitem As Variant

For Each varitem In Me.List0.ItemsSelected

List3.RemoveItem Index = (variitem)
Next
 
R

RoyVidar

Dave said:
Hi ALL I need some help please

I am trying to use removeitem method in a listbox. It is a extended
list and I need to be able to remove the selected Items (Not clear
the listbox). I got the additem to work fine in the listbox. I
have tried something like the code below but it only deletes the
last value. I know this has something to due with the listbox array
but just do not know how to solve. Example code is appreciated
Thanks in advance.


Dim varitem As Variant

For Each varitem In Me.List0.ItemsSelected

List3.RemoveItem Index = (variitem)
Next

Except that you're looping one listbox (List0) and removing items from
another listbox (List3), I'm not sure.

Well, I don't know if it applies to the ItemsSelected collection, but
when removing items from a collection, you'll be renumbering or
reindexing the collection, too, which makes it possible to error.

I e, removing index 42 will cause item with index 43 to become 42 ...
so you'll need to "start at the end"

Perhaps assign to an array? Air code

Dim varitem As Variant
dim arr() as long
dim l as long

redim arr(Me.List0.ItemsSelected-1)
For Each varitem In Me.List0.ItemsSelected
arr(l) = clng(varitem)
l = l + 1
Next
for l = ubound(arr) to 0 step -1
Me.List0.RemoveItem index:=arr(l)
next l

Or if you loop from the end of the listbox, and check if the item is
selected - air code

For l = Me.List0.ListCount - 1 to 0 Step - 1
If Me.List0.Selected(l) = true Then
Me.List0.RemoveItem index:=l
end if
next l
 
G

Guest

Thanks for helping Roy, I found a solution. Here it is:

I take the unselected records and populate them in an array
I then delete the entire listbox
Repoplate the list with the unselected records from the array


Dim Db As Database
Set Db = CurrentDb
Set ctrlListBox = Forms!SelectRecords!List4

' Create the Array
Dim ArrSize As Integer
Dim i As Integer
Dim TempFilesArray()


'Get the count
ArrSize = 0
For i = 0 To ctrlListBox.ListCount - 1
If ctrlListBox.Selected(i) = False Then
ArrSize = ArrSize + 1

'Keep the array and populate with unselected item

ReDim Preserve TempFilesArray(ArrSize)
TempFilesArray(ArrSize) = ctrlListBox.ItemData(i)
End If
Next

'remove the items

For i = 0 To ctrlListBox.ListCount - 1
ctrlListBox.RemoveItem (0)
Next i

'Add Items back in from array

For i = 1 To ArrSize
ctrlListBox.AddItem TempFilesArray(i)
Next
 

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