Office 2007 - Access

Lesson 65: Macros (cont…)

65/72 Lessons 

Embedded “Macros”

To add a macro to a button on the form, select the button and open the “Properties window”.
You already know how to add a button and how to open the “Properties window”.
Select the “OnClick” in the “Properties window” and click the button with the dots.
This is just for information: clicking the downward pointing arrow next to this button displays a list of all previously created independent “Macros”.

This opens the “Macro” window.

The first action I do remains the same as in the previous lesson so, open a “Report” in “Print Preview”.
But here I add a second action in example .
When the “Report” opens, I zoom the contents by 50%.
So I click the second line in the “Action” column and select “Run Command” in the drop-down list.
We can set only argument for this action.
I choose “Zoom50%” command from the drop-down menu .

Click the “Save” button in the “Quick Access” toolbar and close the “Macro” window.

When you look in the “Navigation pane” for the newly created “Macro”, you will not find it. This is what is called an “embedded macro”.

You can not use this “Macro” with other buttons.

You will not find this “Macro” when you click the downward pointing arrow in the list of “independent macros”.

Enter Conditions

It is not possible to explain the purpose of all actions that are possible with a macro but,  I do not want to deprive you of a few important ones.

The first is to enter a prerequisite.

In this example, I open the “Add customer” form, in “Design View”.

What I want to achieve is, when someone types a Zipcode of 9100 , it should automatically set the “Location” field to ‘St-Niklaas’ and the cursor should jump to the “Phone Number” field, when the Tab key or the Enter key is pressed on your keyboard .

This aside: it is an embedded macro.

How do we do it ?

First select the “Zipcode” field in the form.

Open the “Properties window”.

Select the field “On Exit” in the “Properties window”, under the “Event” tab.

Click the button with three dots and choose “Macro builder” in the dialog box.
Click the OK button.

The “Macro” dialog box appears.

Since we want to add a condition to the action, we need the “Conditions” column.

Just click the “Conditions” button in the “Macro” window “Ribbon”.

It is also important when you work with a specific value for a specific field, you should select “Show All Actions” button, this gives us a list of actions that we can use in “Access”. By default it is not set and you will only see the actions that are “safe” according to “Access”.

The first thing we enter the is a “Condition”.

When you’re used to working with “Access Codes” you can enter them manually or click the “Expression Builder” in the “Ribbon”.
This opens the “Expression Builder”.
In this dialog box, select the text you want to use in your code.
This can be found when you double-click on the name of the form in which you are working, in this case the “Add Customers” form.
In the middle of the dialog box, double-click the “Zipcode” field and then type the value:
In (“9100”)
This code means nothing more or less than:
when you are in the “Add Customer” form and “Zipcode” field value is 9100.

Important: This value must be in parentheses and quotation marks.

Click the “OK” button

For your information:

If you want to fill different values in “Location” field automatically, type eg: In (“9100”, “9111”, “9110”).
You separate the different values with a coma and each value must be between quotes.

This brings us back into the “Macro” window.
Select the first field in the “Action” column, click the downward pointing arrow that appears and in the drop-down menu, choose the “SetValue” action.
Select the first field in the “Arguments” column.
Down in the “Action Arguments”,  in the “Item”, enter the field name, enclosed on square brackets, to be automatically ,in this case, the [Location]. I
In the “Expression” type the value in quotes, that the field “Location” must be completed, in this case “St Niklaas”.

The second action we want is the cursor to jump to the ” Phone Number” after clicking the “Enter” key on our keyboard.
Just click the second row in the “Action” column and choose the “GoToControl” action in the drop-down menu.
In the “Condition” column, type three dots, which means that after the “SetValue” action, it moves to the next action.
In the “Action Arguments” for this action, we have only one argument, namely the control name.
This is the control to where the cursor will jump when you press the “Enter” key. In this case, [ Phone Number].
We also type this control in brackets.

Then we have this:

Click the “Save” button in the “Quick Access” toolbar.

Close the “Macro” window and try all this in “Form View”.

Awesome!
You've completed Lesson 65
START NEXT LESSON