To automate your reports in Access, you can use VBA (Visual Basic for Applications) programming language to write code in the code module of the report.
The report object in your database has many properties. Each property can execute an event procedure. The difference with forms is that the user can not initiate any properties from his program. It is the developer who does everything. There are no buttons to click.
To write code behind a report, follow these steps:
First, you open a report in design view. Then open its properties menu. Click the event tab and open the combo box associated to the On Open event property. This is the event that is triggered when the report is opened.
Next, you choose [Event Procedure] from the combo box Then click the button that is just right of the combo box. The button has the period of ellipses (…) on it. This will open the VBA editor. A new sub procedure is created for this event.
Finally, you write some statements that will be executed when the report opens. For example, you want your program to prepare the data, just prior to printing.
Suppose you first need to execute a delete query, then an append query and finally an update query, you can write the following lines of code:
This technique ensures that the data is always up to date when your report executes.
You need to know though, that using VBA is not always the right answer to a problem. When developing a report, there are things that you can do that don ‘t require programming. Instead, with a properly placed control, or with the use of a grouping section, the desired functionality can also be achieved.
Over time, you will learn to identify what can be solved with code and what can be solved via the use of controls.