Solved

Using a collection

Posted on 2002-07-15
4
179 Views
Last Modified: 2010-05-02
What I want to do is iterate through a collection and if an object meets a certain critreion, delete that object from the collection.

for each myobject in mycollection

  if myobject.mytest = "deleteme" then mycollection.remove

next

It is the mycollection.remove that needs the element index and I don't know how to determine the current index.

TIA
0
Comment
Question by:Philip Pinnell
  • 2
4 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 7154031
for each myobject in mycollection

 if myobject.mytest = "deleteme" then mycollection.remove mycollection.Index

next
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7154041
Sorry that should have been:

for each myobject in mycollection

if myobject.mytest = "deleteme" then mycollection.remove myobject.Index

next
0
 
LVL 13

Author Comment

by:Philip Pinnell
ID: 7154051
Cor that was easy. I perhaps should have got it myself.

Thanks
0
 
LVL 17

Expert Comment

by:inthedark
ID: 7154067
First the object you are placing in the collection must have a unique name to be used as an ID.
You need to use the class builder wizard to add a collection class so that you can use the For Each syntax.
(Here are the steps: Projects-Add Class Module-Select VB Class builder-File-New Collection-Based on New Collection-BAsed on a new Class called Dummy. Now remove the dummy class.) Change the add method as follows:

Public Sub Add(YourObject As YourObjectName, Optional sKey As String)
   
    If Len(sKey) = 0 Then
        mCol.Add YourObject
    Else
        mCol.Add YourObject, sKey
    End If

End Sub


To Add New Items:

Dim YO as YourObject
Dim CS as Collection1

' create you collection
Set CS = New Collection1

' Creat a new object
Set YO= New YourObject
YO.ID = "Item1"
YO.etc.....
CS.ADD Yo, "Item1"

Set YO = Nothing ' this will kill YO but keep the instance in the collection

' now you can loop elements

For Each YO in CS
  If Yo.Obsolete Then ' whatever property indicates removal required
    CS.Delete YO.ID ' Pass a string value to indicate key to delete
  End If
Next











0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now