Microsoft KB Archive/103437

= How to Fill (Populate) a Grid with Database Data -- 4 Methods =

Article ID: 103437

Article Last Modified on 1/8/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q103437



SUMMARY
This article gives you four separate examples demonstrating how to use Visual Basic to fill a grid control with data coming from database tables.


 * The first example uses a data control to fill the grid.
 * The second example uses a Dynaset object to fill the grid.
 * The third example uses a Snapshot object to fill the grid.
 * The fourth example uses a Table object to fill the grid.



Example One
 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default. Add one Data1 control, one Grid control, one Command button and two Text boxes to Form1.  Using the following table as a guide, set the properties of the controls you added in step 2.   Control Name   Property       New Value    Comment Data1         DatabaseName   BIBLIO.MDB   Provide the full path to                                              this file, which should be in the Visual Basic directory -- C:\VB Data1         RecordSource   Authors Data1         Visible        False Text1         DataSource     Data1 Text1         DataField      AU_ID Text1         Visible        False Text2         DataSource     Data1 Text2         DataField      Author Text2         Visible        False Grid1         Cols           3 Grid1         Rows           50 Command1      Caption        Press to Load Grid

  Place the following code in the Form1 Load event procedure: Sub Form_Load 'Initialize the colwidths for the grid and supply headers Show grid1.ColWidth(1) = 3000     'For Author name grid1.ColWidth(2) = 1000     'For Author ID      grid1.Col = 1 grid1.Row = 0 grid1.Text = "Author Name"   'Header for Author Name grid1.Col = 2 grid1.Row = 0 grid1.Text = "Author ID"     'Header for Author ID   End Sub

  Place the following code in the Command1 Click event procedure: Sub Command1_Click ' The routine to load data into grid Dim counter% counter% = 1                         'Start counter at Row=1 Do Until data1.Recordset.EOF grid1.Col = 1 grid1.Row = counter% grid1.Text = data1.Recordset(1)   'Load the Author Name grid1.Col = 2 grid1.Row = counter% grid1.Text = data1.Recordset(0)   'Load the Author ID         counter% = counter% + 1 data1.Recordset.MoveNext Loop data1.Recordset.Close End Sub

 From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.</li></ol>

Example Two
<ol> Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.</li> Add one Grid control and one Command button to Form1.</li>  Using the following table as a guide, set the properties of the controls you added in step 2. <pre class="fixed_text">  Control Name   Property       New Value -  Grid1          Cols           3 Grid1         Rows           50 Command1      Caption        Press to Load Grid

</li>  Place the following code in the Form1 Load event procedure: Sub Form_Load 'Initialize the colwidths for the grid and supply headers Show grid1.ColWidth(1) = 3000     'For Author name grid1.ColWidth(2) = 1000     'For Author ID      grid1.Col = 1 grid1.Row = 0 grid1.Text = "Author Name"   'Header for Author Name grid1.Col = 2 grid1.Row = 0 grid1.Text = "Author ID"     'Header for Author ID   End Sub

</li>  Place the following code in the Command1 Click event procedure: Sub Command1_Click ' The routine to load data into grid Dim db as Database Dim ds as Dynaset Dim counter% Set db = OpenDatabase("BIBLIO.MDB") Set ds = db.CreateDynaset("Authors") counter% = 1                 'Start counter at Row=1 Do Until ds.EOF grid1.Col = 1 grid1.Row = counter% grid1.Text = ds(1)        'Load the Author Name grid1.Col = 2 grid1.Row = counter% grid1.Text = ds(0)        'Load the Author ID         counter% = counter% + 1 ds.MoveNext Loop ds.Close db.Close End Sub

</li> From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.</li></ol>

Example Three
<ol> Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.</li> Add one Grid control and one Command button to Form1.</li>  Using the following table as a guide, set the properties of the controls you added in step 2. <pre class="fixed_text">  Control Name   Property       New Value Grid1         Cols           3 Grid1         Rows           50 Command1      Caption        Press to Load Grid

</li>  Place the following code in the Form1 Load event procedure: Sub Form_Load 'Initialize the colwidths for the grid and supply headers Show grid1.ColWidth(1) = 3000     'For Author name grid1.ColWidth(2) = 1000     'For Author ID      grid1.Col = 1 grid1.Row = 0 grid1.Text = "Author Name"   'Header for Author Name grid1.Col = 2 grid1.Row = 0 grid1.Text = "Author ID"     'Header for Author ID   End Sub

</li>  Place the following code in the Command1 Click event procedure: Sub Command1_Click ' The routine to load data into grid Dim db as Database Dim Snap1 as Snapshot Dim counter% Set db = OpenDatabase("BIBLIO.MDB") Set Snap1 = db.CreateSnapshot("Authors") counter% = 1                 'Start counter at Row=1 Do Until Snap1.EOF grid1.Col = 1 grid1.Row = counter% grid1.Text = Snap1(1)     'Load the Author Name grid1.Col = 2 grid1.Row = counter% grid1.Text = Snap1(0)     'Load the Author ID         counter% = counter% + 1 Snap1.MoveNext Loop Snap1.Close db.Close End Sub

</li> From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.</li></ol>

Example Four
<ol> Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.</li> Add one Grid control and one Command button to Form1.</li>  Using the following table as a guide, set the properties of the controls you added in step 2. <pre class="fixed_text">  Control Name   Property       New Value Grid1         Cols           3 Grid1         Rows           50 Command1      Caption        Press to Load Grid

</li>  Place the following code in the Form1 Load event procedure: Sub Form_Load 'Initialize the colwidths for the grid and supply headers Show grid1.ColWidth(1) = 3000     'For Author name grid1.ColWidth(2) = 1000     'For Author ID      grid1.Col = 1 grid1.Row = 0 grid1.Text = "Author Name"   'Header for Author Name grid1.Col = 2 grid1.Row = 0 grid1.Text = "Author ID"     'Header for Author ID   End Sub

</li> <li> Place the following code in the Command1 Click event procedure: Sub Command1_Click ' The routine to load data into grid Dim db as Database Dim t as Table Dim counter% Set db = OpenDatabase("BIBLIO.MDB") Set t = db.Opentable("Authors") counter% = 1        'Start counter at Row=1 Do Until t.EOF grid1.Col = 1 grid1.Row = counter% grid1.Text = t(1)         'Load the Author Name grid1.Col = 2 grid1.Row = counter% grid1.Text = t(0)         'Load the Author ID         counter% = counter% + 1 t.MoveNext Loop t.Close db.Close End Sub

</li> <li>From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.</li></ol>

Additional query words: 3.00

Keywords: KB103437

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.