SUMMARY
This article shows you how to ensure that the
RecordSource property of a linked subreport is set only the first time that the Open event of the subreport is triggered. A linked subreport triggers the Open event several times, so you must make sure that the
RecordSource property is set only once.
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.
You can set the
RecordSource property of a subreport from
its OnOpen event at run time only. At all other times, this property is read-only and cannot be modified.
To create a report and subreport in which the
RecordSource of the subreport is selected at run time, follow these steps:
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.
back to the top
Create Two Example Queries
- Start Microsoft Access and open the sample database Northwind.mdb.
- In the Database window, click Queries and then click New to create a new query based on the Customers table.
- Add the CustomerName field to the query grid and type Like 'A*' in the Criteria row.
- Save the query as qryTest-A.
- Change the criteria to Like 'B*' and save the query with the new name qryTest-B and close the query.
back to the top
Create a Subreport
- In the Database window, click Reports and then click New to create a new report.
- Click the qryTest-A query in the selection list to indicate where the data comes from and then click OK.
- On the View menu, click Page Header/Footer to remove those sections from the report.
- Add the CompanyName to the Detail section of the report and shorten the Detail section to remove unnecessary white space.
- Save the report as rptTestSub and close the report.
back to the top
Create a Main Report
- Create a new report in Design view without selecting a record source.
- Click the Subform/Subreport tool in the Toolbox and then click in the top left corner of the Detail section.
- Click Use an Existing Report or Form, click rptTestSub in the list box and then click Finish.
- Save the report as rptTestMain.
- Click Preview and observe that the customers listed are those whose names begin with "A."
- Close the report.
back to the top
Use the Sub-report's OnOpen Event to Change Its RecordSource Property
- Open rptTestSub in Design view.
- In the report's property sheet, click the Event tab, and create the following OnOpen event procedure:
Private Sub Report_Open(Cancel As Integer)
Static intCallCount As Long
If intCallCount = 0 Then Me.RecordSource = "qryTest-B"
intCallCount = intCallCount + 1
End Sub
- Close the Visual Basic Editor and close and save the rptTestSub report.
back to the top
Test the Report
Open rptTestMain and notice that the customers listed are now those whose names begin with "B."
NOTE: Microsoft Access checks the value of the static variable intCallCount. If intCallCount is zero, this is the first time the Open event is triggered, so the code sets the
RecordSource property. Subsequent calls to the Open event will have a value of intCallCount greater than zero.
The variable intCallCount is declared in a
Static statement because its value must be retained through several Open events. If it had been declared using the
Dim keyword, its value would revert to 0 on each event and the code would fail.
back to the top