Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Erasing Collection

Posted on 2000-04-18
9
Medium Priority
?
209 Views
Last Modified: 2010-05-02
Hi,
I want to erase the contents of a collection but this doesn't work, just say TheCollection already stored 23 entrees

For X = 1 To TheCollection.Count
    TheCollection.Delete(X)
Next X

Can someone give me code that would work
0
Comment
Question by:adam8
[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
  • 4
  • 4
9 Comments
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 200 total points
ID: 2728658
Don't remove them using X as the item to remove.  Instead, just remove the first item in the collection each time (there will always be a first item)

Also, shouldn't this be .Remove, not .Delete?

For X = 1 to TheCollection.Count
  TheCollection.Remove 1
Next X
0
 
LVL 1

Author Comment

by:adam8
ID: 2728686
sorry,
it is delete, i just got mixed up.
The problem is that i store info to the collection until the user changes his/her selection, then if that happens i need to remove every item in the collection and then add different info to the collection the second time.
Get it, i need to remove all the items in the collection, not just the first one.
0
 
LVL 1

Author Comment

by:adam8
ID: 2728693
I think it is delete,
It doesn't matter anyway,
I know what you are talking about
0
Independent Software Vendors: 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 1

Author Comment

by:adam8
ID: 2728698
it is very important that the entire contents of the collection is deleted.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2728704
The .Delete versus .Remove doesn't matter - it could be version differences.   Just go with removing the first entry each time in your for...next loop, and you'll be fine.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2728711
The reason this will work:

Assume you have five items in your collection.  When you delete the first item, the 2nd item is automatically renumbered to be the first item, the 3rd becomes the 2nd, etc.

Given a collection:

1  2  3  4  5
After deleting #1, your collection is:

1  2  3  4
After deleting #2, your collection is:

1  2  3

Etc.
0
 
LVL 1

Author Comment

by:adam8
ID: 2728745
ok, i forgot to think about that.
so how would i go about deleting these items.

For X = 1 to Items.Count
    Items.Delete(1)
Next X

Would that work?
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2728771
That should be just perfect.
0
 
LVL 28

Expert Comment

by:Ark
ID: 2728828
Hi
For collection it's better to use For Each statement:
Dim X As Variant, TheCollection As Collection
For Each X in TheCollection
    TheCollection.Remove (1)
Next X
BTW, if your collection has delete method, may be it has clear method?
Cheers
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…
Suggested Courses

604 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