Office
 

Microsoft Access 2010 : Learning the Basics of Creating and Running a Macro (part 2) - Submacros, Program Flow

10/10/2011 9:22:26 AM

Submacros

Submacros are like subroutines in a programming module; they allow you to place more than one routine in a macro. This means you can create many macro routines without having to create several separate macros. You should include submacros that perform related functions within one particular macro. For example, you might build a macro that contains all the routines required for form handling and another that has all the routines needed for report handling.

Only two steps are needed to add submacros to a macro:

1.
Click and drag a submacro from the Action Catalog onto the macro. A submacro appears as in Figure 7.

Figure 7. The submacro allows you to create subroutines within a macro.

2.
Add macro actions to each submacro. Figure 8 shows a macro with three submacros: OpenCustomers, OpenOrders, and CloseForm. The OpenCustomers submacro opens the Customer List form, showing all customers. The OpenOrders submacro opens the OrderList form displaying only orders placed in March and April of 2006, and the CloseForm submacro displays a message to the user and then closes the active window.
Figure 8. A macro with three submacros.

Program Flow

At times, you want a macro action to execute only when a certain condition is true. Fortunately, Access allows you to specify the conditions under which a macro action executes:

1.
Make sure that the Action Catalog is visible. (If it isn’t, click the Action Catalog tool in the Show/Hide group of the Macro Tools Design tab of the Ribbon.)

2.
Click and drag the If statement to the macro. It will appear as in Figure 9.

Figure 9. You can designate the condition under which a macro action executes by selecting If from the Program Flow node of the Action Catalog.

The macro pictured in Figure 10 evaluates information entered on a form. The macro evaluates the date entered in the txtBirthDate text box on the frmPersonalInfo form. Here’s the expression entered in the first condition:

DateDiff("yyyy",[Forms]![frmPersonalInfo]![txtBirthDate],Date()) Between 25 And 49


Figure 10. An example of a macro containing conditions.

This expression uses the DateDiff function to determine the difference between the date entered in the txtBirthDate text box and the current date. If the difference between the two dates is between 25 and 49 years, a message box is displayed indicating that the person is over a quarter century old.

If the first condition isn’t satisfied, the macro continues evaluating each condition. The CheckBirthDate subroutine displays an age-specific message for each person 25 years of age and older. If the person is younger than 25, none of the conditions are met, and no message is displayed.

The CheckGender macro works a little bit differently (see Figure 11). It evaluates the value of the fraGender option group. One of the first two lines of the subroutine execute, depending on whether the first or second option button is selected. The third line of the subroutine executes regardless of the Option Group value because it is after the End If.

Figure 11. An example of a macro containing conditions.
 
Others
 
- Microsoft Access 2010 : Learning the Basics of Creating and Running a Macro (part 1) - Working with Macro Actions & Working with Action Arguments
- Microsoft Access 2010 : Deleting Records
- Microsoft Visio 2010 : Sharing Diagrams by Using the Visio Viewer
- Microsoft Visio 2010 : Creating Visio Templates
- Microsoft Word 2010 : Creating an XML Document
- Microsoft Word 2010 : Customizing Word - Linking and Embedding Files
- Microsoft PowerPoint 2010 : Saving a Presentation as a Slide Show & Saving a Presentation as a Video
- Microsoft PowerPoint 2010 : Delivering a Show on Multiple Monitors
- Getting Started with Microsoft Excel 2010 : Arranging Windows
- Getting Started with Microsoft Excel 2010 : Moving Around the Workbook
- Microsoft Outlook 2010 : Working with the Ribbon and the Backstage View
- Microsoft Outlook 2010 : Working in the Outlook Program Window
- Microsoft OneOnte 2010 : Creating Pages - Choose the Default Page Template for Section Pages
- Microsoft OneOnte 2010 : Creating Sections & Creating Section Groups
- Facilitating Your Plan with Microsoft Project 2010
- Microsoft Project 2010 : Understanding Project Management Basics
- Microsoft Access 2010 : Working with Table Data
- Microsoft Access 2010 : Working with an Existing Database
- Microsoft Visio 2010 : Sharing and Publishing Diagrams - Saving in Other File Formats
- Microsoft Visio 2010 : Sharing and Publishing Diagrams - Creating Graphics
 
 
Most View
 
- Microsoft PowerPoint 2010 : Preparing a Slide Show - Working with Fonts
- Exchange Server 2013 : The Exchange Management Shell - EMS basics (part 4) - Identities, Piping
- Windows Server 2008 R2 high-availability and recovery features : Planning for High Availability
- Windows 8 : Customizing the Start Screen (part 3) - Turning Off a Live Tile, Pinning a Program to the Start Screen
- Feature Overview and Benefits of Microsoft Lync Server 2013 : Remote Access
- Microsoft Visio 2010 : Saving Visio Files in XML Format, Saving Files in Older Visio Formats
- Sharepoint 2013 : Introducing SharePoint Apps - Understanding the new SharePoint app model
- Active Directory 2008 : Implementing Sites and Subnets (part 2) - Creating Subnets
- Windows 7 : Programming Drivers for the Kernel Mode Driver Framework (part 4) - Creating a WDF Driver Object: DriverEntry
- Microsoft Lync Server 2013 Edge Server : Edge Server Administration (part 4) - Managing Public Providers
 
 
Top 10
 
- Securing an Exchange Server 2007 Environment : Securing Outlook Web Access
- Securing an Exchange Server 2007 Environment : Protecting Against Spam (part 2) - Filtering Junk Mail
- Securing an Exchange Server 2007 Environment : Protecting Against Spam (part 2) - Filtering Junk Mail
- Securing an Exchange Server 2007 Environment : Protecting Against Spam (part 1) - Protecting Against Web Beaconing
- Securing an Exchange Server 2007 Environment : Securing Outlook 2007 (part 2) - Encrypting Communications Between Outlook and Exchange , Blocking Attachments
- Securing an Exchange Server 2007 Environment : Securing Outlook 2007 (part 1) - Outlook Anywhere
- Securing an Exchange Server 2007 Environment : Securing Your Windows Environment (part 3) - Keeping Up with Security Patches and Updates
- Securing an Exchange Server 2007 Environment : Securing Your Windows Environment (part 2) - Utilizing Security Templates
- Securing an Exchange Server 2007 Environment : Securing Your Windows Environment (part 1) - Windows Server 2003 Security Improvements , Windows Vista Security Improvements
- Securing an Exchange Server 2007 Environment : Client-Level Secured Messaging - Exchange Server 2007 Client-Level Security Enhancements