Solved

Extract Key from a collection

Posted on 2004-10-15
4
911 Views
Last Modified: 2008-01-09
Hi,
I have maintained a collection in my Visual basic project using Collection Object. What I did is I inserted addresses of windows procedures of the controls of a form as elements in that collection and inserted handles of those controls as keys in that collection for the corresponding windows procedure addresses. Now, when I pass the key that is the handle of a control, I get the address of that controls. What I want is to extract key of collection also; either using index,
Please give some idea and/or code sample.

Thanks & Regards,
msjammu
0
Comment
Question by:msjammu
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 150 total points
Comment Utility
Using the standard collection object, there isn't any way to determine the key for an entry.  What you can do however, is create a class to store your data and include the key as one of the values.

' ---------------------------------------------------------
' Class1
' ---------------------------------------------------------
Public key As String
Public someValue As String

' ---------------------------------------------------------
' Form1
' ---------------------------------------------------------
Option Explicit

Private myCollection As Collection

Private Sub Form_Load()
    Set myCollection = New Collection
   
    Dim c As Class1
    Dim i As Integer
   
    For i = 1 To 5
        Set c = New Class1
        c.key = "Item" & i
        c.someValue = "Value" & i
        myCollection.Add c, c.key
    Next i
   
    For Each c In myCollection
        Debug.Print "someValue = " & c.someValue, "key = " & c.key
    Next c
   
    For i = 1 To myCollection.Count
        Set c = myCollection.Item(i)
        Debug.Print "index = " & i, "someValue = " & c.someValue, "key = " & c.key
    Next i
End Sub
0
 
LVL 50

Assisted Solution

by:Dave Brett
Dave Brett earned 100 total points
Comment Utility
You could try the Dictionary Object instead of a Collection. You can return the Keys

From VBA help

Keys Method
         

Description

Returns an array containing all existing keys in a Dictionary object.

Syntax

object.Keys

The object is always the name of a Dictionary object.

Remarks

The following code illustrates use of the Keys method:

Dim a, d, i             'Create some variables
Set d = CreateObject("Scripting.Dictionary")
d.Add "a", "Athens"     'Add some keys and items.
d.Add "b", "Belgrade"
d.Add "c", "Cairo"
a = d.keys              'Get the keys
For i = 0 To d.Count -1 'Iterate the array
    Print a(i)          'Print key
Next
...

Cheers

Dave
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
If you are going to use the Dictionary class I would add a reference to the "Microsoft Scripting Runtime" (Project -->References).  This way, you can declare as below and intellisense will work for you:

    Dim d As Dictionary
    Set d = New Dictionary

~IM
0
 
LVL 6

Author Comment

by:msjammu
Comment Utility
Thanks you Idle_Mind, brettdj
I will try both the ideas, and choose best that suit me
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

763 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

7 Experts available now in Live!

Get 1:1 Help Now