3 4
If you need to run a macro action only if a condition is met-such as a specific form being
To write a macro with conditions that will open one form on a weekday and another form on a
Weekday(Date())=0 Or Weekday(Date())=7
(This expression selects Saturday and Sunday only.)
Weekday(Date())<>0 And Weekday(Date())<>7
(This expression selects weekdays only.)
To test the macro, run it from the Macro Designer by clicking the Run button on the toolbar. If you run the macro on a weekday, frmOrdersWeekdays opens; if you run the macro on a weekend day, frmOrdersWeekends opens.
Troubleshooting - I imported a macro into a database, and when I run it, I get the message The expression you entered has a function name that databasename can't find
A great many macros use a function named IsLoaded to determine whether a form is loaded before running a database action. If you don't have this function in a module in your database, you'll get this error message when you run a macro that uses IsLoaded .
The IsLoaded function is located in the Utility Functions module in the sample Northwind database. To import it into your current database, follow these steps:
The Utility Functions module now contains the IsLoaded function, which will be available for use in VBA code and macros in your database.
3 4
As mentioned earlier in the chapter, from the earliest versions of Access, there have been two macro
Additionally, there's another type of special-purpose macro that has no reserved
The AutoKeys macro group is just as useful in Access 2002 as it was in Access 1.0. This macro group specifies actions to run from custom hot keys in an Access database. These hot keys supplement the built-in hot keys with your own hot keys to run actions you need frequently.
Each macro in the AutoKeys macro group corresponds to a letter of the alphabet, preceded by the caret symbol (^), which represents the Ctrl key. For example, after you create an ^A macro in the AutoKeys macro group, you can run the macro by pressing the Ctrl+A hot key. You can use all the
Table 14-1 lists the macros in the AutoKeys macro group that I use in most of my Access databases. Of course, you can create your own hot keys to meet your needs.
Table 14-1. Sample AutoKeys macro group
| Macro name | Action | Arguments | Description |
|
^B |
OpenForm |
Form Name: fmnuMain |
Opens the main menu form, named fmnuMain. (You can replace
fmnuMain
with the name of your main menu.) This macro is handy for quickly reopening the main menu when it has been closed
|
|
^D |
SendKeys |
Keystrokes: %fa{home} +^{right 2}-{end} |
Creates a duplicate of a database object. The new object's name is the same as the original object's but with a dash in front, and you can also append a numeral at the end. Used to create a backup copy of an object. (See Figure 14-5 for an example of this hot key in action.) |
|
^E |
Echo |
Echo On: Yes |
Turns Echo on. Useful when VBA code has
|
|
^O |
SendKeys |
Keystrokes: {f2}{home}-O- {enter}{down} |
Renames an object in focus by adding an
-O-
in front of its name. This macro is useful for
|
|
^Q |
SendKeys |
Keystrokes: {f2}{home}-?- {enter}{down} |
Renames an object by adding a -?- in front of its name. This prefix is used to identify an object that might not be needed anymore but that you're not yet ready to delete. |
|
^R |
SendKeys |
Keystrokes: {f2}{home}- {enter}{down} |
Renames an object by adding a dash in front of its name. This prefix indicates database objects that aren't needed anymore and that can be deleted when you
|
Several of these macros are used to rename or copy database objects, which is a way of keeping older versions of objects available by giving them
Many of my AutoKeys macros use the
SendKeys
action to send a series of keystrokes to Access. These macros
Figure 14-5. Use the ^D macro to make a backup copy of a database object.
note
Although the Startup dialog box (opened by choosing Tools, Startup) lets you select a form to
For example, if you have a function named OpenWord that creates a Word object to work with when exporting Access data to Word documents, you could run the function from a RunCode macro action. The same is true for a function (say, SetErrorTrappingOn ) that turns on global error trapping.
note
If you need to periodically import data from a text file,
tip
Figure 14-6 shows the text file in Notepad.
Figure 14-6. A text file to be imported shows text in all capital letters when opened in Notepad.
To make this data useful in an Access database, some of the name and address data must be converted to proper case (first letter capitalized), and the prospect name must be split into FirstName, MiddleName, and LastName fields.
note
To create the macro to automate importing data from this text file, follow these steps:
Figure 14-7. The Expression Builder helps you select a function to run from the RunCode macro action.
To test the macro, run it from the Macro Designer by clicking the Run button on the toolbar. You'll see a series of confirmation messages as the macro runs the queries and code. The final table of cleaned-up prospect data is shown in Figure 14-8.
Figure 14-8. After processing by an append query, the imported data is in proper case, and the name data is split into FirstName, MiddleName and LastName fields.
In future, when you need to import and process a new prospects file, you can simply double-click this macro in the Database window to run it.
tip