macros tutorial week 20. objectives by the end of this tutorial you should understand how to: create...

39
Macros Tutorial Week 20

Post on 21-Dec-2015

234 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Macros

Tutorial Week 20

Page 2: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Objectives

By the end of this tutorial you should understand how to:

Create macros Assign macros to events Associate macros with VBA Assign a macro to an application

Switchboard at start up Add error messages through macros

associated with events

Page 3: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

What are macros?

Macros are a means by which we can automate a process or group of processes.

For example, in many Microsoft applications we can ‘record’ a macro to automate a task that the end user performs on a regular basis

Page 4: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Recording a macro

A useful macro for a student to record would be to set up a Word document that can be used as a generic template for all the reports that need to be written for assignments

Most academic reports require a contents list, page numbers, headings and a footer showing the authors name

Page 5: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Recording a macro

To do this, create a new Word document

Then select Tools, Macros, Record New Macro from the main menu

Page 6: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Recording a macro

Give the macro a name

You could assign the template to a keyboard shortcut, a toolbar or run it directly from the macro menu

Click OK

Page 7: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Recording a macro

Add the section headings and assign them to ‘Heading 1’ style

Set each heading on a new page by pressing ctrl_Enter

Add the footer with page numbers

Page 8: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Recording a macro

Add appropriate table of contents from main menu

Select ‘Table of Contents’ tab and click OK

Page 9: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Recording a macro

Click ‘Stop Recording’ Close the document

without saving The macro is now

available to use

Page 10: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Playing a recorded macro

Open a new document and select – Tools, Macro, Macros

Find the recorded macro from the list and click ‘Run’

The document will open with your pre-formatted conditions

Page 11: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

VBA – Visual Basic for Applications

The actions performed whilst recording the macro have been converted into lines of Visual Basic code

To view the code select Tools, Macro, Macros and then select the macro in the LH window and click ‘Step into’ – more on VBA later

Page 12: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

VBA Code

Page 13: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Macros in MS Access

To use macros in MS Access we have to use a different approach as we cannot ‘record’ the macro

Before we create a macro we must consider what we want to automate

As macros are based on VB code a macro can only respond to an ‘event’

Page 14: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Macros in MS Access There are many

pre-written ‘actions’ that a macro can respond to

To view these actions select the Macro option in the database design interface and click ‘New’

Page 15: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Macros in MS Access

Click on the ‘Action’ dropdown list to view the actions

Page 16: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Macros in MS Access Now let us consider where we could use

macros As computing students you will have to

develop a software project, possibly using MS Access

An Access project must have a menu structure to allow the end user to navigate though the system

Access provides this functionality through a Switchboard

Page 17: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Switchboards

An example of a switchboard is shown below

Page 18: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Switchboards

When the user clicks on the database icon we would not normally expect them to have access to the development environment, only the menu (switchboard)

To load the switchboard at the program start we can create a new macro named AutoExec

Page 19: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

AutoExec Macro Assume that we have already created

our Switchboard (a tutorial for creating switchboards is available)

Click on ‘Macros’ in the development window and select ‘New’

Select ‘OpenForm’ from the action list and set the Form name to Switchboard – see next slide

Page 20: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

AutoExec Macro

Set the Data Mode to Edit

Close and name the macro AutoExec

There can only be one file of this name in any application

Page 21: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Setting Startup The next time you open the program

it will look for the AutoExec macro and perform the actions assigned to it – in this case, open the Switchboard

However, the end user still has access to the development environment

To prevent this select Tools, Startup… from the main menu

Page 22: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Setting Startup Uncheck all the options in

the dialog box to remove all development toolbars

This is best performed after all development has been carried out

However, after un-checking all the options you can access the development environment by pressing and holding the Shift key before clicking the icon to open the application

Page 23: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Multiple Actions

It is possible to assign multiple actions to a macro, for example, if we wanted to open a ‘Clients’ table showing the records in descending order and set the window to maximised we would write the following macro

Page 24: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Multiple Actions

The action ‘arguments’ are assigned in the bottom LH pane of the design window

Page 25: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Activating the macro

To activate the macro we can either double click the macro name in the Macros window or assign it to an event

If end-users only have access to the Switchboard then we will have to assign the macro to the click event of one of the switchboard menu buttons

Page 26: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Assigning a macro to an event Assuming that a Switchboard has been

added to the application, click on Tools, Database Utilities, Switchboard manager – see below

Page 27: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Assigning a macro to an event

If we have a menu structure similar to the one shown, click on Open Tables and select Edit

Then click New – see next slide

Page 28: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Assigning a macro to an event

Add a name in the Text textbox Select Run Macro from the Command

list Select the OpenClientTable from the

Macro List and click OK and close switchboard manager

Page 29: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Running the Switchboard Macro

Open the Switchboard Click on Open Tables

in the Main Switchboard

Click on Open Client Table and the table will be displayed maximised and the data listed in descending order

Page 30: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Macro Conditions You may want to specify a condition

i.e. do not display the table if there are no records to display

To add conditions click on the ‘Conditions’ icon on the main menu bar and the conditions column is displayed

Page 31: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Building the Condition expression

Right click on the row where you want to apply the condition and select ‘Build’

From the Built in Functions list select DCount

Add the parameters to the Function

Page 32: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Building the Condition expression

The DCount function will return the count of all the records in the Client table.

The expression checks if count > 0 If the expression is true then all the

actions displaying ellipses (… (denoting that the above condition also applies to these actions)) in the condition column are invoked – see next slide

Page 33: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Building the Condition expression

If the condition is met then a message is displayed informing the user that the macro has been run

Page 34: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Adding Message Boxes If the expression returns false then all

the actions with ellipses are ignored Below shows another condition which

checks if there are no records – if true then display an appropriate message i.e. No Data to Display

Page 35: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

Adding Macros to Property Events

Another approach to assigning macros is to use the ‘Events’ of a control, form or report at design time

For example, if we only want to display a report if there is data to display we could use the ‘On No Data’ event of the report

Page 36: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

The following report asks the user to enter two date parameters and displays all orders between those dates

Adding Macros to Property Events

Page 37: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

The problem is that if there are no orders between those dates an empty report is displayed

To remedy this we add the following macro to the On No Data event of the report

If there is no data to display the message is displayed and the event is cancelled

Adding Macros to Property Events

Page 38: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

To access the On No Data event of the report, open the report in design view and click the Properties icon on the main menu

Select the ‘Event’ tab and add the macro to the On No Data event

The macro will only run if there is no data

Adding Macros to Property Events

Page 39: Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with

SummaryIn this tutorial we have looked at How to record a macro How to create an Access macro How to associate a macro with an event How to create an AutoExec macro How to associate the AutoExec macro with

startup and the Switchboard How to add conditions to the macro How to create error/information messages in

macros