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

Last reviewed: June 21, 1995
Article ID: Q103437
The information in this article applies to:

- Microsoft Visual Basic programming system for Windows, version 3.0

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.

MORE INFORMATION

Example One

  1. 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.

  2. Add one Data1 control, one Grid control, one Command button and two Text boxes to Form1.

  3. 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
    
    

  4. 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
    
    

  5. 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
    
    

  6. From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.

Example Two

  1. 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.

  2. Add one Grid control and one Command button to Form1.

  3. Using the following table as a guide, set the properties of the controls you added in step 2.

       Control Name   Property       New Value
       -------------------------------------------------
       Grid1          Cols           3
       Grid1          Rows           50
       Command1       Caption        Press to Load Grid
    
    

  4. 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
    
    

  5. 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
    
    

  6. From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.

Example Three

  1. 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.

  2. Add one Grid control and one Command button to Form1.

  3. Using the following table as a guide, set the properties of the controls you added in step 2.

       Control Name   Property       New Value
       ------------------------------------------------
       Grid1          Cols           3
       Grid1          Rows           50
       Command1       Caption        Press to Load Grid
    
    

  4. 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
    
    

  5. 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
    
    

  6. From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.

Example Four

  1. 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.

  2. Add one Grid control and one Command button to Form1.

  3. Using the following table as a guide, set the properties of the controls you added in step 2.

       Control Name   Property       New Value
       ------------------------------------------------
       Grid1          Cols           3
       Grid1          Rows           50
       Command1       Caption        Press to Load Grid
    
    

  4. 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
    
    

  5. 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
    
    

  6. From the Run menu, choose Start (ALT, R, S), or press the F5 key to run the program. Click the Command1 button.


Additional reference words: 3.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsCus


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.