removing items from a listbox (Acess 2002)

P

pietlinden

Sorry to ask a stupid question, but I checked the NG and can't get any
of the examples to work (like Steve Arbaugh's). I have two listboxes
and I'm trying to move the items from one side to the other. IOW, copy
item from lbxLeft to lbxRight, and remove the same item from lbxLeft.
The AddItem method works a champ, but no matter what I do, I can't get
the items to remove.

I thought I could do something like

Private Sub MoveSelected(ByVal lbxFrom As ListBox, ByVal lbxTo As
ListBox)

Dim varItem As Variant
Dim arrSelected(0 To 1) As Integer
Dim intIndex() As Integer '--copy selected to destination listbox

For Each varItem In lbxFrom.ItemsSelected
lbxTo.AddItem lbxFrom.ItemData(varItem)

Next varItem

'--remove from source lbxFrom (straight from the help file...)
For Each varItem In lbxFrom.ItemsSelected
lbxFrom.RemoveItem Index:=varItem
Next varItem

End Sub

For some reason, only the first item gets removed. Is this a case
where I have to iterate backwards because the collection is decreasing
in size or something?

Sorry to ask such a stupid question, but for the life of me, I can't
figure out how to get it to work. I'm sure it's the fact that the
index is changing after each loop...

So, I've checked the help file, the NG, tested a few "logical" tests...
and no joy. (Maybe this instance of clsProgrammer has a memory leak?)

Thanks for any pointers.

Pieter
 
P

pietlinden

for grins, I tried using the example to remove an item from a listbox
from the help file...

Function RemoveListItem(ctrlListBox As ListBox, _
ByVal varItem As Variant) As Boolean

' Trap for errors.
On Error GoTo ERROR_HANDLER

' Remove the list box item and set the return value
' to True, indicating success.
ctrlListBox.RemoveItem Index:=varItem
RemoveListItem = True

' Reset the error trap and exit the function.
On Error GoTo 0
Exit Function

' Return False if an error occurs.
ERROR_HANDLER:
RemoveListItem = False

End Function

but no joy. It still only removes the first item. Is it the way I'm
doing the loop that's wrong? (Must be!)

dim varItem as variant
for each varItem in lbx.ItemsSelected
ctrlListBox.RemoveItem Index:=varItem
next varItem

Perplexed...

Pieter


Terry, did I miss anything? Or am I just another instance of the
clsProgrammer class?
 
P

pietlinden

Van said:
See if the following Microsoft Knowledge Base article helps:

http://support.microsoft.com/kb/209878

Okay, thanks... I'll check it out. So you mean that the RemoveItem
method of the listbox control doesn't work in Access2002? (I ask
because the MS answer of updating a table is WAAAY roundabout.) I
wanted to use a listbox because I can search them and then iterate
through the itemsSelected collection to process them. (I was going to
use it to export items, like query results).

Pieter
 

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