Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Collections adn Keys

Posted on 2003-03-10
5
Medium Priority
?
203 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
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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

571 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