Solved

How do i clear a collection in vba

Posted on 2009-04-01
19
5,722 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
  • 7
  • 5
  • 3
  • +1
19 Comments
 
LVL 47

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 47

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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

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

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 47

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 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…

820 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