This example uses a Data control to create a Recordset object containing all Titles published in 1994. The Recordset object created is accessible by referencing the Recordset property of the Data control.
Dim Rs As Recordset
Data1.DatabaseName = "BIBLIO.MDB"
Data1.RecordSource = _
"Select * From TITLES where [Year Published] = 1994"
Data1.Refresh
Set Rs = Data1.Recordset
If Rs.RecordCount > 0 Then
Do Until Rs.EOF
Debug.Print Rs!Title
Rs.MoveNext
Loop
Else
MsgBox "No titles in 1994."
End If
This next example creates the same set of records as above in a Recordset object and assigns it to the Data control's Recordset property. To use this example, place a Textbox, Commandbutton, and Data control on a form and paste in the code.
Private Sub Command1_Click()
Dim DB As Database
Dim Rs As Recordset
Set DB = DBEngine.Workspaces(0).OpenDatabase("BIBLIO.MDB")
Set Rs = DB.OpenRecordset _
("Select * From TITLES where [Year Published] = 1994")
Set Data1.Recordset = Rs
Text1.DataField = "Title"
Data1.Refresh ' Force Data control to update now
End Sub