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
Solved

Extract Key from a collection

Posted on 2004-10-15
4
920 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
ID: 12327673
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
ID: 12331008
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
ID: 12331022
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
ID: 12336220
Thanks you Idle_Mind, brettdj
I will try both the ideas, and choose best that suit me
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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 In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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 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…

809 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