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

x
?
Solved

How do i clear a collection in vba

Posted on 2009-04-01
19
Medium Priority
?
5,781 Views
Last Modified: 2012-05-06
In Visual Basic there seems to be a collection.clear() command but not in Visual Basic for Applications.
Anyone know how to do it in vba?
0
Comment
Question by:JoseJalapeno
[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
  • 7
  • 5
  • 3
  • +1
19 Comments
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036698
You'll need to use the Remove method to remove each item in the collection. The below function shows an example.

Wayne
Public Function ClearCollection(ByRef coll As Collection)
     Do
          coll.Remove (1)
     Loop While coll.Count > 0
End Function

Open in new window

0
 

Author Comment

by:JoseJalapeno
ID: 24036735
Thanks for that.
One more question though:
If all you do is empty the items surely the collection still exists. If you set a new collectio you end up with 2
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036751
>>If all you do is empty the items surely the collection still exists.

Yeah, sure, but you didn't want to dispose of the collection - simply clear the items. To dispose of a collection, use this....

    Set coll = Nothing

>>If you set a new collectio you end up with 2

Do you mean if you do this to an already instantiated collection?

    Set coll = New Collection

If so, then no. The old collection is disposed of.

Wayne
0
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!

 
LVL 17

Expert Comment

by:ExcelGuide
ID: 24036754
sorry for the offtopic, but webtubbs I love that mask!!! :)
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036760
Yeah, the folks in the office are into the April Fool's spirit :)
0
 

Author Comment

by:JoseJalapeno
ID: 24036785
OK sorry to appear to have a single brain cell but...
...i understood that = New Collection meant a new instance of the collection, same as a new instance of an object, am i wrong?
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 24036804
You are not wrong. Using "Set coll = New Collection" sets the collection *variable* to a new collection. The previous instance of the collection is no longer "connected" to a variable and is released from memory.
0
 

Author Comment

by:JoseJalapeno
ID: 24036885
How come then if you open multiple instances of a form you use frm = new Form_X which produces a new copy of the form. The previous one does not dissapear.
Is it a different set of rules for collections?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24037088
Userforms are a little bit odd. You have to actually close them, either manually or in code.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24037109
This is basically because they are added to the Userforms collection which maintains a reference to them until they are unloaded.
0
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 total points
ID: 24037486
I don't mean to be rude, but I answered your question, "How do i clear a collection in vba" in my first comment.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 24037543
I agree. Can you reopen it, or do we need a VB ZAPE?
0
 

Author Comment

by:JoseJalapeno
ID: 24037743
Sorry but when i clicked on "Accept as Solution" against webtubbs it did not offer me the opportunity to assign any points.
If i am doing something wrong please educate me.
0
 

Author Comment

by:JoseJalapeno
ID: 24038208
I didn't want to assign points to more than one person.
I accepted webtubbs solution, or at least i thought i had.
How then do points get awarded to one person?
0
 

Author Comment

by:JoseJalapeno
ID: 24038222
Also can you tell me where the help is for assigning points as i must have missed it.
0
 

Author Closing Comment

by:JoseJalapeno
ID: 31565206
Apologies webtubbs, genuine mistake.
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 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…
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

649 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