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

Article translations Article translations
Article ID: 313330 - View products that this article applies to.
This article was previously published under Q313330
Expand all | Collapse all

On This Page

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.

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)

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
    

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.

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

Properties

Article ID: 313330 - Last Review: June 5, 2003 - Revision: 2.1
APPLIES TO
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Learning Edition
Keywords: 
kbdatabinding kbuidesign kbforms kbhowtomaster KB313330

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com