Solved

Removing item from Listbox Using ItemData

Posted on 2004-09-24
5
151 Views
Last Modified: 2010-05-02
Basically I have two listboxes. When the user clicks the add button, selected items are removed from one and added to the other, it works both ways.

I have no problem adding selected items from one listbox to the other but the problem comes when I try to remove an item. Here is the code:
-----------------------------------------------------------
Dim index As Integer

For index = 0 To ListTables.ListCount - 1

If ListTables.Selected(index) = True Then
    ListSelectedTables.AddItem (ListTables.List(index))
    ListTables.RemoveItem (index)
End If

Next index
-----------------------------------------------------------

The problem occurs after an item is removed. I get an "Invalid propery array index" error when the index is referenced after. I believe that the problem has to do with the fact that the listcount and indexes are changed once an item is removed, but it is still in a loop. Anyway, I know that the item data property is used so that a specific number is assigned to each item so that it does not matter what order they are in and can still be referenced. Problem is I can not remember how to use it through code.

Can someone please give a quick example of using item data to reference items in a listbox and/or if there is a better way without using it.

This question is urgent, thank you.

0
Comment
Question by:fisherbrsnch00
  • 3
  • 2
5 Comments
 
LVL 10

Expert Comment

by:anv
ID: 12149201
u need to change your loop use the following instead

dim LstCountas integer

LstCount = ListTables.ListCount - 1

For index = 0 To LstCount

If ListTables.Selected(index) = True Then
    ListSelectedTables.AddItem (ListTables.List(index))
    ListTables.RemoveItem (index)
End If

LstCount = LstTables.ListCount - 1
Next

The LstTables.Listcount That u use in the for loop doesnot change even if u r removing items..either u use While loop or use above loop for ur code and test it..
0
 

Author Comment

by:fisherbrsnch00
ID: 12149231
With that code I get the same error
0
 
LVL 10

Accepted Solution

by:
anv earned 500 total points
ID: 12149246
use the statement

For index = 0 To LstCount

If index >= LstCount Then Exit For

If ListTables.Selected(index) = True Then
    ListSelectedTables.AddItem (ListTables.List(index))
    ListTables.RemoveItem (index)
End If

LstCount = LstTables.ListCount - 1
Next
0
 
LVL 10

Expert Comment

by:anv
ID: 12149250
Missed the statement

If index >= List1.ListCount - 1 Then Exit For

Sorry..:)
0
 

Author Comment

by:fisherbrsnch00
ID: 12149280
Thank you, works great!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now