Saturday, March 10, 2012

How To Create Dynamic Record Set in Visual Basic 6.0

' Start VB COM DLL code
Function getRS1() As ADODB.Recordset
Dim rsParent As New ADODB.Recordset
Dim rsChild As New ADODB.Recordset

rsParent.CursorLocation = adUseClient
rsChild.CursorLocation = adUseClient

' Add fields to recordsets.
rsParent.Fields.Append "ParentName", adVarChar, 20
rsParent.Fields.Append "ChildRS", adIDispatch
rsChild.Fields.Append "ChildName", adVarChar, 20

' Open recordsets
rsParent.Open
rsChild.Open

' Add records to child recordset.
rsChild.AddNew
rsChild(0) = "Child1"
rsChild.Update
rsChild.AddNew
rsChild(0) = "Child2"
rsChild.Update
rsChild.MoveFirst

' Add record to parent recordset.
rsParent.AddNew
rsParent(0) = "Parent1"
rsParent(1) = rsChild
rsParent.Update
rsParent.MoveFirst

Set getRS1 = rsParent

End Function
' End VB COM DLL code

' Start VB client code
Private Sub Command1_Click()
Dim ds As New RDS.DataSpace
Dim rs As New ADODB.Recordset
Dim objTestHR As Object

' UN-COMMENT ONE OF THE FOLLOWING 3 CREATEOBJECT LINES.
' USE THE PROGID FOR THE VB COM DLL CREATED IN STEP 1.

' HTTP METHOD:
'Set objTestHR = ds.CreateObject( "MyProject.MyClass", _
'"http://servername" )

' DCOM METHOD:
'Set objTestHR = ds.CreateObject( "MyProject.MyClass", _
'"\\servername" )

' IN-PROC METHOD:
'Set objTestHR = CreateObject("MyProject.MyClass")

Set rs = objTestHR.getRS1
Debug.Print "getRS1 results:"
Debug.Print ">>" & rs.Fields(0).Value
Debug.Print ">>>>" & rs.Fields(1).Value.Fields(0).Value
rs.Fields(1).Value.MoveNext
Debug.Print ">>>>" & rs.Fields(1).Value.Fields(0).Value
rs.Close
Set rs = Nothing

End Sub
' End VB client code

No comments:

Post a Comment