473,426 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

VBA collections question

Hello.

If I create a collection in VBA and populate it with entries
indexed by text keys, is there a way to go back and enumerate
the actual key values used?

For example:

'Create collection of really weak passwords, indexed by username
public cPW as new collection
cPW.add "dogs_name", "mark"
cPW.add "daughters_birthday", "john"
cPW.add "mypassword", "sally"

sub enum_users_from_collection(coll as collection)
dim sName as string
dim sMsg as string
'Collections have no "keys" property, of course
for each sName in coll.keys
sMsg = sMsg & vbcrlf & sName
next
msgbox sMsg,,"User Name List"
end sub

Thanks
-Mark

Feb 8 '06 #1
4 13816
"Mark" <no****@thanksanyway.org> wrote in message
news:Wt********************@w-link.net...
Hello.

If I create a collection in VBA and populate it with entries
indexed by text keys, is there a way to go back and enumerate
the actual key values used?

For example:

'Create collection of really weak passwords, indexed by username
public cPW as new collection
cPW.add "dogs_name", "mark"
cPW.add "daughters_birthday", "john"
cPW.add "mypassword", "sally"

sub enum_users_from_collection(coll as collection)
dim sName as string
dim sMsg as string
'Collections have no "keys" property, of course
for each sName in coll.keys
sMsg = sMsg & vbcrlf & sName
next
msgbox sMsg,,"User Name List"
end sub

Thanks
-Mark

No there is no way with a plain old vba collection. You could create your
own class to do this sort of thing or you could use something like the
Scripting.Dictionary object which is built for this sort of thing.
But why do you not simply want to use an array? If all you need to do is
build up a list of username/password pairs and then run through the list,
then an array would be fine. However, perhaps there are some extra features
you require...


Feb 8 '06 #2
"Anthony England" <ae******@oops.co.uk> wrote:

No there is no way with a plain old vba collection. You could create your
own class to do this sort of thing or you could use something like the
Scripting.Dictionary object which is built for this sort of thing.
But why do you not simply want to use an array? If all you need to do is
build up a list of username/password pairs and then run through the list,
then an array would be fine. However, perhaps there are some extra
features you require...


It's really a hypothetical, although Perl will give you the names of your
hash
keys if you know how to ask, so it isn't unprecedented.
Feb 8 '06 #3
On Wed, 8 Feb 2006 14:08:32 -0800, "Mark" <no****@thanksanyway.org>
wrote:

VBA doesn't expose this property, but "if you know how to ask" there
probably is a way. It would involve a lengthy low-level debugging
session, and several Windows API calls, I'm sure.
Scripting.Dictionary is a good alternative, though I have heard some
people argue against it because they consider scrrun.dll a security
risk. I'm not up to speed on all the ins and outs of that.

-Tom.

"Anthony England" <ae******@oops.co.uk> wrote:

No there is no way with a plain old vba collection. You could create your
own class to do this sort of thing or you could use something like the
Scripting.Dictionary object which is built for this sort of thing.
But why do you not simply want to use an array? If all you need to do is
build up a list of username/password pairs and then run through the list,
then an array would be fine. However, perhaps there are some extra
features you require...


It's really a hypothetical, although Perl will give you the names of your
hash
keys if you know how to ask, so it isn't unprecedented.


Feb 9 '06 #4
No, the easy way to handle this would be to create a collection of class
objects each object of which exposes it's own key.

e,g,
class module cPeople

' **********************
Option Explicit

Private Type MeProps
Value As String
Key As String
End Type

Private MP As MeProps

Property Let Value(RHS As String)
MP.Value = RHS
End Property

Property Get Value() As String
Value = MP.Value
End Property

Property Let Key(RHS As String)
MP.Key = RHS
End Property

Property Get Key() As String
Key = MP.Key
End Property
' **********************

Then fill the collection
' **********************
public cPW as collection

Dim cP as cPeople

Set cPW as New Colloection

set cp = new cpeople
with cp
.Value = "dogs_name"
.Key = "mark"
End With
cpw.add cp, cp.Key

set cp = new cpeople
with cp
.Value = "daughters_birthday"
.Key = "john"
End With
cpw.add cp, cp.Key

set cp = new cpeople
with cp
.Value = "mypassword"
.Key = "sally"
End With
cpw.add cp, cp.Key
set cp = nothing
' **********************

Then enumerate
' **********************
Dim o as Object
dim cP as cPeople

For each o in cPW
If TypeOf o is cPeople Then
Set cP = o
Debug.Print cP.Key
End if
Next
Set cP = nothing
Set o = nothing
' **********************

The really correct way would be to create a collection class in which you
could implement the usual Add, Count, Remove, Item and Enumeration
procedures.

The problem with this method is setting the procedure attributes for the
item and enumeration procs so that they work properly, but it can be done
(with a bit of hacking).

--

Terry Kreft
"Mark" <no****@thanksanyway.org> wrote in message
news:Wt********************@w-link.net...
Hello.

If I create a collection in VBA and populate it with entries
indexed by text keys, is there a way to go back and enumerate
the actual key values used?

For example:

'Create collection of really weak passwords, indexed by username
public cPW as new collection
cPW.add "dogs_name", "mark"
cPW.add "daughters_birthday", "john"
cPW.add "mypassword", "sally"

sub enum_users_from_collection(coll as collection)
dim sName as string
dim sMsg as string
'Collections have no "keys" property, of course
for each sName in coll.keys
sMsg = sMsg & vbcrlf & sName
next
msgbox sMsg,,"User Name List"
end sub

Thanks
-Mark

Feb 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: MattB | last post by:
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different...
6
by: Chua Wen Ching | last post by:
Hi there, 1) I am looking for the best collections techniques to be used in my program. Is hashtable or arraylist or any .net collection, which is the fastest when adding, getting out data???...
3
by: aa7im | last post by:
I am trying to design a good domain model for a system I am working on but I am having a hard time trying to figure out how to deal with relations and transaction... For example if I create an...
3
by: juli jul | last post by:
Hello, I grid and each line of it is taken from a collection,each line represents data and for each of those lines ,I have a button that when is being pressed sub data of each line wich is also...
1
by: Mark Miller | last post by:
I just recently started getting the above error on a page I am posting MULTIPART/FORM-DATA. We have SoftArtisans FileUp component and Filter installed on the server in question and up until a day...
3
by: Rob Thomas | last post by:
Hi, I've been tasked to come up with a new architecture for a large application at one of my customer's sites. In the past, I have developed multi-tier applications whereby the business...
5
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a...
6
by: Dave J | last post by:
I’m in need of some remedial instruction collections of objects. I doubt if this question is specific to pocket pc and VB, but that’s where I’m coding. When I create a collection, call it...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
5
by: WebSnozz | last post by:
Some collections are such that efficient search algorithms work on them such as binary search if the collection is a type which is sorted. I'm wondering how LINQ searches these collections and if...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.