?
Solved

Collections adn Keys

Posted on 2003-03-10
5
Medium Priority
?
195 Views
Last Modified: 2010-04-07
I am needing to know how to use Keys in respect to collections. how do they get assigned, how to use them etc...
0
Comment
Question by:sk33v3
[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
5 Comments
 
LVL 5

Expert Comment

by:JohnMcCann
ID: 8108121
First off i suggest you start a new vb project (exe)
Project->Add Class Module -> VB Class Builder
Click the icon Add new collection create from new class.

Update your project.

Look at the collection class.

Ok.

The key field is used to identify an object within a collection.

e.g.

Collection.add obj, "MYKey"

and retrieved using

Collection.Item("MyKey")

Collections can also be used as foolows

Collection.Item(1)

For each obj in Collection


Hope this is of some help

0
 
LVL 9

Author Comment

by:sk33v3
ID: 8108717
So the key can be any type of variable?
0
 
LVL 1

Accepted Solution

by:
irfanazam earned 320 total points
ID: 8108734
yes but there are two exceptions.

1). key cannot be a numeric value like 123.

2). key cannot contains a numeric value as its first character like 1MyKey.
0
 
LVL 3

Expert Comment

by:OblivionSY
ID: 8109857
this example shows a function I have used to add the values from a database (the recorset) into a collection. So my collection is a collection of officers whose information is stored in a "class module"

Basically, inst is the item I am adding to the collection, so inst has several variables set, the attributes of the officer, then the key line

AddToInstance.Add inst, CStr(inst.Warrant)

This adds the "inst" into the collection and uses the Warrant number (unique to each officer) as the key for that officer. Now I can use this KEY to reference any officer and find anyinformation i want to

msgbox "Their section is " & AddToInstance("W243").Section

Would result in a message box appearing looking in the collection, finding the record with the Key W243 and showing the user their section, eg "Their section is D"


Here is the function

Function AddToInstance(Rcst As Recordset) As Collection
'New instance of the officer class
Dim inst As New OfficersD
Set AddToInstance = New Collection
    Rcst.MoveFirst
    Do While Not Rcst.EOF
        'Assign database data to the instance
        inst.Warrant = Rcst!Warrant_
        inst.AFO = Rcst!AFO
        inst.DogHandler = Rcst("Dog Handlers")
        inst.SectionL = Rcst!Section
        inst.SectionNO = Rcst("Section & No")
        inst.Rank = Rcst!Rank
        'Add the instance into the collection
        AddToInstance.Add inst, "W" & CStr(inst.Warrant)
        Set inst = Nothing 'clear the data for the next run
        Rcst.MoveNext
    Loop
end Function
0
 
LVL 9

Author Comment

by:sk33v3
ID: 8123328
Ok that was my problem I was assigning numeric values. Thanks.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses

765 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