Example
Loading Recordsets using DGrid for DAO.
NOTE: Recorsets must have a primary index, be scrollable, and bookmarkable. For best results set the RecordsetType to dbOpenDynaset.
DGrid for DAO requires a reference to "Microsoft DAO 3.6 Object Library".
With a Data Control
The following example loads a recordset using a Data Control. This example assumes there is a data control named Data1 and a DGrid control named DGrid1 sited on the form.
Option Explicit
Private Sub Form_Load()
Data1.Visible = False
Data1.Refresh
DGrid1.LoadRecords Data1.Recordset
End Sub
Private Sub Form_Resize()
DGrid1.Top = 0
DGrid1.Left = 0
DGrid1.Width = Me.ScaleWidth
DGrid1.Height = Me.ScaleHeight
End Sub
Without a Data Control
The following example loads a recordset without using a Data Control. This example assumes you have a DGrid control named DGrid1 sited on the form and have added a reference to the "Microsoft DAO 3.6 Object Library".
Option Explicit
Dim db As DAO.Database
Dim rs As DAO.Recordset
Private Sub Form_Load()
Set db = OpenDatabase("YourDatabase.mdb")
Set rs = db.OpenRecordset("Select * From YourTable Order By YourField")
DGrid1.LoadRecords rs
End Sub
Private Sub Form_Resize()
DGrid1.Top = 0
DGrid1.Left = 0
DGrid1.Width = Me.ScaleWidth
DGrid1.Height = Me.ScaleHeight
End Sub
Private Sub Form_Unload(Cancel As Integer)
rs.Close
db.Close
End Sub