Scripting.Dictionary (1 Viewer)

moke123

AWF VIP
Local time
Today, 13:22
Joined
Jan 11, 2013
Messages
2,801
I've been playing around with dictionaries trying to get a grip on how to use them and what they can do.

I've been trying to declare a public dictionary so that its members are available to other procedures within the module.
It seems to work OK if I add the keys and items using Dict.Add "SomeKey", "SomeValue"

If I try to open a recordset and use Dict.Add "SomeKey", rs!SomeField and If I try to access any member I get an error that the object no longer exists.

Am I missing something? Or is this not possible?
 

Grumm

Registered User.
Local time
Today, 19:22
Joined
Oct 9, 2015
Messages
354
From what I found, you will need to store the rs!SomeField in a variable first.
Then use the variable.
Without a data type, dictionary stores a pointer to the memory location instead of memory itself. So that may cause your problem.
 

moke123

AWF VIP
Local time
Today, 13:22
Joined
Jan 11, 2013
Messages
2,801
As I typed my question that thought came to mind but I haven't had a chance to test it yet. Thanks for reply.
That makes sense that once the recordset is set = nothing its no longer available.
It may also explain another error message re: datatype
 

jdraw

Super Moderator
Staff member
Local time
Today, 13:22
Joined
Jan 23, 2006
Messages
14,255
Moke,

Paul Kelly has some youtube videos on various subjects. He is an Excel person(I'm not), but his vba stuff is very good (IMHO). Here's one on Dictionary.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:22
Joined
May 21, 2018
Messages
6,268
You may want to relook at my thread on custom collections. You can take your dictionary and wrap it in a class and make a custom "collection" using a dictionary instead of a collection for the class variable.

This way you can make dedicated
Add
Item
Delete
etc.
Especially if this is tailored to do one thing. You can make get information out of the dictionary easier.
 

Cronk

Registered User.
Local time
Tomorrow, 03:22
Joined
Jul 4, 2013
Messages
2,660
Moke, you can add brackets around the recordset field
Dict.Add "SomeKey", (rs!SomeField)
 

moke123

AWF VIP
Local time
Today, 13:22
Joined
Jan 11, 2013
Messages
2,801
@MajP I'm still looking at the challenges. Thats what got me messing around with collections and dictionaries.
@Cronk I think I had tried that but not in the add statement. I'll take another look, thanks.
 

Grumm

Registered User.
Local time
Today, 19:22
Joined
Oct 9, 2015
Messages
354
There are a lot of workarounds for this I guess.
Using .value should also work
 

Users who are viewing this thread

Top Bottom