Creating Macro Conditions

3 4

If you need to run a macro action only if a condition is met-such as a specific form being open-you can create a condition expression in the macro's Condition column. A condition expression must evaluate to True or False; if it's True, the macro action runs; if it's False, the action is skipped. If you need to run one macro action if a condition is True and another if it's False, you need to write two macro rows with different conditions (one the negation of the other).

To write a macro with conditions that will open one form on a weekday and another form on a weekend day, follow these steps. (The two forms referenced in the macro are found in the sample database Test Access 2002.)

  1. Click Macros in the Database window's Objects bar, and click New to open a new macro in the Macro Designer.
  2. If the Condition column isn't visible, click the Conditions button on the toolbar to make it visible.
  3. In the Condition column of the first macro row, enter the following expression:

     Weekday(Date())=0 Or Weekday(Date())=7 

    (This expression selects Saturday and Sunday only.)

  4. Select OpenForm as the macro action, and frmOrdersWeekends as the form name. Leave the other arguments for this action at their default settings.
  5. In the Condition column of the second macro row, enter the following expression:

     Weekday(Date())<>0 And Weekday(Date())<>7 

    (This expression selects weekdays only.)

  6. Select OpenForm as the macro action, and frmOrdersWeekdays as the form name, and leave the other arguments at their default settings.
  7. Save the macro as mcrOpenOrders.

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:

  1. Open the database that needs the IsLoaded function.
  2. Open the Northwind.mdb sample database in another Access window. (It's located in the \Microsoft Office\Office10\Samples folder.)
  3. Click the Modules object in the Northwind Database window.
  4. Drag the Utility Functions module from Northwind to the other database to create a copy of the module in your database.

The Utility Functions module now contains the IsLoaded function, which will be available for use in VBA code and macros in your database.



Microsoft Access Version 2002 Inside Out
Microsoft Access Version 2002 Inside Out (Inside Out (Microsoft))
ISBN: 0735612838
EAN: 2147483647
Year: 2005
Pages: 172
Authors: Helen Feddema

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net