You can simulate binding a report to a DAO recordset object by setting the
RecordSource property to the name of a table, a query, or an SQL SELECT statement. If you want to use the same source for the report as a DAO recordset that you have opened, set the
RecordSource property of the report to the
Name property of the DAO recordset. The following example shows you how to open a DAO recordset that is based on a query and how to set the
RecordSource property of the report to the same source as the source of the recordset.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
- CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.
- Open the sample database Northwind.mdb.
- Close the Main Switchboard form when it appears.
- On the View menu, point to Database Objects, and then click Reports.
- In the Database window, click the Alphabetical List of Products report, and then click Design.
- On the View menu, click Properties to display the property sheet.
- On the Edit menu, click Select Report to verify that the property sheet being displayed is the one for the report.
- Click the Data tab, and then clear the RecordSource property.
- On the View menu, click Code to view the module of the form.
- Add the following code to the module:
Private Sub Report_Open(Cancel As Integer)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
'Open a DAO recordset based on the Alphabetical List of Products query
Set rs = db.OpenRecordset("Alphabetical List of Products", dbOpenDynaset)
Me.RecordSource = rs.Name
End Sub
- On the File menu, click Close and Return to Microsoft Access.
- On the File menu, click Save, and then close the report.
- Open the Alphabetical List of Products report in print preview.
Note that the report is bound to data from the Alphabetical List of Products query, which was the source of the DAO recordset object.