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!
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".
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.
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!
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
Article Publishing and Re-Print Guidelines
- The article text, resource box, URLs copyright information and these guidelines must be left intact and unchanged.
- When re-published online, all links must be live hyperlinks.
- These articles may not be distributed in any manner that does not comply with federal communication guidelines. Be Good!
- These articles must not be password protected or limited to membership or paid-only viewing. Keep 'em free, folks!
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:


