[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

RomoveItem

Posted on 2000-03-10
7
Medium Priority
?
166 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Accepted Solution

by:
paulstamp earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Suggested Courses

656 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