Using Masks to Block Bad Data - Using a Ready-Made Mask (Page 3 of 4 ) 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’s 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 left edge, as shown in Figure 4-9. Figure 4-9. The ellipsis (...) button (circled) is just the way Access tells you that you don’t need to fill in this value by hand. Instead, you can click the ellipsis and pop up a wizard (like the Input Mask wizard) or some sort of helpful dialog box.
Click the ellipsis button.
The Input Mask wizard starts (see Figure 4-10). 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. Choose the mask you want from the list of options.
In this case, choose the first item in the list (Phone Number).
Note: Don’t see what you want? You’ll need to create your own, using the tips on page 128. 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. Page 129 explains it all. Input Masks
Figure 4-11. The phone number mask is !(999) 000-000. Each 9 represents an optional number from 0 to 9. Each 0 represents a required number from 0 to 9. So according to this mask, (123) 456-7890 is a valid phone number, as is 123-4567, but (123) 456 isn’t.
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. Click Next.
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. Figure 4-12. The final step lets you choose how the data in your field is chosen—with or without the mask symbols.
Choose how you want to store the value in this field.
The standard choice is to store just the characters you’ve typed in (in other words, everything you type 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’s 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.
Your input mask is now in place.
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’s not an absolute guarantee against invalid data—if you want such a guarantee, then you need a validation rule instead.
Next: Creating Your Own Mask >>
More MS SQL Server Articles More By O'Reilly Media | 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.
|
| |