Access: Macros

Access contains many ready-made macros so you don't have to record them by pressing keystrokes the way that you do in Word and Excel. There are many macro options available and they can be grouped together to produce powerful actions. In fact macros are probably best viewed as a stepping stone to Visual Basic as the latter is even more powerful and almost essential in the development of advanced applications.

We have already seen that we can create a macro to generate a report and then open the report from a command button on a form.

You may find it useful to open an Access object from within a form. The object can be opened by clicking on a command button, which is the obvious way to do it, or through an event trigger such as changing a data item. Let us for the moment stick with command buttons.

The macro options you are most likely to want to use to begin with are:

OpenForm

Open a form; specify the name of the form along with the view, the name of any filter, any search (SQL where) conditions, the data mode (add, edit, read only) and the window mode.

OpenQuery

Open a query; specify the name of the query, the view (datasheet, design, Print Preview, PivotTable, PivotChart) and the data mode (add, edit, read only).

OpenReport

Open a report; specify the name of the report, the view (datasheet, design, Print Preview), the name of any filter, any search (SQL where) conditions and the window mode.

OpenTable

Open a table; specify the name of the table, the view (datasheet, design, Print Preview, PivotTable, PivotChart) and the data mode (add, edit, read only).

Command Buttons

You can use a command button on a form to call a ready-made macro and perform a basic operation such as displaying a report or a query and printing its contents. To do this open a form in design view and edit it to make room for a button.

Select the Command button tool from the Toolbox and draw a button on the form. This starts the Command Button wizard, which we saw in the section on tables. To change the macro associated with the button right-click it and choose Properties:

If you return to editing a button you should right-click it and choose 'Build Event' from the popup menu.

In the example below a macro has been made to open a query. The query is a parameterised query that prompts the user for a CD Serial Number and displays the tracks associated with it in a separate query window.

Here is the output from the query:

 

Return to Access Menu