DoCmd.OpenReport (ReportName, View, Filter, WhereCondition, DataMode, WindowMode, OpenArgs)
- ReportName: It is the name of a report in the current database. It is a required argument. It must be written between double quotes.
- View: There are a few types of report's view such as acViewDesign, acViewNormal, acViewPreview, acViewLayout and so on. You can choose one among those types to specify your report when it open.
- Filter: A query's name is in the current database. You must set it between double quotes.
- WhereCondition: Where the SQL clause without the word WHERE is applied. You must set it between double quotes.
- DataMode: This argument allows you to specify the data entry mode for your report. You can add data, edit data, or you can just read the data.
- WindowMode: You can specify the window mode when a report open. You can open a report as acDialog, acHidden, acIcon or acWindowNormal.
+ acDialog: The open report cannot move.
+ acHidden: The open report is hidden.
+ acIcon: The open report displays as an icon.
+ acWindowNormal: The open report displays as the view report.
- OpenArgs: This argument is used to specify the specific record when a report open. You must set it between double quotes.Remark: All arguments in DoCmd.OpenReport syntax are optional except ReportName argument.
For example, you have a form as the figure below:
When the form first open, the current record is Cate001. If you want to print this Category, click Print Report button. The report displays Cate001 with the products it contained. If you want to print Cate002 record, move to this record and then click Print Report button. If you intend to print Cate003, Cate004, Cate005, and son on, do the action of printing Cate001 or Cate002 record. To do this task, you need to create a master detail report as the figure below:
Then you need to apply the VBA code in CmdPrint_Click event procedure as the following:
Private Sub CmdPrint_Click()
DoCmd.OpenReport "RptProductByCategory" _
, acViewReport, , "CategoryID='" & txtCategoryID & "'"
Note: If you don't know how to create a master detail report, read this page: www.worldbestlearningcenter.com/Access-2010-report-master-detail-report.htm. The name of Command Button is optional.
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: