home Java Script MS Access Perl HTML Delphi C ++ Visual Basic Java CGIPerl MS Excel Front Page 98 Windows 98 Ms Word Builder PHP Assembler Link to us Links |
Although macros should not be used to develop the routines that control your applications, a few specific application-development tasks can be accomplished only by using macros. It is therefore important to understand at least the basics of how macros work. Furthermore, using Access 95 macros can often help you to get started with the application-development process because Access 95 macros can be converted to VBA code. This means that you can develop part of your application using macros, convert the macros to VBA code, and proceed with the development process. Although I don't recommend this approach for serious developers, it provides a great jump-start for those new to Access or Windows development in general.
To create a macro, click the Macros tab and then click New. The Macro Design window pictured in Figure 11.1 appears. This window allows you to build a "program" by adding macro actions, arguments, names, and conditions to the macro.
Figure 11.1. The Macro Design window showing the macro action and comments columns.
Macro actions are like programming commands or functions. They instruct Access to take a specific actionfor example, to open a form. Macro arguments are like parameters to a command or function. They provide Access with specifics regarding the selected action. For example, if the macro action instructs Access to open a form, the arguments for that action tell Access which form is to be opened and how it is to be opened (Form, Design, or Datasheet view or Print Preview). Macro names are like subroutines. Multiple subroutines can be included within one Access macro. Each of these routines is identified by its macro name. Macro conditions allow you to determine when a specific macro action will execute. For example, you might want one form to open in one situation and a second form to open in another situation.
As mentioned, macro actions instruct Access to perform a task. You can add a macro action to the Macro Design window in several ways. One method is to click a cell within the Macro Action column and then click to open the drop-down menu (see Figure 11.2). A list of all the macro actions appears. Select the desired action from the list, and it is added instantly to the macro. Use this method of selecting a macro action if you are not sure of the name of the macro action and want to browse the available actions.
Figure 11.2. The Macro Action drop-down showing all of the available macro actions.
After you have been working with macros for a while, you'll know which actions you want to select. Rather than opening the drop-down and scrolling through the entire list of actions, you can click a cell within the Action column and then begin to type the name of the macro action you want to add. Access locates the first macro action beginning with the character(s) you type.
The OpenTable, OpenQuery, OpenForm, OpenReport, and OpenModule actions are used to open a table, query, form, report, or module, respectively. These actions and associated arguments can all be filled in quite easily using a drag-and-drop technique:
Figure 11.3. The Database window and Macro Design window tiled.
Figure 11.4. The Macro Design window after the frmClients form was dragged and dropped on it.
The process of dragging and dropping a table, query, form, report, or module onto the Macro Design window saves you some time because all the macro action arguments are automatically filled in for you. Notice in Figure 11.4 that six action arguments are associated with the OpenForm action: Form Name, View, Filter Name, Where Condition, Data Mode, and Window Mode. The process of dragging and dropping the frmClients form to the Macro Design window filled in four of the arguments for the OpenForm action. The name of the form (frmClients), the view (Form), the data mode (edit), and the window mode (normal) were all automatically filled in. Macro action arguments are covered more thoroughly in the next section.
A macro action argument is like a parameter to a command or function. The macro action arguments that are available differ depending on what macro action has been selected. The macro action arguments give Access specific instructions on how to execute the selected macro action. Some macro action arguments force you to select from a drop-down containing appropriate choices; others allow you to enter a valid Access expression. Macro action arguments are automatically filled in when you click and drag a Table, Query, Form, Report, or Module object to the Macro Design window. In all other situations, you must supply Access with the arguments required to properly execute a macro action. To specify a macro action argument, follow five steps:
Figure 11.5. Available choices for OpenForm argument.
Figure 11.6. The Expression Builder dialog allows you to easily add complex expressions to your macros.
Figure 11.7. Completed expression for Where argument of OpenForm action.
It is important to remember that each macro action has different macro action arguments. Some of the macro action arguments associated with a particular macro action are required, whereas others are optional. If you need help on a particular macro action argument, click the specific argument. Access provides you with a short description of the selected argument. If you need additional assistance, press F1; Help appears for the macro action and all its arguments (see Figure 11.8).
Figure 11.8. Help on OpenForm action.
Macro names are like subroutines. They allow you to place more than one routine within a macro. This means that you can create many macro routines without having to create a large volume of separate macros. It is appropriate to include macros 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 contains all the routines required for report handling.
Only two steps are needed to add macro names to a macro:
Figure 11.9. The Macro Names column allows you to create subroutines within a macro.
Figure 11.10. A macro containing three subroutines.
At times you'll 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:
Figure 11.11. The Conditions column of a macro allows you to designate a condition under which a macro action executes.
The macro pictured in Figure 11.12 evaluates information entered on a form. The CheckBirthDate subroutine evaluates the date entered in the txtBirthDate text box. Here is the expression entered in the first condition:
DateDiff("yyyy",[Forms]![frmPersonalInfo]![txtBirthDate],Date()) Between 25 And 49
Figure 11.12. 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 individual is over a quarter century old.
Notice the ellipsis on the second line of the CheckBirthDate subroutine. The ellipsis indicates to Access that the macro action should be executed only if the condition entered on the previous line is True. In this case, if the condition is True, the macro is terminated.
If the first condition is not satisfied, the macro continues evaluating each condition within the subroutine. 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 are displayed.
The CheckGender subroutine works a little bit differently. It evaluates the value of the optGender option group. One of the first two lines of the subroutine executes, depending on whether the first or second option button is selected. The third line of the subroutine executes regardless of the Option Group value. This is because no ellipsis is entered in the Condition column of the macro action. If no ellipsis is found on any line of the subroutine, the macro action executes unconditionally. If an ellipsis were placed before the line, the macro action would execute only if the value of OptGender was 2.
You have learned quite a bit about macros but have not yet learned how to execute them. The process of executing a macro varies depending on what you are attempting to accomplish. A macro can be run from the Macro Design window, from the Macros tab, by being triggered from a Form or Report event, or by selecting a Menu or Toolbar option. The first three methods are covered here. Invoking a macro from a Menu or Toolbar option is covered in Chapter 13, "Let's Get More Intimate With Forms: Advanced Techniques."
A macro can be executed easily from the Macro Design window. It is easy to run a macro without subroutines. Click Run on the Macro Design toolbar. Each line of the macro is executed unless conditions have been placed on specific macro actions. After you click the Run button of mcrClients (pictured in Figure 11.13), the frmClients form is opened.
Figure 11.13. Running a macro from the Macro Design window.
From Macro Design view, you can run only the first subroutine within a macro. To run a macro containing subroutines, click Run on the Macro Design toolbar. The first subroutine within the macro executes. As soon as the second macro name is encountered, the macro execution terminates.
To run a macro from the Macros tab of the Database window, follow these two steps:
Chapter 10, "The Real Scoop on Objects, Properties, and Events," introduces the concept of executing code in response to an event. Here you learn how to associate a macro with a command button.
The form pictured in Figure 11.14 illustrates the process of associating a macro with the Click event of a command button on a form. Four steps are needed to associate a macro with a Form or Report event:
Figure 11.14. Associating a macro with a Form or Report event.
[ic:tryit]To practice implementing the techniques you have learned, build the macro pictured in Figure 11.12:
Macro Name |
Macro Condition |
Macro Action |
Argument |
Value |
CheckBirthDate |
DateDiff("yyyy", [Forms]![frmPersonalInfo]![txtBirth Date],Date()) Between 25 and 49 |
MsgBox |
Message |
You Are Over a Quarter Century Old |
Type |
Information | |||
... DateDiff("yyyy", [Forms]!]frmPersonalInfo]![txtBirthDate],Date()) Between 50 And 74 |
StopMacro MsgBox |
Message |
You Are Over a Half Century Old | |
Type |
Information | |||
... DateDiff ("yyyy",[Forms]![frmPersonalInfo]![txtBirthDate], Date()) Between 75 And 99 |
StopMacro MsgBox |
Message |
You Are Over Three Quarters of a Century Old | |
Type |
Warning | |||
... DateDiff ("yyyy", [Forms]![frmPersonalInfo]![txtBirth Date],Date ())>100 |
StopMacro MsgBox |
Message |
You Are Over aCentury Old!! | |
Type |
Warning | |||
... |
StopMacro | |||
CheckGender |
[Forms]![frmPersonalInfo]![optGender]=1 |
MsgBox |
Message |
You Are Male |
Type |
Information | |||
[Forms]![frmPersonalInfo]![optGender]=2 |
MsgBox |
Message |
You Are Female | |
Type |
Information | |||
MsgBox |
Message |
Thank You for the Information |
You have learned quite a bit about macros. You have learned how to create a macro, add macro actions and their associated arguments, create macro subroutines by adding macro names, and conditionally execute the actions within the macro by adding macro conditions. Once a macro has been created, you might want to modify it. To modify a macro, you must first enter Design view for the macro:
The design of the macro appears. You are now ready to insert new lines, delete existing lines, move the macro actions around, or copy macro actions to the macro you are modifying or to another macro.
To insert a macro action:
To insert multiple macro actions:
To delete a macro action:
To delete multiple macro actions:
Figure 11.15. Selecting and deleting macro actions.
You can move macro actions in a few ways, including dragging and dropping and cutting and pasting.
To move macro actions by dragging and dropping:
To move macro actions by cutting and pasting:
Macro actions can be copied within a macro or to another macro. To copy macro actions within a macro:
To copy macro actions to another macro:
Just as it is useful to document any program, it is useful to document what you are trying to accomplish within your macro. These comments can be used when you or others are attempting to modify your macro at a later time. They can also be used as documentation because they print when you print the macro.
To add a comment to a macro, click the Comment column of the macro and begin to type. Figure 11.16 shows a macro with comments. As you can see in Figure 11.17, these comments appear in the printed macro.
Figure 11.16. Adding comments to a macro.
Figure 11.17. Comments included in the printed macro.
Access does not provide very sophisticated tools for testing and debugging your macros. However, it does provide a method by which you can step through each line of a macro:
Figure 11.18. The Macro Single Step dialog allows you to view the macro name, condition, action name, and arguments for the current step of the macro.
[ic:tryit]It is easiest to learn about the process of stepping through a macro by experiencing it first-hand. To begin, open the mcrPersonalInfo macro created in the last example in Design view, and follow these steps:
As you can see, although Microsoft provides some tools to help you debug your macro, you'll probably agree that they are quite limited. This is one reason why most developers prefer to develop applications using VBA code.
As mentioned, certain tasks within Access can be performed only by using macros. These tasks include:
Although this chapter provides you with many other examples of macros, these are provided only so that you can obtain a cursory understanding of macros and how they work. Developers should only use macros to accomplish the four tasks listed here. As you'll see later in this section, VBA code is much more appropriate for most of the tasks that your application performs.
Although macros are not your only choice as a starting point for your applications in Access 95, the AutoExec macro is one of two choices for this task. For more about the AutoExec macro, see the section in this chapter titled "Creating an AutoExec Macro."
Macros must be used for certain tasks, but they are not the best tool for creating code that controls industrial-strength applications because they are quite limited in both function and capability. Access macros are limited in the following ways:
Now that you have discovered all the limitations of macros, you might be thinking about all the macros you have already written that you wish you had developed using VBA code. Or, after seeing how easy it is to accomplish certain tasks using macros, you might be disappointed to learn how limited macros are. Fortunately, Access 95 comes to the rescueit is easy to convert an Access macro to VBA code. Once the macro has been converted to VBA code, the code can be modified just like any VBA module. The process of converting an Access macro to VBA code consists of six steps:
Figure 11.19. The macro File|Save As/Export dialog allows you to save a macro as a Visual Basic module.
Figure 11.20. The Convert Macro dialog appears when you are saving a macro as a Visual Basic module. It allows you to specify whether error handling and comments will be added to the Visual Basic module.
Figure 11.21 shows a converted macro. The macro is converted into distinct subroutines, one for each macro name. The macro is complete with logic, comments, and error handling. All macro conditions are converted into If...Else...End If statements. All macro comments are converted into VBA comments. Basic error-handling routines are automatically added to the code.
Figure 11.21. A converted macro as a module.
Prior to Access 95, the only way to have something happen when a database was opened was to use an AutoExec macro. With Access 95, you can use either an AutoExec macro or Startup options to determine what occurs when a database is opened. Nevertheless, using an AutoExec macro to launch the processing of your application is certainly a viable option.
The process of creating an AutoExec macro is quite simple. An AutoExec macro is just a normal macro saved with the name AutoExec. An AutoExec macro usually performs tasks such as hiding or minimizing the Database window and opening a Startup form or switchboard. The macro pictured in Figure 11.22 hides the Database window, displays a welcome message, and then opens the frmClients form.
Figure 11.22. Example of an AutoExec macro.
An AutoKeys macro allows you to redefine keystrokes within your database. You can map selected keystrokes to a single command or to a series of commands. Follow six steps to build an AutoKeys macro:
Figure 11.23. List of valid Key Names for AutoKey macro.
Most macro commands can be performed in VBA code using the DoCmd object. The macro action becomes a method of the DoCmd object. The arguments associated with each macro action become the arguments of the method. For example, the following method of the DoCmd object is used to open a form:
DoCmd.OpenForm "frmClients", acNormal, "", "[tblClients]![IntroDate]>Date() _ -30", acEdit, acNormal
The OpenForm method of the DoCmd object opens that form that appears as the first argument to the method. The second argument indicates the view in which the form will be opened. The third and fourth arguments are used to specify a Filter and Where Condition, respectively. The fifth argument of the OpenForm method is used to specify the Data Mode for the form (Add, Edit, or Read Only). The sixth argument is used to indicate the Window Mode (Normal, Hidden, Minimized, or Dialog).
Notice the intrinsic constants that are used for the OpenForm arguments. These intrinsic constants greatly aid in the readability of the code. They can be found in Help for each DoCmd method.
In Chapter 13, "Getting More Intimate with Forms: Advanced Techniques," you learn how to add a switchboard to your application. In Chapter 34, "Distributing Your Application," you learn how to use Startup options to designate a Startup form for your application. For now, we'll build an AutoExec macro that acts as the launching point for our application. The macro starts the application by hiding the Database window, displaying a message to the user and opening the frmClients form.
Build the macro pictured in Figure 11.22. Start by opening a new macro in Design view. Set the first action of the macro to DoMenuItem. Set the Menu Bar to Database, the Menu Name to Window, and the Command to Hide. The DoMenuItem macro action and selected arguments serve to hide the Database window. Set the second action of the macro to MsgBox. Set the Message to "Welcome to the Client Billing Application." Set Beep to No, the Type to Information, and the Title to "Welcome." The final action of the macro opens the frmClients form. Set the action to OpenForm. Set the FormName to frmClients. Leave the rest of the arguments at their default values.
Close and reopen the database. The AutoExec macro should automatically execute when the database is opened. Close the database and open it again, holding down the Shift key to prevent the macro from executing.
Many end-users attempt to develop entire applications using macros. Although this is possible, it is not appropriate. Macros do not provide the developer with adequate control, error handling, debugging capabilities, and many other features that are requisite to successful application development.
Although macros should not be used to develop your application, certain tasks can be accomplished only by macros. These include the creation of menus, the reassignment of key combinations, the assignment of a custom action to a toolbar, and the automation of Access by other applications using DDE. Furthermore, an AutoExec macro is one of two ways that you can determine what happens when an application loads. Unless you are using a macro to perform one of these tasks, you should always automate your application using VBA code.