Using Masks to Block Bad Data - Creating Your Own Mask
(Page 4 of 4 )
The Input Mask wizard provides a fairly limited set of choices. If you want to use a mask with your own type of information (like a special customer code that your business uses), then you’ll have to create your own mask.
Creating a mask’s fairly easy, but it can take a bit of fiddling before you nail down exactly the result you want. You have two basic options:
- Type or edit the mask directly in the Input Mask field property.
- Launch the Input Mask wizard, choose a mask to use as a starting point (as described on page 125), and then tweak it in step 2. This approach has the advantage that you can test your mask in the Try It box before you save it as part of your table.
Every mask’s built out of three types of characters:
- Placeholders designate where you type in a character.
- Special characters give additional instructions that tell Access how to treat a part of the mask.
- Literals are all other characters, which are really just decoration to help make the value easier to interpret.
In the previous example, the phone number mask was !(999) 000-000. The characters 9 and 0 are placeholders—they represent where you type in the digits of the phone number. The parentheses, space, and dash are just formatting niceties—they’re the literals. And the exclamation mark’s the only special character. It tells Access that characters should be entered into the mask from left to right, which is the standard option and the only one that really makes sense for a phone number.
To help you sort all this out, refer to the following tables. Table 4-1 shows all the placeholders you can use in an input mask. Table 4-2 shows other special characters. Everything else is automatically a literal character.
Table 4-1. Special Characters for an Input Mask
Character | Description |
0 | A required digit (0 through 9). |
9 | An optional digit (0 through 9). |
# | An optional digit, a plus sign (+), or a minus sign (–) |
L | A required letter. |
? | An optional letter. |
A | A required letter or digit. |
a | An optional letter or digit. |
& | A required character of any type (including letters, numbers, punctuation, and so on). |
C | An optional character of any type (including letters, numbers, punctuation, and so on). |
Table 4-2. Special Characters for an Input Mask
Character | Description |
! | Indicates that the mask’s filled from left to right when characters are typed in. This is the default, so this character’s not required (although the prebuilt masks include it). |
< | Converts all characters that follow to lowercase. |
> | Converts all characters that follow to uppercase. |
\ | Indicates that the following character should be treated as a literal. For exam-ple, the # character has a special meaning in masks. Thus, if you want to actu-ally include a # in your mask, you need to use \#. Sometimes, this character’s used before a placeholder even when it’s not needed. You may see a phone mask that has the character sequence \- instead of just -. Both are equivalent. |
Password | Creates a password entry box. Any character you type in the box is stored as the character but displayed as an asterisk (*). When using this option, you can’t include anything else in your mask. |
Here are a few sample masks to get you started:
- (000) 000-000. A phone number that requires the area code digits. This mask’s different from the phone number mask that the Input Mask wizard uses. That mask replaces the first three 0 characters with 9, making the area code optional.
- 00000-9999. A U.S. zip code, which consists of five required digits followed by a hyphen and (optionally) four more digits.
- L0L 0L0. A British or Canadian postal code, which is a pattern of six characters that alternate between characters and digits, like M6S 3H2.
- 99:00:00 >LL. A mask for entering time information into a Date/Time field. It’s made up of two digits for the hour and two digits for the minute. The last two characters are always displayed in uppercase (thanks to the > character), and are meant to be AM or PM. (Technically, this mask doesn’t prevent the user from flouting the system and typing in two different characters. However, if you enter a time like 12:30 GM, Access complains that it can’t convert your entry into the Date/Time data type, as required for the field.)
- 099.099.099.099. An IP (Internet Protocol) address, which identifies a computer on a network. An IP address is written as four values separated by periods. Each value must have at least one digit, and can have up to three. This pattern’s represented in the mask by 099 (one required digit, followed by two optional digits).
- Password. A mask that allows ordinary, unlimited text, with one difference. All characters are displayed as asterisks (*), to hide them from prying eyes.
Masks can also have two optional bits of information at the end, separated by semicolons (;).
The second section’s a number that tells Access whether or not it should store the literal characters for the mask in the record. (This is the last question that the Input Mask wizard asks.) If you leave this piece out or use the number 1, then Access stores only the characters that someone types in. If you use the number 0, then Access stores the full text with the literals.
The third section supplies the placeholder character. If you leave this section out, then Access uses the familiar underscore.
Here’s a mask that uses these two extra bits of information:
(000) 000-000;1;#
Here, the second section’s 1, and the third section’s #. This mask’s for phone numbers, and it stores literals (in this case, two parentheses, a space, and a dash) and uses the number sign for a placeholder instead of the underscore.
Please check back tomorrow for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of Access 2007: The Missing Manual, written by Matthew MacDonald (O'Reilly, 2006; ISBN: 0596527608). Check it out today at your favorite bookstore. Buy this book now.
|
|