Solved

RomoveItem

Posted on 2000-03-10
7
162 Views
Last Modified: 2010-05-02
I am trying to remove an item form a list box by selecting the item and clicking a button with the following code.  If I click on any item in the list other then the last one I get a error 381 (invalid property array index). Please help.

Private Sub cmdDelete_Click()
For intcounter = 0 To lstMain.ListCount - 1
If lstMain.Selected(intcounter) = True Then
lstMain.RemoveItem (intcounter)
End If
Next intcounter
End Sub
0
Comment
Question by:mad3654
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 1

Expert Comment

by:imurphy76
ID: 2606871
Try this instead:

Private Sub cmdDelete_Click()
lstMain.RemoveItem lstMain.ListIndex
End Sub
0
 
LVL 1

Expert Comment

by:imurphy76
ID: 2606887
Private Sub cmdDelete_Click()
lstMain.RemoveItem lstMain.ListIndex
End Sub

0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2607413
The correct way to remove the selected items from a multiselect-able listbox is using code like

   For intcounter = (lstMain.ListCount - 1) To 0
      If (lstMain.Selected(intcounter)) Then
         Call lstMain.RemoveItem(intcounter)
      End If
   Next

If you want to know why you got that problem with your old code.. it's because let's say you have a list with 3 items. You select the first item and then run the code. You first checks the first item, you see it is selected and removes it. Now you have a list of 2 items, and you check the 2nd item in the new list. Then you try to check the 3rd item (because you think that the list is 3 items) but there aint no such item and therefore you get that OutOfRange error.

Hope this helps.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
paulstamp earned 50 total points
ID: 2607860
A simple remedy for your code (assuming single select in your listbox) is to add Exit For into your loop :

Private Sub cmdDelete_Click()
For intcounter = 0 To lstMain.ListCount - 1
If lstMain.Selected(intcounter) = True Then
    lstMain.RemoveItem (intcounter)
    Exit For
End If
Next intcounter
End Sub

0
 
LVL 3

Expert Comment

by:paulstamp
ID: 2607864
VBMaster.. don't you need a STEP -1 in your loop ??
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2607883
Oops! Ye, to mad[blaha]: you need to add " Step -1" at the end of the For statement if you use the code from me.
0
 

Author Comment

by:mad3654
ID: 2613953
Thanks for the explaination also.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

685 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