VB6: Trouble getting/returning RecordSet back from Dictionary!


I am creating a class module with a private Dictionary in VB6.  I successfully insert and retrieve Strings, Integers, Dates, and RecordSets (into/from the Dictionary).  I have successfully written functions to retrieve and return the Strings, Integers, and Dates, but I am having difficulty with the RecordSets.  If I make the dictionary public, I can see/retrieve the RecordSet with no problem.  But I would prefer to keep the Dictionary private and use a function (many reasons).

Here is the code that uses the class module:

Dim myobject As New MyClassModule
myobject.AddToDictionary 'adds the RecordSet (code below)
Set myMSHFlexGrid.DataSource = myobject.d.Item("mykey") 'works fine
'Set myMSHFlexGrid.DataSource = myobject.getRS("mykey") 'no workey!

'MyClassModule below including 2 attempts at get/return
Option Explicit

Public d As New Dictionary 'wish it were private ;(

Public Sub AddToDictionary() 'create and add a RecordSet to the Dictionary
Dim rs As New Recordset
rs.Fields.Append "Field1", adBSTR
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Fields.Item(0) = "test1"
d.Add key:="mykey", Item:=rs
End Sub

Public Function getRS(key As String) As Recordset 'error: invalid use of property
tget = d.Item(key)
End Function

Public Function getRS(key As String) As Object
tget = d.Item(key) 'error: object variable or with block variable not set
End Function

By the way, VarType(d.Item(key)) returns vbObject!  Is there a downcast in VB6?

I have almost identical code working for the Dates, Strings, and Integers

Thanks for any help, much obliged-

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Add 'Set' keyword...

Public Function getRS(key As String) As Object
Set getRS = d.Item(key)
End Function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ottenmAuthor Commented:
Thanks, easy change!

I still run into trouble downcasting (if that's the right term here):

Dim value As Variant
value = dictionary.Item(key) 'some values are RecordSets, others are Strings, Longs, etc.
    Select Case VarType(value)
        Case vbLong, vbDecimal, vbDate, vbString
           'all of these work fine
        Case vbObject 'this is what VarType() returns for my RecordSets
           'trying to use it as a RecordSet
           Debug.print value.RecordCount 'error: object doesn't support this property
           Dim r As Recordset
           Set r = value 'error: type mismatch

Thanks for your help-
>>value = dictionary.Item(key)

Because of this assignment, an object must be:

Set value = dictionary.Item(key)

But since that doesn't work for strings or numerical data types, you should probably do:

    Select Case VarType(dictionary.Item(key))
        Case vbLong, vbDecimal, vbDate, vbString
           value = dictionary.Item(key)
           'other code
        Case vbObject 'this is what VarType() returns for my RecordSets
           Set value = dictionary.Item(key)
           Debug.print value.RecordCount
ottenmAuthor Commented:
That's a charmer man, thanks for the tips.

This is some crazy smoke spelling out t-h-e-n all the time and being 'insulated' from, ... the other stuff.

Have a great day-

An old C++/Java guy
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.