amp110 microsoft access macros

70
Accelerated Computer Training for Working Professionals Factory 2010 Case Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com Orange Coast Database Associates Specializing in Microsoft Office, Access, SQL, and related technologies Classes custom designed forWorking Professionals http://www.dhdursoassociates.com San Juan Capistrano, CA (800)355-9855 AMP110 Microsoft Access Macros

Upload: dan-durso

Post on 09-May-2015

848 views

Category:

Technology


5 download

DESCRIPTION

AMP110 Access Macros.

TRANSCRIPT

Page 1: AMP110 Microsoft Access Macros

Accelerated Computer Training for Working Professionals Factory 2010 Case

Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com

Orange CoastDatabase Associates

Specializing in Microsoft Office,

Access, SQL, and related technologiesClasses custom designed forWorking Professionals

http://www.dhdursoassociates.com

San Juan Capistrano, CA

(800)355-9855

AMP110 Microsoft Access Macros

Page 2: AMP110 Microsoft Access Macros

Factory AMP1101

AMP110 - Introduction to AccessMacros

Quick introduction to AccessMacros for experienced Windowsusers

P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com

FactoryAMP1102

MS Access Introductory (100 Level)Curriculum

AIN100

AIA101

AMP110Macros

AIN104Reports& Forms

AIN102Queries

AIN100T A, B

Page 3: AMP110 Microsoft Access Macros

FactoryAMP1103

AMP110 - Introduction to AccessMacros

Introduction (s)

Facilities

Course Packet (May vary by course and class)

– Student Questionnaire

– Collaterals (Catalogs, etc.)

– PowerPoint handouts for all sessions

– Evaluation form in back

– Training certificate

FactoryAMP1104

AMP110 – Introduction to AccessMacros

Quick pace for experienced windows users

Assumes prior knowledge of Access equivalentto AIA101/AIN100

End-user, not programmer, oriented

Somewhat structured towards personal use foranalysis and reporting

Page 4: AMP110 Microsoft Access Macros

FactoryAMP1105

AMP110 - Introduction to AccessMacros

Macros (90% of class)

Hot Keys, etc.

Switchboards (2007) orNavigation Forms(2010)

Course Topics:

FactoryAMP1106

Introduction to Access Macros

Industry Standard Approach:

Tables Table Designer

Queries Query designer

Reports Wizard then modify

Forms Wizard then modify

Macros Macro Designer

Access has many ways to accomplish tasks. As an acceleratedcourse we will cover only the most common.

Page 5: AMP110 Microsoft Access Macros

FactoryAMP1107

AMP110 - Introduction to AccessMacros

2 Sessions

Lecture

Demo

Student “hands-on” - by the end of theclass the student will have added macros toa small sample application

Exercises are cumulative – later examplesbuild on objects created earlier

Course Format:

FactoryAMP1108

AMP110 - Introduction to AccessMacros

Session 1

– Macro Concepts

Session 2

– Using Macros

– Switchboards (2007) or

– Navigation Forms (2010)

Course Schedule (3 hour sessions):

Page 6: AMP110 Microsoft Access Macros

Factory Access Macros1

Module – Macro Concepts

P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com

Microsoft Access

FactoryAccess Macros2

Module Outline

Part 1: Basic Macro concepts

Part 2: Form concepts

Part 3: Building a macro “group:

Part 4: Introduction & Keyboardshortcuts

Two parts:

Page 7: AMP110 Microsoft Access Macros

FactoryAccess Macros3

Module Hands On

– Create macros Simple

Run from form

Add where criteria

Add a condition

Add another command

Custom keys

Students “hands-on”:

FactoryAccess Macros4

Sample Application – Factory2000

Simple cost accountingapplication

Employees work on workorders

Clock labor hours for eachwork order

Page 8: AMP110 Microsoft Access Macros

FactoryAccess Macros5

Factory2000 Relational Database Structure

EmpNo fName lName Rate

12 Bob Smith 15

13 Mary Chavez 20

14 Alicia Parks 25

EmpNo Wono Start End Hours

12 A1 1/1/01 1/31/01 20

14 A2 2/1/01 2/28/01 42

14 B3 1/1/01 2/28/01 40

employees

Labor

Wono Descr Std

A1 Casting 30

B3 Fitting 50

C2 Screws 70

Work_orders

FactoryAccess Macros6

Permanent Links

Page 9: AMP110 Microsoft Access Macros

Factory Access Macros7

Microsoft Access

Part 1

Introduction toMacro concepts

FactoryAccess Macros8

Introduction to MacroProgramming

Two ways of “programming” a taskin Access– Macros

– Visual Basic for Applications (VBA)

We will cover Macros primarily

We will also take a look at controlwizards which do use VBA

We do have two Access VBA courses youcan take

Page 10: AMP110 Microsoft Access Macros

Types of Macros

Standalone

Data

Embedded

FactoryAccess Macros9

FactoryAccess Macros10

Introduction to MacroProgramming

Macros are not coded – they aredesigned using a Macro Designer

They run in response to an “event”– like when a form launches or theuser clicks on a button

Page 11: AMP110 Microsoft Access Macros

FactoryAccess Macros11

Some Common Events

Common button event

– “On Click”

Common form events

– On Load

– On Activate

– On Current (record)

– On insert/update/delete (records)

– On Close

FactoryAccess Macros12

Introduction to Macros

Elements of Macros– Actions – what the macro does

– Action arguments – control the specifics of theaction

– Name – Allows you to store multiple macros withina single macro “object”

– Conditions – circumstance under which the macro“fires”

Page 12: AMP110 Microsoft Access Macros

FactoryAccess Macros13

Typical Actions

Apply Filter

Cancel event

Go to control/record

Minimize/Maximize

Open form/report/query

Set value

Select object

Transfer spreadsheet

Transfer text

FactoryAccess Macros14

Macro Design Grid

Page 13: AMP110 Microsoft Access Macros

FactoryAccess Macros15

Introduction to Macros – Actions &Arguments

Create and run a simple macro, mcrFirst,to become familiar with the concepts

– Action = msgbox

– Arguments: “Hello, Macro”

information type

title = “My First Macro”

FactoryAccess Macros16

Designing the First Macro

Page 14: AMP110 Microsoft Access Macros

FactoryAccess Macros17

Designing the First Macro

FactoryAccess Macros18

My First Macro

Page 15: AMP110 Microsoft Access Macros

Design | Macro Tools

FactoryAccess Macros19

Comments

Use thecomment actionto comment yourmacros

Always a goodidea

FactoryAccess Macros20

Page 16: AMP110 Microsoft Access Macros

Submacros

Same idea as a programming subroutine

Used to store common actions that are usedrepeatedly

Submacros are NOT run unless you call themby name

You call them with programming “dotnotation”…

Macroname.submacroname

FactoryAccess Macros21

Submacros

FactoryAccess Macros22

Page 17: AMP110 Microsoft Access Macros

Groups

Another way to create subroutines or blocks ofmacros (submacros)

Hold over from 2007 which we won’t use in thecourse

FactoryAccess Macros23

FactoryAccess Macros24

Conditional Macros (If)

Use if to createa macro thatruns only whencertainconditions aremet

Page 18: AMP110 Microsoft Access Macros

Factory Access Macros25

Microsoft Access

Part 2

Form concepts

FactoryAccess Macros26

Form Events

Forms and controls have “events”

These “fire” on certain actions. Examples:

– Click a button (onclick event)

– Before table is updated (beforeupdate event)

– After table is updated (afterupdate event)

– Etc.

Page 19: AMP110 Microsoft Access Macros

FactoryAccess Macros27

Form Events (cont’d)

When an event “fires” can execute:

– VBA Code

– Macro

This provides a method for “running” anapplication

Macros will appear in the event drop down list

FactoryAccess Macros28

Event List with Macros

Page 20: AMP110 Microsoft Access Macros

Factory Access Macros29

Microsoft Access

Part 3

Building a macroGroup

Building a Macro Group

We will build a series of submacros in a macro.

– Open report

– Open report with filter

– Open report with filter conditionally

These will be run from a form with a buttonusing the OnClick event of the button.

FactoryAccess Macros30

Page 21: AMP110 Microsoft Access Macros

FactoryAccess Macros31

Open Report Macro

Design a simple macro to open(run) a report for the displayedform record

Use rptWork_OrdersFull

Give this macro the namework_Orders Report

Save the “group” as mcrReports

Note: it won’t run right just yet – still hasall records

FactoryAccess Macros32

Designing The Open Report Macro

Page 22: AMP110 Microsoft Access Macros

FactoryAccess Macros33

Invoking the macro

Add a command button to the form

Bring up properties sheet, events tab

Click the drop down for the On Click Event

Select your macro

That’s it!

FactoryAccess Macros34

Use Onclick Event to “Fire” the Macro

Page 23: AMP110 Microsoft Access Macros

FactoryAccess Macros35

Select Macro

FactoryAccess Macros36

Invoking the macro

Open the frmWork_OrdersFinal formin design mode

– Add a command button to the form withcontrol wizard off

– Bring up properties sheet, events tab

– Click the drop down for the On Click Event

– Select your macro

– That’s it!

Save as frmWork_OrdersMacro

(instructor may use AMP110 or other suffix for this class)

Page 24: AMP110 Microsoft Access Macros

FactoryAccess Macros37

Adding Where Conditions to aMacro

Use action argument where

Create a filter like the where clause ofan SQL statement

FactoryAccess Macros38

Using the Expression Builder for the Where Condition

Page 25: AMP110 Microsoft Access Macros

FactoryAccess Macros39

Where Condition (in Zoom Window)

FactoryAccess Macros40

Macros - Adding a where clause

Open mcrOpenReport in design mode

Add a where criteria to preview only the workorder on the screen. Put this in a new submacro.

Click to right of where in macro property sheet

Use the builder to get name of form text control,etc. Your expression will look like this:– [wono]=[Forms]![frmWork_OrdersMacro]!

[Wono]

Save the macro under the same name (instructormay use a slightly different name)

Page 26: AMP110 Microsoft Access Macros

FactoryAccess Macros41

Invoke the macro

Open the frmWork_OrderMacroin design mode

Update the preview button to usethe new submacro

Observe that the form only printsdetail for the work order selectedin the form!

FactoryAccess Macros42

Macro Group w/ where Macroadded

Page 27: AMP110 Microsoft Access Macros

FactoryAccess Macros43

More on Macros

Macros can be invoked depending on“condition”– Uses an if macro

– Macro will run if and only if condition is true

Macros can be run one after another like a“program”. Example:– Do a maketable query , then an append query from

a different file. I.E different downloads from an ERPsystem

FactoryAccess Macros44

Conditional Macros

Modify your mcrOpenReport to check for amissing work order number before doing areport

Add a new action like the mcrFirst to print“Please select a work order number”

Add a condition to each to check to see if wonois null

Use the builder if desired

Name submacro OpenReport_Condition

Save macro group with same name

Page 28: AMP110 Microsoft Access Macros

FactoryAccess Macros45

Using the Builder to Create a Macro Condition

FactoryAccess Macros46

Invoking the Conditional Macro

Modify the formfrmWorkOrders_Macro torun the new macro

Save asfrmWorkOrders_MacroFinal

Test your new form andmacro

Page 29: AMP110 Microsoft Access Macros

FactoryAccess Macros47

Macro Group with new conditionalmacro

FactoryAccess Macros48

Adding Commands to a Macro

Move focus on form back to wonowhen an error occurs

Add a go to control action

Save your macro asmcrOpen_Report_Final

Page 30: AMP110 Microsoft Access Macros

FactoryAccess Macros49

Final Submacro in Design Mode

FactoryAccess Macros50

Macro Messages

Often want to suppress messages whenrunning a macro

– Set warnings off

– Echo off

Page 31: AMP110 Microsoft Access Macros

Factory Access Macros51

Microsoft Access

Part 4

Custom keyAssignments

FactoryAccess Macros52

Custom Key Assignments

Can customize keyboard shortcuts in twoways:

– Access keys

– Key Assignment macros

Page 32: AMP110 Microsoft Access Macros

FactoryAccess Macros53

Access Keys (Hot Keys)

FactoryAccess Macros54

Custom Key Assignments

Access has many built-in keyboard shortcuts:CTRL-G to open Debug Window, F11 todisplay Database Window, etc. See Help.

Can also create your own using macros – wewill cover this in module 2

Page 33: AMP110 Microsoft Access Macros

Factory Access Macros55

Microsoft Access

End of Module

P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com

Page 34: AMP110 Microsoft Access Macros

Factory Access - Using Macros1

Module – Using Macros to AutomateYour Application

P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com

Microsoft Access

FactoryAccess - Using Macros2

Module Hands On

– Create macros Imports

Exports

Startup (AutoExec)

AutoKeys

Set Values on a form

Data Macros

Macros to code

– Use the control wizard to close aform

Students “hands-on”:

Page 35: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros3

Introduction to Macros

Two ways of “programming” a taskin Access– Macros

– Visual Basic for Applications (VBA)

We will cover Macros primarily

We will also take a look at controlwizards which do use VBA

FactoryAccess - Using Macros4

Introduction to Macros

Elements of Macros– Events – what “fires” the macro

– Actions – what the macro does

– Action arguments – control the specifics of the action

– Conditions – circumstance under which the macro “fires”

Macro design grid – how macro is defined

Page 36: AMP110 Microsoft Access Macros

Macro Designer

FactoryAccess - Using Macros5

FactoryAccess - Using Macros6

Some Available Actions

Apply Filter

Find

Go

Max/minimize

Open

Run

Set

Transfer

Page 37: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros7

Form Events

Forms and controls have “events”

These “fire” on certain actions. Examples:

– Click a button (onclick event)

– Before table is updated (beforeupdate event)

– Before a record is deleted

– After table is updated (afterupdate event)

– After the user leaves a control

– Etc.

FactoryAccess - Using Macros8

Form Events (cont’d)

When an event “fires” can execute:

– VBA Code

– Macro

This provides a method for “running” anapplication

Page 38: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros9

Import/Export Form

Create a simple, unbound form named frmData

We will use it to drive a series of very usefulimport and export macros

FactoryAccess - Using Macros10

Import/Export Form

Page 39: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros11

Export Macro

Design a simple macro toexport our three main tablesto text files – use transfer text

Export order is not critical

Let’s put them into a datamacro group

Group name mcrData

Macro name export_all

FactoryAccess - Using Macros12

Designing The Export Macro

Note: can use saved spec

Page 40: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros13

Invoking the macro

Open the frmData form in design mode

– Add a command button to the form with wizardenabled

– Select run macro

– Select your macro

– That’s it!

Save with same name (instructor will use adifferent name for this working form)

FactoryAccess - Using Macros14

Use Control Wizard to “Fire” the Macro

Page 41: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros15

Select Your Macro

FactoryAccess - Using Macros16

New Data Form with Macro Button

Page 42: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros17

Export Your Data

Run the export macro – check for files

FactoryAccess - Using Macros18

Macros - Adding the Imports

Open mcrOpenData in design mode

Add the imports

Call the macro import_all

Link the two parent text files towork_orders_linked and employees_linked

Import the labor_export text file tolabor_import

Page 43: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros19

Creating the Import Macro

FactoryAccess - Using Macros20

Invoke the macro

Open the frmData form

Add a button to run the importmacro

Click on it

Observe that the tables in yourdatabase are updated!

Page 44: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros21

Updated Data Form

FactoryAccess - Using Macros22

New Tables after Running Macro

Page 45: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros23

Custom Key Assignments

Key combination goes in macro name field

Use Send Keys codes to create the macro

Can have conditions if desired, too

FactoryAccess - Using Macros24

Create Import/Export AutoKeys

Page 46: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros25

Automating Startup

Put VBA code in the form Open and/or Loadevent, or

Use a startup (old DOS autoexec concept)macro

– Create a macro with the name Autoexec – it will“fire” when your app starts. Hold down the shift keyto bypass it.

FactoryAccess - Using Macros26

Autoexec Macro

Page 47: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros27

Create an AutoExec Macro

Modify your import/export macro

Keep the import part only

Save as autoexec

Delete your linked tables and the labor import

Close and restart your database – notice thetables were added again automatically

FactoryAccess - Using Macros28

Setting a Value

Set value can be used to do calculations, etc.

Two action arguments– The item that will have its value set

– The expression to put in

Page 48: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros29

First Add an Overhead Control

Add an unbound text box to thefrmwork_OrdersMacro form

Call it txtOverhead

FactoryAccess - Using Macros30

Form with Overhead Control

Page 49: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros31

Create Set Value Macro

FactoryAccess - Using Macros32

Item to set

Page 50: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros33

Value Expression

FactoryAccess - Using Macros34

Invoke the Macro

Use the form’s on current event

This fires whenever we move to a new record

When the event fires run the macro

Page 51: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros35

Invoke the Macro

Note: thisIs a form event

FactoryAccess - Using Macros36

Using the Macro

Page 52: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros37

Using Set Value

Update frmData_Macro with two newcontrols…

– Checkbox which says wether or not to hide the workorder selection text box

– Textbox for work order

Add a macro which will hide and show theabove text box

FactoryAccess - Using Macros38

Adding the Macro

Page 53: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros39

Item to Set

FactoryAccess - Using Macros40

Value Expression to Hide/Unhide

Page 54: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros41

Form with New Controls

Data Macros

Data macros fire when a certain event takesplace within a table – similar to a trigger inOracle or SQL Server

– After insert

– After update

– Etc.

FactoryAccess - Using Macros42

Page 55: AMP110 Microsoft Access Macros

Data Macros

Add an overtime rate field to the employeestable

Add a data macro to update this field when theregular time rate changes

FactoryAccess - Using Macros43

Data Macros

FactoryAccess - Using Macros44

Page 56: AMP110 Microsoft Access Macros

Before Change Macro

FactoryAccess - Using Macros45

Table with Data Macro

FactoryAccess - Using Macros46

Page 57: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros47

Macros to Code

Can convert macros to VBA code

A handy way to get your VBA subroutinestarted

In general, programmers will use code, notmacros

But as we have seen Macros can be quitehandy

FactoryAccess - Using Macros48

Macros to Code

Page 58: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros49

Macro to Code

FactoryAccess - Using Macros50

Add a Close Button

Open frmData_Macro in designmode

Add a close form button

Select close form fromoperations

Run the form

That’s it!

Page 59: AMP110 Microsoft Access Macros

FactoryAccess - Using Macros51

Final Data Macro Form

Factory Access - Using Macros52

Microsoft Access

End of Module

P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com

Please fill out and turn in yourend-of-course evaluations ifthis is your last module

Page 60: AMP110 Microsoft Access Macros

Factory Access Navigation1

Module – Navigation

P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com

Microsoft Access

Access navigation

Newer Access 2010 databases use the newNavigation forms

Older MDB databases still have theSwitchboard Manager tool available

FactoryAccess Navigation2

Page 61: AMP110 Microsoft Access Macros

Creating a Navigation form

Select the desired style

FactoryAccess Navigation3

Navigation Forms

Drag and drop forms and reports one by one

FactoryAccess Navigation4

Page 62: AMP110 Microsoft Access Macros

Adjust properties

Adjust properties as desired and save asfrmNavigation

FactoryAccess Navigation5

Set startup options (File | Options)

Set the Access options to open the navigationform when it starts up

FactoryAccess Navigation6

Page 63: AMP110 Microsoft Access Macros

FactoryAccess Navigation7

Navigation Form

Create a navigation form foryour application

FactoryAccess Navigation8

Switchboards

Create via a switchboard manager tool

Creates a tree of launch forms – mainform is called switchboard and is placedwith your other form objects

Page 64: AMP110 Microsoft Access Macros

FactoryAccess Navigation9

Launching Switchboard Manager

FactoryAccess Navigation10

Switchboard Manager

Yes – we want to create a new switchboard!

Page 65: AMP110 Microsoft Access Macros

FactoryAccess Navigation11

Switchboard Manager

FactoryAccess Navigation12

Switchboards

Create a switchboard for yourapplication

Page 66: AMP110 Microsoft Access Macros

FactoryAccess Switchboards13

Add a macro to minimize databasewindow

Create macro (add to mcrForm macro group)

Invoke on form load of switchboard form

FactoryAccess Switchboards14

Updated mcrForm

Page 67: AMP110 Microsoft Access Macros

FactoryAccess Switchboards15

Add to main switchboard loadevent

FactoryAccess Switchboards16

Switchboard test

Close your database

Reopen your database – the database windowshould be minimized

Page 68: AMP110 Microsoft Access Macros

Factory Access Navigation17

Microsoft Access

End of Module

P.O. Box 6142Laguna Niguel, CA 92607949-489-1472http://www.d2associates.com

Please fill out and turn in yourend-of-course evaluations.

Notes

FactoryAccess Navigation18

Page 69: AMP110 Microsoft Access Macros

Notes

FactoryAccess Navigation19

Notes

FactoryAccess Navigation20

Page 70: AMP110 Microsoft Access Macros

Accelerated Computer Training for Working Professionals

Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com

Orange CoastDatabase Associates

Specializing in Microsoft Office,

Access, SQL, and related technologiesComputer Training, Programming & Consulting

32422 Alipaz St., Suite A-15

San Juan Capistrano, CA

(800)355-9855 (Toll Free) | (949)489-1472 (Direct) | (949)485-6284 (Fax)

http://www.dhdursoassociates.com | [email protected]

Accelerated Computer Training