Listbox + selected items only doing first item selected.

rolaaus

Registered User.
Local time
Today, 11:55
Joined
Feb 12, 2008
Messages
84
I had the following bit of code in a project I'm working on that worked fine, until another user in these forumns asked about removing items from a listbox, then I modified my form to do this, and it only removes the first item selected.

basically, I have 2 list boxes. The first has an unfiltered list, the 2nd listbox I want to fill with items that I have selected in the 1st list box. This was working fine, even with multi-select chosen. But when I add the removeitem line of code, now it only does the very first item selected then stops!

Any ideas?
Code:
For Each varItem In Me.lst_Files.ItemsSelected
    Me.lst_Holdings.AddItem Item:=Me.lst_Files.ItemData(varItem)
    Me.lst_Files.RemoveItem Me.lst_Files.ItemData(varItem)
Next varItem
 
i wouldnt have thought you could add/remove items in this way

i thought access modified the operation (ie this behaviour) of list/combo boxes to enable it to work with bound data sources.

if you are changing a value list, then i dont think you can do it that way either

what about trying to set a flag in the underlying data and using that to determine which list obx the item appears in. that is more normal, i would have thought
 
Hello rolaaus!

Look at "DemoListBoxA2000.mdb" (attachment).
Open Form1 and try, I think it can help you.
 

Attachments

The listbox is set to value list, and the 1st listbox contains a list of all files in a chosen directory (using the folder browser API). Then I only want to select certain files to go into a 2nd listbox (also value list). The 1st listbox (all files) is set to extended multi-select, and as I mentioned before the code works perfectly, until I start to do the removeitem line of code
Code:
Me.lst_Files.RemoveItem Me.lst_Files.ItemData(varItem)

Without this line, everything works just fine - all the files I select and click a push button are moved over to list #2. When I add this single line of code, it only moves the 1st item selected, and removes it, then stops - I placed breakpoints on the whole procedure and it only runs through the procedure one time.

Is there a way to cycle through all items in a listbox and see which ones are selected, rather than using the Itemselected collection?

I guess I shall be off to google this and see if there is other help that will shed some light on this - not that it matter until I helped someone else out here with filling a 2nd listbox with items from the first, I was perfectly content leaving the 1st list alone, but I realized how not so "clean" that is.
 
Look at "DemoListBoxA2000.mdb" (attachment).
Open Form1 and try, I think it can help you.

This demo isn't using a value list listbox, so I had to modify it and make the code behind form say pretty much exactly what I had already posted that my code is doing, and it did the same exact thing. Also, the code in this example is "unselecting" items that are selected, whereas I want them removed from the list (all of them, and not just the first item selected).
 
My assumption is that by removing one of the items in the list, the For Each statement gets confused. If the items in the list stay selected, but the one is gone, this I would think you could do the For Next code again (and again, and again) until there are no more items selected. I would suggest doing that in a While/Wend grouping. Of course, all this only if the selected items stay selected.
 
GolferGuy, I am thinking that maybe, by doing the first RemoveItem that the listbox loses all of it's selected items? That is the only thing I could think of that is happening, however, that doesn't seem correct either, but the code should cycle since the loop is initiated once on the first item selected, however, once I hit a removeitem, the loop just stops. It doesn't mattter if I use the Itemselected collection or if I cycle through all of the listbox items and check to see if me.listbox.selected(itemIDx) is true.

If my guess is correct, then I would have to cycle through the list and store the index values of those items selected, then go a 2nd cycle through all items doing a line by line remove based on the stored Index values. Can you think of a way to do this, I've never been very good at multi-valued variables, like intCount(1) intCount(2), etc. I probably have only used them once or twice in my 10+ years of programming.
 
Last edited:
Hello rolaaus!

As I can understand you, you need something like "Demo2ListBoxA2000.mdb", (attachment). Open Form1 and try.
 

Attachments

Rolaaus,
If you do a two pass thing, the second pass should be from the high end down to the low end. If you delete item 3, then you have lost all the items numbers above that. Because once you delete item 3, then item 4 becomes 3, 5 becomes 4, etc. But if you start at the high end, the only items that are put into a new location have already be dealt with, so the problems occur because of that. I did not take the time to look at MStef's attachment. If it looks like it will do for you what you need, use it! But please let us know how you solve this situation.
 
Hello rolaus!

Look at a new "DemoRemoveItemA2000.mdb".
I think it is what you want, (on your way).
The ROW SOURCE TYPE property of the list box must be set to "Value List".
Open Form1 and try.
 

Attachments

Last edited:

Users who are viewing this thread

Back
Top Bottom