How to Fill (Populate) a Grid with Database Data -- 4 MethodsID: Q103437
|
This article gives you four separate examples demonstrating how to use
Visual Basic to fill a grid control with data coming from database tables.
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
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
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
Control Name Property New Value
-------------------------------------------------
Grid1 Cols 3
Grid1 Rows 50
Command1 Caption Press to Load Grid
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
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
Control Name Property New Value
------------------------------------------------
Grid1 Cols 3
Grid1 Rows 50
Command1 Caption Press to Load Grid
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
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
Control Name Property New Value
------------------------------------------------
Grid1 Cols 3
Grid1 Rows 50
Command1 Caption Press to Load Grid
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
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
Additional query words: 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 24, 1999