Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Removing objects from a collection based on index key

Posted on 2006-07-14
7
Medium Priority
?
174 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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

597 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