Microsoft Access VBA Macro Programming

(Tina Sui) #1
ribbons for particular reports. Once you have your ribbon designed, you simply use the
Ribbon Name property on the report to integrate it into your report.
You can even remove the ribbon totally on a form using this methodology

Using VBA on Forms

VBA is not so common on reports as on forms due to the fact that the form is a means for the
user to view and print data. There is not normally user interaction once the report has been
displayed except for the user taking action to print out a hard copy.
However, reports also have their own modules and there is a whole event-driven structure
both for the report and the controls on it. As you have already seen, reports and controls have
a rich collection of properties, which allows an enormous amount of manipulation, letting
you change what the user sees according to certain circumstances.

Opening Your Report in VBA
To load a report, use the OpenReport method of the DoCmd object (see Chapter 16 for more
information on this object).

DoCmd.OpenReport _
(ReportName,View,FilterName,WhereCondition,WindowMode,OpenArgs)

The ReportName is a required parameter, but the others are optional:

 View An acView constant specifying the view that the form will open in. This is
acNormal by default.
 FilterName The name of a query in the current database.
 WhereCondition An SQL WHERE clause without the word WHERE.
 WindowMode An acWindowMode constant that specifies the window that the form
opens in.
 OpenArgs This sets the report’s OpenArgs property.

You can also use the Close method of the DoCmd object to close the report when it is
finished.

Using Events

When the user opens your report, they are firing off events that can be put to use in VBA.
Some of these events can be confusing when they are fired off and sometimes it is a good
idea to put a simple message box into each event so you can see when it actually happens.

120 Microsoft Access 2010 VBA Macro Programming

Free download pdf