HOW TO: Populate a DataGrid with an ADO Recordset without Using a Database Connection (313330)



The information in this article applies to:

  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic Learning Edition for Windows 6.0

This article was previously published under Q313330

SUMMARY

This step-by-step article describes how to populate a Microsoft ActiveX Data Objects (ADO) Recordset and DataGrid without using a database connection. You may want to use the DataGrid as a typical grid. When you do this, you can display without binding the grid to any data source. This is useful when you use the grid as a spreadsheet. To do this, you typically use other grids, such as the Microsoft FlexGrid control. This article describes how to populate a DataGrid without using a database connection as you do for FlexGrid.

Back to the top

Prerequisites

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required for this procedure:
  • Microsoft Visual Basic 6.0
This article assumes that you are familiar with the following topics:
  • Visual Basic 6.0 terminology and syntax
  • Data Access technologies (ADO)
Back to the top

Populate the ADO Recordset

To populate the ADO Recordset, follow these steps:
  1. Open Visual Basic 6.0. On the File menu, click New Project.
  2. In the New Project dialog box, click to select Standard EXE, and then click OK.
  3. On the Project menu, click References.
  4. In the Available References list, double-click to select Microsoft ActiveX Data Objects 2.5 Library, and then click OK.
  5. On the Project menu, click Components.
  6. In the Component list, double-click to select Microsoft DataGrid Control 6.0, and then click OK.
  7. On the toolbox, double-click DataGrid control.

    DataGrid1 is created on the Form1.
  8. Similarly, add two CommandButtons to Form1.
  9. Open the Code Editor, and then copy the following code:
    Option Explicit
    
    ' Create a Recordset
    Dim rst As ADODB.Recordset
    
    Private Sub Command1_Click()
      
       Set rst = New ADODB.Recordset
       rst.CursorLocation = adUseClient
       
       ' Add columns to the Recordset
       rst.Fields.Append "Key", adInteger
       rst.Fields.Append "Field1", adVarChar, 40, adFldIsNullable
       rst.Fields.Append "Field2", adDate
    
       ' Open the Recordset
       rst.Open , , adOpenStatic, adLockBatchOptimistic
    
       ' Add data to the Recordset
       rst.AddNew Array("Key", "Field1", "Field2"), _
          Array(1, "string1", Date)
       rst.AddNew Array("Key", "Field1", "Field2"), _
          Array(2, "string2", #1/1/2000#)
    
       ' Populate the Data in the DataGrid
       Set DataGrid1.DataSource = rst
    
    End Sub
    
    
    Private Sub Command2_Click()
       ' Modify the data through code
       rst.MoveFirst
       rst(1) = "Changed Field"
       rst.UpdateBatch
    End Sub
    
    Private Sub Form_Load()
       Command1.Caption = "Populate"
       Command2.Caption = "Update"
    End Sub
    
Back to the top

Verify the Results

To verify the results, follow these steps:
  1. On the Run menu, click Start to run the application.
  2. Click Populate to populate the DataGrid with data.
  3. Modify the data "string2" in the second row to "Test String".
  4. Click Update.

    The data is modified in both rows.

Back to the top

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

140021 FILE: DBGRIDUB.EXE Uses DBGRID in an Unbound Mode

Back to the top

Modification Type:MajorLast Reviewed:6/5/2003
Keywords:kbDataBinding KbUIDesign kbForms kbHOWTOmaster KB313330 kbAudDeveloper