Solved

RomoveItem

Posted on 2000-03-10
7
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

696 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