Re: Using collection to store variable values
- From: "Dale Fye" <dale.fye@xxxxxxxxxx>
- Date: Tue, 28 Aug 2007 21:48:25 -0400
Thanks, Marsh,
I was just playing around with this concept, I don't really need the key
values but was wondering whether there was a method to do so.
A while back, I read an article that advised use of custom functions to
store variables rather than declaring Global Variables. It explaained that:
1. some global variables will lose their values when some errors occur,
causing unintended problems
2. unlike functions, you cannot use global variables as parameters in
queries, unless you build the SQL dynamically and concatenate the value of
the variable.
It described how to create the function that allows you to both set and
retrieve a value stored in a static variable. I've found this extremely
useful but in one of my programs have about a dozen of these functions. I
was toying with the idea of extending that functionality to a static
collection defined within a function, so that instead of having to have
numerous functions, I'd just have this one function that gets passed a key,
and a value (to set the value) or a key (to retrieve the value). So far,
that functionality is working fine in the test program I've developed. Have
not had a chance to test reading and writing to it multiple times (for a
speed test comparison).
The problem with using an array is that you either have to keep track of
which position the variable is stored in, or have to search the array for
the value. At least with a collection, you can get the value as long as you
know the key. Hopefully, the code that supports the collection is optimized
for this search.
Dale
"Marshall Barton" <marshbarton@xxxxxxxxxx> wrote in message
news:g699d3963im77em7g2spkvfrn6gad9tv93@xxxxxxxxxx
Dale Fye wrote:
I'm attempting to use a collection to store variable values associated
with
some of my code, so I don't have to keep using DLOOKUP to get information
from some of my tables.
I have a function that I pass a key and an optional value. If the key
value
is not part of the collection, I add it and set its value. If the
optional
value is included, the function sets the value associated with the
collections key value. if no value is provided, the function returns the
value that is already associated with the key.
How can I enumerate the key values associated with this collection? I
know
I can loop through the item numbers, but how can I see what keys are
associated with a particular item number?
It seems like a stupid omission, but you can not get the key
value from a collection. I suggest that you use a two
dimensional array instead (probably faster too).
--
Marsh
MVP [MS Access]
.
- Follow-Ups:
- Re: Using collection to store variable values
- From: Marshall Barton
- Re: Using collection to store variable values
- References:
- Re: Using collection to store variable values
- From: Marshall Barton
- Re: Using collection to store variable values
- Prev by Date: Re: Select Query - Adding New record
- Next by Date: Re: Setting records in subform by pressing button in masterform
- Previous by thread: Re: Using collection to store variable values
- Next by thread: Re: Using collection to store variable values
- Index(es):
Relevant Pages
|