Solved

Removing objects from a collection based on index key

Posted on 2006-07-14
7
166 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I'd like the points.  In the words of the question asker: "Cool, that would work"
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…

762 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

7 Experts available now in Live!

Get 1:1 Help Now