The Database Journal
03/10/2006
Adjusting Autonumber Increments

03/10/2006
Moving Access Data to Microsoft Excel

03/10/2006
Exporting Access Data with TransferSpreadsheet

03/10/2006
Export Access Data with VBA

03/13/2006
Data Layout in Microsoft Excel

03/17/2006
Excel Text to Number Conversion

03/21/2006
Transposing Excel Ranges

03/25/2006
Access VBA Timer Event

03/29/2006
What is MySQL?

04/02/2006
Microsoft Access Parameter Query Tutorial Video

04/06/2006
How does PHP Work?

04/10/2006
Microsoft Access VBA Tools - References

Click here to view all articles.

Database Design
Website Design
Search Engine Marketing
Home
Contact Us
About Us
Database Journal
Newsletters
Database Newsletter

Microsoft Access Macro Conditions

Microsoft Access macros aren't typically used by people who are slightly more experienced programming VBA, but that doesn't mean that they're to be avoided at all costs. While many people don't like using macros, you can't deny how easy it is to create one for common tasks on a form.

In this article, we're going to create a conditional macro - that is, a macro that only launches when certain criteria are met. There's no VBA involved, it's all strictly through the Access interface.

First, click on Tools, Options in Microsoft Access. On the View tab, you'll notice a checkbox called "Conditions column". Check it, and then hit OK. Congratulations, you've just enabled the use of conditional macros in Microsoft Access!

Figure 1

Now it's time to get to know the form we'll be working in. In this case, I'm using a form with two objects - a text box called "txt_Number" and a button called "cmd_Macro".

Figure 2

The idea is, we're going to place a number in the textbox, then press the button, and a msgbox macro is going to tell us if that number is greater than 20. If it's not, nothing happens, if it is, the message box appears.

"Cmd_Macro" will launch a macro called, "mcr_Over20". The macro is a simple msgbox macro which will pop open a message box with the words, "Yes, it's over 20!" if in fact it is. The trick here, is creating the right condition for the macro. Notice the statement in the Condition column.

Figure 3

This statement is saying in plain English, "Look at a Form called, 'frm_blah,' and then look at a field on that form called, 'txt_Number.' If the value of that field is greater than 20, run the macro."

So when we go back to our form, type in the number 21, and then press the button, our result is a message box telling us that yes, in fact we have typed in a number over 20!

Figure 4

This is a great technique to use for quick data validation on Microsoft Access forms. Using macros with conditions can help you guide a user when they're filling out form information. If a specific value on your form must be filled in, or if it must be between certain values, you can easily alert them while they're inputting the data - a much better time than after they've already written up an entire record. If you want to use this as data validation, instead of using a button, you can launch the macro on the AfterUpdate event of the field they're typing in. Then, as soon as they leave that field, a validation message can appear!


©2010 Blue Moose Technology, LLC

David Badurina, President of Blue Moose Technology, LLC, is a relational database design expert. David's unique ability to easily explain virtually any technical concept has allowed him to work with companies such as AMD, Motorola, the American Heart Association, and countless small businesses. Learn more about database design right now at http://www.bluemoosetech.com.

Article Publishing and Re-Print Guidelines

You can easily link to this article!

If you'd like to link to this article from your own website, copy and paste the following HTML code onto your page:

On your page, the code will appear like this:

Microsoft Access Macro Conditions