?
Solved

Removing objects from a collection based on index key

Posted on 2006-07-14
7
Medium Priority
?
172 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
[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
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
dancebert earned 2000 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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

801 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