HOWTO: Create a Recordset From a Variant Array With RDS

ID: Q193331

The information in this article applies to:

SUMMARY

This article describes how to pass a variant array using the CreateRecordset method of the RDSServer.Datafactory and RDS.DataControl.

The article assumes that you have the Microsoft Data Access Components (MDAC) components installed on your computer. The Microsoft Data Access Components can be found at that following Web URL:

   http://www.microsoft.com/data/

MORE INFORMATION

The following code creates a Visual Basic ActiveX DLL that contains two methods. One method generates the recordset and the other method updates the recordset.

The client program that is created in Visual Basic tests the functionality of the component.

You can also use the RDS.Datacontrol to create a recordset from a variant array.

Step-by-Step Example

1. Start Visual Basic and create a business object (ActiveX DLL) project.

   Name the ActiveX DLL Project RDSRecordset.

2. Rename the default class name (class1) to MyClass.

3. Add a project reference to the Microsoft Remote Data Services Server

   Library and the Microsoft ActiveX Data Objects Recordset Library.

4. Paste the following code into the Declarations section of the class:

      Dim ADF As New RDSServer.DataFactory
      Dim rs As New ADOR.Recordset
      Public Function GetRS() As Variant

      Dim ColInfo(1), c0(3), c1(3)

      c0(0) = "Name"          ' Column name.
      c0(1) = CInt(129)       ' Column type (129 = adChar).
      c0(2) = CInt(40)        ' Column size.
      c0(3) = False           ' Is the column nullable?

      c1(0) = "Age"           ' Column name.
      c1(1) = CInt(3)         ' Column type (3 = adInteger).
      c1(2) = CInt(-1)        ' Column size.
      c1(3) = True            ' Is the column nullable?

      ' Add the columns to the recordset definition.
      ColInfo(0) = c0
      ColInfo(1) = c1

      ' Actual creation of Recordset from the variant array.

      Set rs = ADF.CreateRecordSet(ColInfo)
      rs.AddNew
      rs(0) = "Tom"
      rs(1) = 20
      rs.Update
      rs.AddNew
      rs(0) = "anotherTom"
      rs(1) = 21
      rs.Update
      Set GetRS = rs

      End Function


      Public Function ProcessRs(rs As Variant) As Variant

       ' Add another row to the recordset and send it back to the client.
       rs.AddNew
       rs(0) = "YetanotherTom"
       rs(1) = 30
       rs.Update
       Set ProcessRs = rs

      End Function

5. Compile and create the RDSRecordset.dll file.

6. Create the Visual Basic client application, RDSRecordsetClient. Create a

   Standard .exe project.

7. Add a reference to the RDSRecordset.dll file and to the Microsoft
   ActiveX Data Objects Recordset Library.

8. Place the following objects on the default form:

      Object            Name          Caption
      =========================================================

      List box          lstFields
      Command button    cmdGetRs      Get data.
      Command button    cmdUpdate     Update data.
      Command button    cmdSendData   Send data.
      Command button    cmdEnd        End.
      Form              frmMain       Creating recordsets without
                                      connecting to a database.

9. Paste the following code into the General Declarations of the frmMain
   form:

      Dim myObj As New RDSRecordset.myClass
      Dim myrs As New ADOR.Recordset

       Private Sub cmdGetRS_Click()

         ' Call the method of our object to get the recordset.
         Set myrs = myObj.GetRS

         DisplayData
         cmdGetRS.Enabled = False
         cmdUpdate.Enabled = True

      End Sub

      Private Sub cmdSendData_Click()

         'Let us call another method on our object to update the recordset
         ' and return it back.
         Set myrs = myObj.ProcessRs(myrs)
      DisplayData
      ReInitializeCmd

      End Sub

      Private Sub cmdUpdate_Click()

      ' Update the local recordset.
        cmdSendData.Enabled = True
        myrs.MoveFirst
        myrs.Fields(0).Value = "Changed1"
        myrs.Update
        DisplayData
        cmdUpdate.Enabled = False

      End Sub

      Private Sub Form_Load()

        Set myrs = myObj.GetRS
        ReInitializeCmd

      End Sub

      Private Sub DisplayData()

        myrs.MoveFirst
        lstFields.Clear
        Do While Not myrs.EOF
           lstFields.AddItem myrs.Fields(0).Value
           myrs.MoveNext
        Loop

      End Sub

      Public Sub ReInitializeCmd()

         cmdSendData.Enabled = False
         cmdUpdate.Enabled = False
         cmdGetRS.Enabled = True

      End Sub

      Private Sub cmdEnd_Click()

        Set myObj = Nothing
        Set myrs = Nothing
        End

      End Sub

10. Run the client program.

REFERENCES

For additional information, please see the following World Wide Web URL:

   http://www.microsoft.com/data/

Additional query words: MDAC kbRDS150 kbRDS200
Version           : WINDOWS:1.5,2.0
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: September 28, 1998