Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Here's a question for you VB gurus.

Status
Not open for further replies.

Bushwack

Programmer
Dec 20, 2001
3
US
Here's a question for you VB gurus.

I'm using a Collection. When adding Items into my Collection, I'm adding values from a Recordset. I'm assigning a field from the Recordset as a string into the "Item", and I'm assigning the record's key into the collection.Item's "key" as a string.

Here's the records I'm making the assignment with... (there's only two fields with the first being the ID or Key for the record)

2 Fred
25 Bob
30 Joe

I use this statement to add each Item into my Collection (the name of my Recordset is "rsUtility")

Collection.add Cstr(rsUtility!Name), Cstr(rsUtility!ID)

Here's my question... when I reference the Collection's Item like this...

Collection(Cstr(rsUtility!ID))

...what will I get back? Will I get the Item "Fred" because the Item's "key" for "Fred" is 2 and I'm giving the Index a string?
...or will I get the second Item in the Collection (which in this case would be "Bob")?
 
You will get Fred because you are giving it a string.
If you need to retrieve an item by ordinal position, you need to give it an integer

Nikita
 
Prepend a 'K' character onto the beginning of the numeric key, so it can't be recognised as anything but a string. The Key parameter for Collection.Add is actually a variant: if you suppy a numeric string the collection will try to translate it into a number.

If this is too confusingm, use a Dictionary, but please remember to use the syntax Recordset!Field.value, not just Recordset!Field as otherwise the Dictionary will add the key as the field object itself!
 
"If this is too confusing, use a Dictionary, but please remember to use the syntax Recordset!Field.value, not just Recordset!Field as otherwise the Dictionary will add the key as the field object itself!"

The same is probably true about a collection because of variants. Thankfully they (variants) go away in VB.NET. Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top