Microsoft Access 2010 tutorial:Docmd to open report


DoCmd.OpenReport

IV. DoCmd.OpenReport

DoCmd.OpenReport is a method used to open a report. This method allows you to open a report in Design View, Layout View, Print Report, View Report, and Print Preview. It has the following syntax :

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:



Access 2010 open report with conditions

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:

Access product by category master detail report 

 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 & "'"
End Sub

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.



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.