Find current index of a key in a VB collection

Posted on 2002-05-24
Medium Priority
Last Modified: 2010-08-05

I have a collection

dim bike as new collection

bike.add "red","red_key"
bike.add "gren","green_key"
bike.add "blue","blue_key"
bike.add "purple","purple_key"
bike.add "yellow","yellow_key"


rr = bike("green_key")

will return green


rr = bike(2)

will also return green.

What I would like to do is

somehow find the current index value for a key.

For example: T

he above collection has

the key "purple_key" sitting at index 4
I would like to be able to do something like

indexpurplekey = bike("purple_key").index

How can I get the current index value for a key?


Question by:bgumble
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
LVL 10

Expert Comment

ID: 7033471
Without itterating through each one, I'm 99% sure you can't. But then there may be a 'backdoor' way of doing this, as there seems to me no reason why this value shouldn't be available.
LVL 16

Expert Comment

ID: 7033752
Function FindInCollection(sKey As String, col As Collection) As Long
Dim o As Long
For o = 1 To col.Count
    If col.Item(o) = col.Item(sKey) Then
        FindInCollection = o
    End If
Next o
End Function
LVL 16

Accepted Solution

Richie_Simonetti earned 200 total points
ID: 7033758
Sorry, FOLLOWING smegghead's comment:

Function FindInCollection(sKey As String, col As Collection) As Long
Dim o As Long
For o = 1 To col.Count
    If col.Item(o) = col.Item(sKey) Then
        FindInCollection = o
        Exit For
    End If
Next o
End Function
Technology Partners: 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!


Expert Comment

ID: 7034220
LVL 22

Expert Comment

ID: 7034304
To do exactly that: indexpurplekey = bike("purple_key").index

you will need to create a Class Module called, for example, MyBike. This can be NonPublicCreatable. This module contains all properties you want, thus index.

private iIndex as integer
Public property Get Index() As integer
   Index = iIndex
End Property

Friend property Let Index(ByVal mIndex as integer)
   iIndex = mIndex
End Property

Next thing you do is create another class module which will be your collection. Code would look like:

Private mColl as Collection

Private Sub Class_Initialize()
   Set mColl = new Collection
End Sub

Public Sub Add( ...)

End SUb

Public Sub Remove( ...)

End SUb

and all the other functions. This will be your collection which you can also use within a For Each Next loop. Last function you have to add will take care of that and make it a collection.

Public Property Get NewEnum() As IUnknown
   On Error Resume Next
   Set NewEnum = mCollection.[_NewEnum]
End Property

Now go to the ObjectBrowser, select the current project and find the NewEnum() function. Go to the properties -> advanced and set the property id to -4. Also hide the member.

Now you have to add code to the add remove etc functions, you can also add other functions. basically you use mColl to actually add the other items, but underneath the hood you will create an object myBike with all properties set your way which you then add to the collection.

The above is a pretty long story but the best that I know of. You can also do it much simpler by only creating a myBike class which you add to your own collection, but then you have to set the index each time yourself while with a self made collection you can do this programmatically.


Expert Comment

ID: 7034645
sure you can do with an object.
but sometimes a cigar is just a cigar and a string is just a string ;-)

Expert Comment

ID: 7034646
i want to have hashes in vb!!!
LVL 49

Expert Comment

ID: 7818539
Hi bgumble,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Richie_Simonetti's comment(s) as an answer.

bgumble, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer

Expert Comment

ID: 7909953
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

764 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