Solved

Removing objects from a collection based on index key

Posted on 2006-07-14
7
170 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 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

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.

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

734 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