MS Excel
MS Excel
Beginning Access Database Macros ............................................................... 3 What is an Access macro? ....................................................................... 3 The macro designer .................................................................................. 4 Creating our first macro ............................................................................ 6 A macro to filter a form by a value ............................................................ 6 Create a macro to copy and back up an object....................................... 10
For beginners of Microsoft Access databases VBA programming can be a daunting task. Fortunately Access offers an alternative in the shape of macros.
Apart from the issues mentioned, Access macros enable us to create some powerful functionality. This guide will enable you to get started and experience the power of MS Access macros.
You can also add another column by going up to the view menu and selecting conditions.
Lets now go over what each column is used for. Macro Name As the title suggests this is where we give the macro a descriptive name. Condition This is where you specify what conditions the macro needs to meet when executed. For example a condition could be - does the order have an order number? If it does, then execute the macro actions. Action This is the macro task to be performed. For example CopyObject will copy an object such as a table or query to the current or another database. Comment A description of what the macro actually does. This is optional, but can be handy for a new developer working on an existing system.
Back at the macro designer window. In the macro name section I have typed in CountryFilter.
I am not adding anything in the condition section. In the action section I select the ApplyFilter action.
On my form I create a new command button using the wizard. I select the miscellaneous category and the run macro action.
I now select the macro I want to use behind the button. Notice it gives the macro name and the action within it.
So now I have wired up my macro to a button on a form. When the button is clicked my macro will fire and will apply a filter to the form for all records in Germany.
We could set up more macros and call them Macro2,3,4,5 etc. However, it is better to set up a macro group to hold more than one macro. Each one is identified and called by name.
Destination database is where I want the object saved. I enter a path here to the database. New name is any new name I want to give to this object. As I am making a backup in another database I called it Customers_BK. In the source object type tell it the type of object you are selecting. In this case my type is a table. The source object name will be the current name of the object in the database. In this case it is my customers table. You may also want to back up additional tables. The procedure would be the same. Just add more CopyObject actions to this group.
10
Ive also added my orders table in, so now I have two CopyObject actions.
Ive also added a message box action to display a message when the operation is complete.
The message I want displayed is Backup is complete. I now save and run the macro.
11
The code window will appear. My procedure looks like the following. Private Sub Command0_Click() DoCmd.RunMacro ("Macro1.BackupObject") End Sub
12
The line that calls the macro is DoCmd.RunMacro ("Macro1.BackupObject") Macro1 is the name of the saved macro shown in the database window. BackupObject is the name of the group inside the macro1 that contains the actions. The other groups will not be run, only the one referred to in the code above. When the macro has finished running the message specified in the macro is displayed.
And in my external database the tables have been saved. Customers_BK Orders_BK
Objects such as forms and reports can also be copied by using the CopyObject action.
13
2010 Paul Barnett. All Rights Reserved. No part of this publication may be reprinted or reproduced without permission.
14