Solved

Finding the last item in a collection

Posted on 2003-11-07
4
156 Views
Last Modified: 2010-05-03
I have a collection call colCustomers with a key element CustomerID.  CustomerID is in the sequence 0001, 0002, 0003 etc... However sometimes a key may be skipped eg: 0001, 0002, 0004, 0005, 0006.  I need to be able to find out what the last CustomerID is so that I can create a new one that is one number higher.  I don't want to create in a missing spot and I don't want to resort the collection and change every CustomerID so non are skipped.

Thanks, Jebus
0
Comment
Question by:dj__jebus
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Sethi
ID: 9705768
Count the no. of items in the collection. The total count -1 would be the last item. example:

Collection.Item(Collection.Count-1)
0
 

Author Comment

by:dj__jebus
ID: 9705795
Well I have

NewCustomerID = colCustomers.Item(colCustomers.Count - 1).CustomerID + 1

and I get "This key is already asociated with an element of this collection" error.  I have tried this already and I think it does it because when a CustomerID is skipped the count is actually less than the highest CustomerID

Thanks, Jebus
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9706012
Why not just store the last used CustomerID externally in a variable and increment it?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 9706035
This is the major drawback of a collection...it takes a long time to find the last item since it has to walk the linked list item by item.  Also there is no way to do a reverse key lookup given an index.

An alternative scheme would be to store the last used customerID in the first spot in the collection!  The objects in a collection do not have to be of the same data type and it would not be hard to modify existing code to not use the first index.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

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

20 Experts available now in Live!

Get 1:1 Help Now