Locked lesson.
About this lesson
Create a command button to open a report using a macro or VBA and create a Combo Box using the wizard to show Customers and Store CustID.
Exercise files
Download this lesson’s related exercise files.
Services_36_Start Main Menu Command Buttons to OpenReports with Criteria, VBA.accdb1.7 MB Services_36_Main Menu Command Buttons to OpenReports with Criteria, VBA.accdb
1.8 MB
Quick reference
Command Buttons to Open Reports using VBA
Application Terminology
VBA Procedure to Open a Report in Print Preview on a Click event
This VBA procedure opens a report called "R_Customers". acViewPreview is the VBA constant to open the report in Print Preview mode so it goes to the screen instead of the printer. The name of the control is cmd_RptCustomers. The event is Click.
Intellisense Shortcut
If Intellisense doesn't come on, press to trigger it
Help for OpenReport
This is the Help from Microsoft for the OpenReport Macro Action.
https://msdn.microsoft.com/EN-US/library/ff834462.aspx
The OpenReport action has the following arguments.
The Print setting for the View argument prints the report immediately by using the current printer settings, without bringing up the Print dialog box. You can also use the OpenReport action to open and set up a report and then use the PrintOut action to print it. For example, you may want to modify the report or use the PrintOut action to change the printer settings before you print.
The filter and WHERE condition you apply become the setting of the report's Filter property.
The OpenReport action is similar to double-clicking the report in the Navigation Pane, or right-clicking the report in the Navigation Pane and selecting a view or the Print command.
Tips
- To print similar reports for different sets of data, use a filter or a WHERE clause to restrict the records printed in the report. Then edit the macro to apply a different filter or change the Where Condition argument.
- You can drag a report from the Navigation Pane to a macro action row. This automatically creates an OpenReport action that opens the report in Report view.
Steps
Command Button to Open a Report Using a Macro
- Click on the Button tool in the Controls group of the FORM DESIGN TOOLS, DESIGN ribbon.
- Click on the form where you want to put a button.
- When the Command Button Wizard comes up, choose Report Operations from Categories on the left.
- Choose Preview Report from Actions.
- Click Next.
- From the list of report, choose the report name you wish to open.
- Click Next.
- Choose the default, "Open the form and show all the records", and click Next.
- Change the option to Text, instead of Picture, specify the Caption for the button (i.e., "Customers Report").
- Click Next.
- For the name of the button, type cmd_something (i.e., cmd_CustomersReport).
- Click Finish.
- Resize the button and change other properties, if desired.
Command Button to Open a Report Using VBA
- Click on the Button tool in the Controls group of the FORM DESIGN TOOLS, DESIGN ribbon to pick up the button tool.
- Click on the form where you want to put a button. If the Command Button Wizard comes up, click Cancel.
- Choose the Event tab on the Property Sheet. In the Click event for the button, type [ and choose [Event Procedure]
- Click the Builder button (…) or press (Ctrl-F2)
- You will be placed in VBA between the procedure declaration statement and the End Sub statement. Press (Tab) to indent the line.
- Type 'date to create a comment with the date
- Press (Enter) to make a new line
- Type:
DoCmd.OpenReport "ReportName", acViewPreview
WHERE ReportName is the name of the report you want to open. - From the menu in VBA, choose Debug, Compile and then Save.
Create a Combo Box Using the Wizard to Show Customers and Store CustID
- Pick the Combo Box tool from the Controls group, then click on the form where you want it to go.
- When the Combo Box wizard comes up, choose "I want the combo box to get the values from another table or query".
- Click Next
- Choose the Customers table
- Click Next.
- For the fields, double-click CustID and Customer
- Click Next.
- Choose to sort by Customer.
- Click Next.
- Hide Key is checked, which means that CustID will not show, even though it is what will be stored.
- In this dialog box, drag the right border of the column header to resize since a customer name may be longer.
- Click Next
- Choose a Caption for the label.
- Click Finish.
- Since this combo will contain CustID, change the Name property to CustID.
- Change the Name property of the label to have CustID in it too.
Lesson notes are only available for subscribers.