Different ways to open an access report using DoCmd.OpenReport
The OpenReport method is used to open a report in Design view or Print Preview, or to print the report immediately. You can restrict the records that are printed in the report.
An Access report has many properties that determine its behaviour. These properties concern the way data are presented, possible filters, how it can be used to enter of edit data etcetera. When you design your report, that is the time you will decide on the defaults for your report. However you may want to use rougly the same report in different ways. A powerful way to change from the design-time default behaviour is using the DocDmd.OpenReport parameters.
OpenReport with ReportName
The basic way to open a report is by supplying the ReportName and keeping all the defaults.
DoCmd.OpenReport ReportName:="Customer Phone Book"
When opened this way Access interprets the command with defaults as here:
DoCmd.OpenReport ReportName:="Customer Phone Book", View:=acWindowNormal,
FilterName:="FilterName", WhereCondition:=WhereCondition,
WindowMode:=acWindowNormal, OpenArgs:=OpenArgs
OpenReport View Mode
The view in which the report will open. acViewReport shows you the data, acViewPreview the print preview.
The most common way to open a report is as Report (acViewNormal - default). acDesign and acLayout are only relevant for development use, not in applications for end users.
The other options are
- acViewDesign - Design view
- acViewLayout - Layout view
- acViewNormal - (Default) Normal view
- acViewPreview - Print Preview
- acViewReport - Report view
DoCmd.OpenReport ReportName:="Sales Report", View:=acViewNormal, FilterName:="Annual Sales"
OpenReport using FilterName
FilterName refers to a query in the current database. You can use either an existing query or a filter that was saved as a query. You can use it both for getting a subset and for having it sorted.
DoCmd.OpenForm ReportName:="Invoice", FilterName:="Customers Extended"
OpenReport Where Condition
A valid SQL WHERE clause (without the word WHERE) to select records from the report's underlying table or query. If you select a filter with the Filter Name argument, Access applies this WHERE clause to the results of the filter.
To open a report and restrict its records to those specified by the value of a control on a form, use the following expression:
[fieldname] = Forms![ReportName]![controlname on the form]
Replace fieldname with the name of a field in the underlying table or query of the report you want to open. Replace ReportName and controlname on form with the name of the form and the control on the form that contains the value you want records in the report to match.
[fieldname] = Forms![ReportName]![controlname on other form]
Replace fieldname with the name of a field in the underlying table or query of the report you want to open. Replace reportname and controlname on other report with the name of the other report and the control on the other report that contains the value you want records in the first report to match.
OpenReport WindowMode
The default value acWindowNormal shows the report as normally done in Access. Using acDialog causes the report's Modal and PopUp properties to be set to Yes. With acDialog your calling code should not continue until the report is closed.
Options:
- acDialog - The form or report's Modal and PopUp properties are set to Yes.
- acHidden - The form or report is hidden.
- acIcon - The form or report opens minimized in the Windows taskbar.
- acWindowNormal - (Default) The form or report opens in the mode set by its properties.
OpenReport OpenArgs
OpenArgs gives an opportunity to pass data to the report which it then can pick up in the Report_Open event. An example of how to do that can be found here