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

ID: Q103437


The information in this article applies to:


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.


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 query words: 3.00


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 24, 1999