AMP110 Microsoft Access Macros

1,234 views

Published on

AMP110 Access Macros.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,234
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
68
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

AMP110 Microsoft Access Macros

  1. 1. Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com AMP110 Microsoft Access Macros Orange Coast Database Associates Specializing in Microsoft Office, Access, SQL, and related technologies Classes custom designed for Working Professionals http://www.dhdursoassociates.com San Juan Capistrano, CA (800)355-9855Accelerated Computer Training for Working Professionals Factory 2010 Case
  2. 2. AMP110 - Introduction to Access Macros Quick introduction to Access Macros for experienced Windows users P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com1 Factory AMP110 MS Access Introductory (100 Level) Curriculum AIN100 AIN100T A, B AIA101 AIN104 AIN102 AMP110 Reports Queries Macros & Forms2 AMP110 Factory
  3. 3. AMP110 - Introduction to Access Macros  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 certificate3 AMP110 Factory AMP110 – Introduction to Access Macros  Quick pace for experienced windows users  Assumes prior knowledge of Access equivalent to AIA101/AIN100  End-user, not programmer, oriented  Somewhat structured towards personal use for analysis and reporting4 AMP110 Factory
  4. 4. AMP110 - Introduction to Access Macros Course Topics:  Macros (90% of class)  Hot Keys, etc.  Switchboards (2007) or Navigation Forms(2010)5 AMP110 Factory Introduction to Access Macros Industry Standard Approach: Access has many ways to accomplish tasks. As an accelerated course we will cover only the most common. Tables Table Designer Queries Query designer Reports Wizard then modify Forms Wizard then modify Macros Macro Designer6 AMP110 Factory
  5. 5. AMP110 - Introduction to Access Macros Course Format:  2 Sessions  Lecture  Demo  Student “hands-on” - by the end of the class the student will have added macros to a small sample application  Exercises are cumulative – later examples build on objects created earlier7 AMP110 Factory AMP110 - Introduction to Access Macros Course Schedule (3 hour sessions):  Session 1 – Macro Concepts  Session 2 – Using Macros – Switchboards (2007) or – Navigation Forms (2010)8 AMP110 Factory
  6. 6. Microsoft Access Module – Macro Concepts P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com1 Factory Access Macros Module Outline Two parts:  Part 1: Basic Macro concepts  Part 2: Form concepts  Part 3: Building a macro “group:  Part 4: Introduction & Keyboard shortcuts2 Access Macros Factory
  7. 7. Module Hands On Students “hands-on”: – Create macros  Simple  Run from form  Add where criteria  Add a condition  Add another command  Custom keys3 Access Macros Factory Sample Application – Factory2000  Simple cost accounting application  Employees work on work orders  Clock labor hours for each work order4 Access Macros Factory
  8. 8. Factory2000 Relational Database Structure EmpNo fName lName Rate Wono Descr Std 12 Bob Smith 15 A1 Casting 30 13 Mary Chavez 20 B3 Fitting 50 14 Alicia Parks 25 C2 Screws 70 employees Work_orders EmpNo Wono Start End Hours 12 A1 1/1/01 1/31/01 20 14 A2 2/1/01 2/28/01 42 Labor 14 B3 1/1/01 2/28/01 405 Access Macros Factory Permanent Links6 Access Macros Factory
  9. 9. Microsoft Access Part 1 Introduction to Macro concepts7 Factory Access Macros Introduction to Macro Programming  Two ways of “programming” a task in Access – Macros – Visual Basic for Applications (VBA)  We will cover Macros primarily  We will also take a look at control wizards which do use VBA  We do have two Access VBA courses you can take 8 Access Macros Factory
  10. 10. Types of Macros  Standalone  Data  Embedded9 Access Macros Factory Introduction to Macro Programming  Macros are not coded – they are designed using a Macro Designer  They run in response to an “event” – like when a form launches or the user clicks on a button10 Access Macros Factory
  11. 11. Some Common Events  Common button event – “On Click”  Common form events – On Load – On Activate – On Current (record) – On insert/update/delete (records) – On Close11 Access Macros Factory Introduction to Macros  Elements of Macros – Actions – what the macro does – Action arguments – control the specifics of the action – Name – Allows you to store multiple macros within a single macro “object” – Conditions – circumstance under which the macro “fires”12 Access Macros Factory
  12. 12. Typical Actions  Apply Filter  Cancel event  Go to control/record  Minimize/Maximize  Open form/report/query  Set value  Select object  Transfer spreadsheet  Transfer text13 Access Macros Factory Macro Design Grid14 Access Macros Factory
  13. 13. 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”15 Access Macros Factory Designing the First Macro16 Access Macros Factory
  14. 14. Designing the First Macro17 Access Macros Factory My First Macro18 Access Macros Factory
  15. 15. Design | Macro Tools19 Access Macros Factory Comments  Use the comment action to comment your macros  Always a good idea20 Access Macros Factory
  16. 16. Submacros  Same idea as a programming subroutine  Used to store common actions that are used repeatedly  Submacros are NOT run unless you call them by name  You call them with programming “dot notation”… Macroname.submacroname21 Access Macros Factory Submacros22 Access Macros Factory
  17. 17. Groups  Another way to create subroutines or blocks of macros (submacros)  Hold over from 2007 which we won’t use in the course23 Access Macros Factory Conditional Macros (If)  Use if to create a macro that runs only when certain conditions are met24 Access Macros Factory
  18. 18. Microsoft Access Part 2 Form concepts25 Factory Access Macros 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.26 Access Macros Factory
  19. 19. Form Events (cont’d)  When an event “fires” can execute: – VBA Code – Macro  This provides a method for “running” an application  Macros will appear in the event drop down list27 Access Macros Factory Event List with Macros28 Access Macros Factory
  20. 20. Microsoft Access Part 3 Building a macro Group29 Factory Access Macros 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 button using the OnClick event of the button.30 Access Macros Factory
  21. 21. Open Report Macro  Design a simple macro to open (run) a report for the displayed form record  Use rptWork_OrdersFull  Give this macro the name work_Orders Report  Save the “group” as mcrReports Note: it won’t run right just yet – still has all records31 Access Macros Factory Designing The Open Report Macro32 Access Macros Factory
  22. 22. 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!33 Access Macros Factory Use Onclick Event to “Fire” the Macro34 Access Macros Factory
  23. 23. Select Macro35 Access Macros Factory Invoking the macro  Open the frmWork_OrdersFinal form in design mode – Add a command button to the form with control 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)36 Access Macros Factory
  24. 24. Adding Where Conditions to a Macro  Use action argument where  Create a filter like the where clause of an SQL statement37 Access Macros Factory Using the Expression Builder for the Where Condition38 Access Macros Factory
  25. 25. Where Condition (in Zoom Window)39 Access Macros Factory Macros - Adding a where clause  Open mcrOpenReport in design mode  Add a where criteria to preview only the work order 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 (instructor may use a slightly different name)40 Access Macros Factory
  26. 26. Invoke the macro  Open the frmWork_OrderMacro in design mode  Update the preview button to use the new submacro  Observe that the form only prints detail for the work order selected in the form!41 Access Macros Factory Macro Group w/ where Macro added42 Access Macros Factory
  27. 27. 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 ERP system43 Access Macros Factory Conditional Macros  Modify your mcrOpenReport to check for a missing work order number before doing a report  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 wono is null  Use the builder if desired  Name submacro OpenReport_Condition  Save macro group with same name44 Access Macros Factory
  28. 28. Using the Builder to Create a Macro Condition45 Access Macros Factory Invoking the Conditional Macro  Modify the form frmWorkOrders_Macro to run the new macro  Save as frmWorkOrders_MacroFinal  Test your new form and macro46 Access Macros Factory
  29. 29. Macro Group with new conditional macro47 Access Macros Factory Adding Commands to a Macro  Move focus on form back to wono when an error occurs  Add a go to control action  Save your macro as mcrOpen_Report_Final48 Access Macros Factory
  30. 30. Final Submacro in Design Mode49 Access Macros Factory Macro Messages  Often want to suppress messages when running a macro – Set warnings off – Echo off50 Access Macros Factory
  31. 31. Microsoft Access Part 4 Custom key Assignments51 Factory Access Macros Custom Key Assignments  Can customize keyboard shortcuts in two ways: – Access keys – Key Assignment macros52 Access Macros Factory
  32. 32. Access Keys (Hot Keys)53 Access Macros Factory Custom Key Assignments  Access has many built-in keyboard shortcuts: CTRL-G to open Debug Window, F11 to display Database Window, etc. See Help.  Can also create your own using macros – we will cover this in module 254 Access Macros Factory
  33. 33. Microsoft Access End of Module P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com55 Factory Access Macros
  34. 34. Microsoft Access Module – Using Macros to Automate Your Application P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com1 Factory Access - Using Macros Module Hands On Students “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 a form2 Access - Using Macros Factory
  35. 35. Introduction to Macros  Two ways of “programming” a task in Access – Macros – Visual Basic for Applications (VBA)  We will cover Macros primarily  We will also take a look at control wizards which do use VBA3 Access - Using Macros Factory 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 defined4 Access - Using Macros Factory
  36. 36. Macro Designer5 Access - Using Macros Factory Some Available Actions  Apply Filter  Find  Go  Max/minimize  Open  Run  Set  Transfer6 Access - Using Macros Factory
  37. 37. 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.7 Access - Using Macros Factory Form Events (cont’d)  When an event “fires” can execute: – VBA Code – Macro  This provides a method for “running” an application8 Access - Using Macros Factory
  38. 38. Import/Export Form  Create a simple, unbound form named frmData  We will use it to drive a series of very useful import and export macros9 Access - Using Macros Factory Import/Export Form10 Access - Using Macros Factory
  39. 39. Export Macro  Design a simple macro to export our three main tables to text files – use transfer text  Export order is not critical  Let’s put them into a data macro group  Group name mcrData  Macro name export_all11 Access - Using Macros Factory Designing The Export Macro Note: can use saved spec12 Access - Using Macros Factory
  40. 40. Invoking the macro  Open the frmData form in design mode – Add a command button to the form with wizard enabled – Select run macro – Select your macro – That’s it!  Save with same name (instructor will use a different name for this working form)13 Access - Using Macros Factory Use Control Wizard to “Fire” the Macro14 Access - Using Macros Factory
  41. 41. Select Your Macro15 Access - Using Macros Factory New Data Form with Macro Button16 Access - Using Macros Factory
  42. 42. Export Your Data  Run the export macro – check for files17 Access - Using Macros Factory Macros - Adding the Imports  Open mcrOpenData in design mode  Add the imports  Call the macro import_all  Link the two parent text files to work_orders_linked and employees _linked  Import the labor_export text file to labor_import18 Access - Using Macros Factory
  43. 43. Creating the Import Macro19 Access - Using Macros Factory Invoke the macro  Open the frmData form  Add a button to run the import macro  Click on it  Observe that the tables in your database are updated!20 Access - Using Macros Factory
  44. 44. Updated Data Form21 Access - Using Macros Factory New Tables after Running Macro22 Access - Using Macros Factory
  45. 45. Custom Key Assignments  Key combination goes in macro name field  Use Send Keys codes to create the macro  Can have conditions if desired, too23 Access - Using Macros Factory Create Import/Export AutoKeys24 Access - Using Macros Factory
  46. 46. Automating Startup  Put VBA code in the form Open and/or Load event, 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 key to bypass it.25 Access - Using Macros Factory Autoexec Macro26 Access - Using Macros Factory
  47. 47. 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 the tables were added again automatically27 Access - Using Macros Factory 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 in28 Access - Using Macros Factory
  48. 48. First Add an Overhead Control  Add an unbound text box to the frmwork_OrdersMacro form  Call it txtOverhead29 Access - Using Macros Factory Form with Overhead Control30 Access - Using Macros Factory
  49. 49. Create Set Value Macro31 Access - Using Macros Factory Item to set32 Access - Using Macros Factory
  50. 50. Value Expression33 Access - Using Macros Factory 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 macro34 Access - Using Macros Factory
  51. 51. Invoke the Macro Note: this Is a form event35 Access - Using Macros Factory Using the Macro36 Access - Using Macros Factory
  52. 52. Using Set Value  Update frmData_Macro with two new controls… – Checkbox which says wether or not to hide the work order selection text box – Textbox for work order  Add a macro which will hide and show the above text box37 Access - Using Macros Factory Adding the Macro38 Access - Using Macros Factory
  53. 53. Item to Set39 Access - Using Macros Factory Value Expression to Hide/Unhide40 Access - Using Macros Factory
  54. 54. Form with New Controls41 Access - Using Macros Factory Data Macros  Data macros fire when a certain event takes place within a table – similar to a trigger in Oracle or SQL Server – After insert – After update – Etc.42 Access - Using Macros Factory
  55. 55. Data Macros  Add an overtime rate field to the employees table  Add a data macro to update this field when the regular time rate changes43 Access - Using Macros Factory Data Macros44 Access - Using Macros Factory
  56. 56. Before Change Macro45 Access - Using Macros Factory Table with Data Macro46 Access - Using Macros Factory
  57. 57. Macros to Code  Can convert macros to VBA code  A handy way to get your VBA subroutine started  In general, programmers will use code, not macros  But as we have seen Macros can be quite handy47 Access - Using Macros Factory Macros to Code48 Access - Using Macros Factory
  58. 58. Macro to Code49 Access - Using Macros Factory Add a Close Button  Open frmData_Macro in design mode  Add a close form button  Select close form from operations  Run the form  That’s it!50 Access - Using Macros Factory
  59. 59. Final Data Macro Form51 Access - Using Macros Factory Microsoft Access End of Module P.O. Box 6142 Please fill out and turn in your Laguna Niguel, CA 92607 949-489-1472 end-of-course evaluations if http://www.d2associates.com this is your last module52 Factory Access - Using Macros
  60. 60. Microsoft Access Module – Navigation P.O. Box 6142 Laguna Niguel, CA 92607 949-489-1472 http://www.d2associates.com1 Factory Access Navigation Access navigation  Newer Access 2010 databases use the new Navigation forms  Older MDB databases still have the Switchboard Manager tool available2 Access Navigation Factory
  61. 61. Creating a Navigation form  Select the desired style3 Access Navigation Factory Navigation Forms  Drag and drop forms and reports one by one4 Access Navigation Factory
  62. 62. Adjust properties  Adjust properties as desired and save as frmNavigation5 Access Navigation Factory Set startup options (File | Options)  Set the Access options to open the navigation form when it starts up6 Access Navigation Factory
  63. 63. Navigation Form  Create a navigation form for your application7 Access Navigation Factory Switchboards  Create via a switchboard manager tool  Creates a tree of launch forms – main form is called switchboard and is placed with your other form objects8 Access Navigation Factory
  64. 64. Launching Switchboard Manager9 Access Navigation Factory Switchboard Manager  Yes – we want to create a new switchboard!10 Access Navigation Factory
  65. 65. Switchboard Manager11 Access Navigation Factory Switchboards  Create a switchboard for your application12 Access Navigation Factory
  66. 66. Add a macro to minimize database window  Create macro (add to mcrForm macro group)  Invoke on form load of switchboard form13 Access Switchboards Factory Updated mcrForm14 Access Switchboards Factory
  67. 67. Add to main switchboard load event15 Access Switchboards Factory Switchboard test  Close your database  Reopen your database – the database window should be minimized16 Access Switchboards Factory
  68. 68. Microsoft Access End of Module P.O. Box 6142 Laguna Niguel, CA 92607 Please fill out and turn in your 949-489-1472 end-of-course evaluations. http://www.d2associates.com17 Factory Access Navigation Notes18 Access Navigation Factory
  69. 69. Notes19 Access Navigation Factory Notes20 Access Navigation Factory
  70. 70. Orange Coast Database Associates Course (800)355-9855 or http://ocdatabases.itgo.com Accelerated Computer Training Orange Coast Database Associates Specializing in Microsoft Office, Access, SQL, and related technologies Computer 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 | sales@dhdursoassociates.comAccelerated Computer Training for Working Professionals

×