ottenm
asked on
VB6: Trouble getting/returning RecordSet back from Dictionary!
Hello-
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.Open
rs.AddNew
rs.Fields.Item(0) = "test1"
rs.Update
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-
Mike
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.Open
rs.AddNew
rs.Fields.Item(0) = "test1"
rs.Update
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-
Mike
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>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(ke y))
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
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(ke
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
ASKER
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
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
ASKER
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-