Microsoft KB Archive/313330

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

Article ID: 313330

Article Last Modified on 6/5/2003

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Learning Edition

-



This article was previously published under Q313330



IN THIS TASK

 * SUMMARY
 * Prerequisites
 * Populate the ADO Recordset
 * Verify the Results
 * REFERENCES



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:  Open Visual Basic 6.0. On the File menu, click New Project. In the New Project dialog box, click to select Standard EXE, and then click OK. On the Project menu, click References. In the Available References list, double-click to select Microsoft ActiveX Data Objects 2.5 Library, and then click OK. On the Project menu, click Components. In the Component list, double-click to select Microsoft DataGrid Control 6.0, and then click OK. On the toolbox, double-click DataGrid control.

DataGrid1 is created on the Form1.</li> Similarly, add two CommandButtons to Form1.</li>  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 &quot;Key&quot;, adInteger rst.Fields.Append &quot;Field1&quot;, adVarChar, 40, adFldIsNullable rst.Fields.Append &quot;Field2&quot;, adDate

' Open the Recordset rst.Open, , adOpenStatic, adLockBatchOptimistic

' Add data to the Recordset rst.AddNew Array(&quot;Key&quot;, &quot;Field1&quot;, &quot;Field2&quot;), _ Array(1, &quot;string1&quot;, Date) rst.AddNew Array(&quot;Key&quot;, &quot;Field1&quot;, &quot;Field2&quot;), _ Array(2, &quot;string2&quot;, #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) = &quot;Changed Field&quot; rst.UpdateBatch End Sub

Private Sub Form_Load Command1.Caption = &quot;Populate&quot; Command2.Caption = &quot;Update&quot; End Sub </li></ol>

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 &quot;string2&quot; in the second row to &quot;Test String&quot;.
 * 4) Click Update.

The data is modified in both rows.

Back to the top

<div class="references_section">