As youâve already learned, databases prize consistency. If you have a field named Height, you better be sure every value in that field uses the same type of measurements; otherwise, your data isnât worth its weight in sock lint. Similarly, if you have a PhoneNumber field, you better make sure every phone number has the same format. If some phone numbers are written with dashes, spaces, and parentheses (like (844) 547-1123), while others are a bit different (say, 847-547-1123), and a few leave out the area code information altogether (547-1123), then youâve got a small problem on your hands. Because of the lack of consistency, youâll have a hard time working with this information (say, searching for a specific phone number or sorting the phone numbers into different categories based on area code).
To help you manage values that have a fixed patternâlike phone numbersâyou can use an input mask. Essentially, an input mask (or just mask for short) gives you a way to tell Access what pattern your data should use. Based on this pattern, Access changes the way values are entered and edited to make them easier to understand and less error-prone. Figure 4-8 shows how a mask lets Access format a series of characters as theyâre being typed into a field.
You can add a mask to any field that uses the Text data type. Masks give you several advantages over ordinary text:
Masks guide data entry. When empty, a masked edit control shows the placeholders where values need to go. A phone number mask shows the text â(_ _ _) _ _ _-_ _ _ _â when itâs empty, clearly indicating what type of information it needs.
Masks make data easier to understand. You can read many values more easily when theyâre presented a certain way. Most people can pick out the numbers in this formatted Social Security number (012-86-7180) faster than in this unformatted one (012867180).
Figure 4-8. Top: Hereâs a PhoneNumber field with a mask thatâs ready to go. So far, the person entering the record hasnât typed anything. The PhoneNumber field automatically starts out with this placeholder text. Bottom: The mask formats the numbers as you type. If you type 1234567890 into this phone number mask, then you see the text (123) 456-7890. Behind the scenes, the databases stores 1234567890, but the information is presented in the datasheet using a nicely formatted package. That package is the mask.
Masks prevent errors. Masks reject characters that donât fit the mold. If youâre using the telephone mask, you canât use letters.
Masks prevent confusion. With many types of data, you have several ways to present the same information. You can enter phone numbers both with and without area codes. By presenting the mask with the area code placeholder, youâre saying that this information is required (and where it goes). Itâs also obvious that you donât need to type in parentheses or a dash to separate numbers, because those details are already there. Youâll see the same benefit if you use masks with dates, which can be entered in all sorts of different combinations (Year/Month/Day, Month-Day-Year, and so on).
Masks are best suited for when youâre storing numeric information in a text field. This scenario occurs with all sorts of data, including credit card numbers, postal codes, and phone numbers. These types of information shouldnât be stored in number fields, because they arenât meant to be interpreted as a single number. Instead, theyâre meant to be understood as a series of digits. (If you do make the mistake of storing a phone number in a number field, youâll find out that people can type in perfectly nonsensical phone numbers like 0 and â14 because these are valid numbers, even if they arenât valid phone numbers. But an input mask on a text field catches these errors easily.)
Masks canât help you with more sophisticated challenges, like data values that have varying lengths or subtle patterns. For instance, a mask doesnât help you spot an incorrect email address.
The easiest way to get started with masks is to use one of the many attractive options that Access has ready for you. This method is great, because it means you donât need to learn the arcane art of mask creation.
Hereâs what you need to do to pick out a prebuilt mask:
In Design view, select the text field where you want to apply the mask.
For this test, try a PhoneNumber field.
Look for the Input Mask field property. Click inside the field.
When you do, a small ellipsis (â¦) button appears at the right edge, as shown in Figure 4-9.
The Input Mask wizard starts (see Figure 4-10).
Choose the mask you want from the list of options.
In this case, choose the first item in the list (Phone Number).
Tip
Donât see what you want? Youâll need to create your own, using the tips on Creating Your Own Mask. If you see one thatâs close but not perfect, select it. You can tweak the mask in the wizardâs second step.
Click Next.
The wizardâs second step appears (see Figure 4-11).
If you want, you can change the mask or the placeholder character.
To change the mask, youâll need to learn what every mask character means. Creating Your Own Mask explains it all.
You use the placeholder to show the empty slots where you enter information. The standard choice is the underscore. Optionally, you can use a space, dash, asterisk, or any other character by typing it in the âPlaceholder characterâ box.
Figure 4-10. The Input Mask wizard starts with a short list of commonly used masks. Next to every mask, Access shows you what a sample formatted value looks like. Once you select a mask, you can try using it in the Try It text box. The Try It text box gives you the same behavior that your field will have once you apply the mask.
If youâre adding a mask to a text field, then the wizardâs final step appears (see Figure 4-12).
If youâre adding a mask to a date field, then Access doesnât need to ask you how to store the informationâit already knows. In this case, you can jump to step 9 and click Finish.
Choose how you want to store the value in this field.
The standard choice is to store just the characters youâve typed into the field. If you use this option, the placeholders arenât included. For example, the phone number (416) 123-4567 is stored as 4161234567. This option saves a little space, and it also lets you change the mask later on to present the information in a slightly different way.
You could also store the mask complete with all the extra characters. Then a phone number is stored complete with hyphens, dashes, and spaces, like (416) 123-4567. This approach isnât nearly as flexible because you canât change the mask later.
Click Finish.
The final mask appears in the Input Mask field property.
Before going any further, you may want to make sure that the length youâve reserved for your field matches the mask. In the phone number example, you need a Field Size of 10 if youâve chosen to store unformatted values (because there are 10 digits), or a Field Size of 14 for the whole shebang, complete with placeholders (one dash, one space, and two parentheses).
Switch back to the Datasheet view, and click Yes when Access asks you to save changes.
Note
Access uses the input mask information to control how you enter information in the datasheet. However, itâs possible to circumvent the mask by entering the information in other ways. You could, for instance, create a form (as described in Part Four), and switch off the mask. A mask isnât an absolute guarantee against invalid dataâif you want such a guarantee, then you need a validation rule instead.
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 is 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 The New Features in Access 20100), 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 is 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-0000. 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 is 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. Placeholder 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 is filled from left to right when characters are typed in. This is the default, so this character isnât 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 example, the # character has a special meaning in masks. Thus, if you want to actually include a # in your mask, you need to use \#. Sometimes, this character is 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-0000. A phone number that requires the area code digits. This mask is 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 Canadian postal code, which is a pattern of six characters that alternate between characters and digits, like M6S 3H2.
99: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 is 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 is a number that tells Access whether 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-0000;1;#
Here, the second section is 1, and the third section is #. This mask is for phone numbers. It uses the number sign for a placeholder, and it includes the following literals: two parentheses, a space, and a dash. These literals arenât stored in the field.
Figure 4-13. To add your own mask, use the âNew blankâ button (circled). Or you can use this window to change a mask. For example, the prebuilt telephone mask doesnât require an area code. If thatâs a liberty youâre not willing to take, then replace it with the more restrictive version (000) 000-0000.
Get Access 2010: The Missing Manual now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.