Solved

Removing objects from a collection based on index key

Posted on 2006-07-14
7
169 Views
Last Modified: 2010-04-30
Help!  Pulling my hair out 6pm Friday!! - Can you help me?

I have a collection which is a series of objects. They contain some information containing an identifer (deal number) and a date.  I want to remove some based on a date specified.  for example, remove all deals that are before April.

in the example I have
Deal Number  Date
1                   Jan
2                   Feb
3                   Mar
4                   Apr
5                   May

I did have this bit of code
If cDealDetail.StartDate < plngStartDate Then
       CDealDetails.Remove cDealDetail.DetailNum
End If

Which when I had the following code in the remove section of the collection
Public Sub Remove(vntIndexKey As Variant)
    'used when removing an element from the collection
    'vntIndexKey contains either the Index or Key, which is why
    'it is declared as a Variant
    'Syntax: x.Remove(xyz)
    mCol.Remove vntIndexKey
End Sub

I thought that this would work a treat, but of course (d'oh) the index changes as the objects removed. So it works fine for dealnumber 1, but after this is removed, the index of deal number 2 is now 1, and therefore it deletes the wrong one!

Anyone got any ideas how I would code this is a different (ie working) way!!

Thank you so much
Zoe
0
Comment
Question by:zoebracken
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
dancebert earned 500 total points
ID: 17111313
I'm guessing that you're iterating the collection from member 1 to the last member, which is why the index number is decrementing (i.e., index 2 becomes 1).  Instead, iterate the collection from the last member to the first.  
0
 

Author Comment

by:zoebracken
ID: 17111361
Cool, that would work....I was doing a simple for each object in collection though, I guess that I will have to write my own procedure to iterate round....seems a bit nasty though, is there not a cleaner way?
Zoe
0
 
LVL 9

Expert Comment

by:dancebert
ID: 17111906
Remove works on Index or Key.  If you store Key in the object you can then read the Key and then Remove based on that.
0
 
LVL 9

Expert Comment

by:dancebert
ID: 17299887
I'd like the points.  In the words of the question asker: "Cool, that would work"
0

Featured Post

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!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

685 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