3. Restricting the Format of Data
Two properties control the format of information in database
tables: the Format property and the Input Mask property. Both
properties affect how information is displayed after it has been
entered in a table, but the Input Mask property also serves an
important function during data entry. As its name implies, an input
mask allows anyone entering new records to see at a glance the format
required and how long the entry should be.
You can use the Input Mask property to control how data is
entered in Text, Number, Date/Time, and Currency fields. For Text and
Date/Time fields, an Input Mask wizard is available to help you apply
several common, predefined masks. For Number and Currency fields, you
have to know how to create a mask from scratch.
The Input Mask property has three sections, separated by
semicolons. For example, the following mask is for a telephone
number:
!\(000”) “000\-0000;1;#
The first section contains characters that are used as
placeholders for the information to be typed, as well as characters
such as parentheses and hyphens. Together, all these characters
control the appearance of the entry. The following table explains the
purpose of the most common input mask characters.
Any characters not included in this list are displayed the way
you type them and are known as literal
characters. If you want to use one of the special
characters in this list as a literal character, precede it with the (backslash) character.
The second and third sections of the input mask are optional.
Including a 1 in the second section (or leaving it blank) tells Access
to store only the characters entered; including a 0 tells it to store
both the characters entered and the literal characters. Entering a
character in the third section causes Access to display that character
as the placeholder for each of the characters to be typed; leaving it
blank causes Access to display an underscore as the
placeholder.
The input mask !\(000”) “000\-0000;1;#
creates this display in a field in either a table or a form:
(###) ###-####
In this example, the 0s in the first part of the mask
restrict the entry to 10 digits—no more and no less. The database user
does not enter the literal characters—the parentheses, space, and
hyphen. The 1 in the second part tells Access not to store the literal
characters; it should store only the 10 digits. The # sign in the
third part tells Access to use that character as the placeholder for
the required 10 digits.
Tip
An input mask can contain text as well as placeholders for the
data to be entered. For example, if you type The number
is in front of the telephone number input mask, the
default entry for the field is The number is (###)
###-####. The numbers you type still replace the #
placeholders, not the text. The Field Size property does not apply
to the literal characters in the mask, so if this property is set to
15, the entry will not be truncated even though the number of
displayed characters (including spaces) is 28.
In this exercise, you’ll use the Input Mask wizard to apply a
predefined input mask for a telephone number to a Text field. Then
you’ll modify the input mask to display the telephone number in a
slightly different way. Finally, in another field you’ll create a
custom mask that displays a text entry with an initial capital letter,
no matter how it is actually typed.
Set Up
You need the GardenCompany06 database you worked with in the
preceding exercise to complete this exercise. Open the
GardenCompany06 database, and display the FieldTest table in Design
view. Then follow the steps.
-
Click the row selector for the fText field, and on the Design contextual tab, in the Tools group, click the Insert Rows button.
-
Add a new field named fPhone that is assigned the Text data type. Then save the
table.
-
With the fPhone field
still selected, in the Field
Properties area, click anywhere in the Input Mask
property.
-
Click the Ellipsis button
to the right of the property, and save the table when
prompted.
The Input Mask wizard displays its first page.
-
With Phone Number
selected in the Input Mask
list, click Next.
The second page of the wizard is displayed.
The barely visible exclamation point at the left end
of the mask causes Access to fill the mask from left to right with
whatever is typed. The parentheses and hyphen are characters that
Access will insert in the specified places. The nines represent
optional digits, and the zeros represent required digits. This
allows you to enter a telephone number either with or without an
area code.
Tip
Because Access fills the mask from left to right, you
would have to press the Right Arrow key to move the insertion
point past the first three placeholders to enter a telephone
number without an area code.
-
Change 999 to 000 to require an area code. Then display
the Placeholder character list,
click #, and click Next.
On the third page of the wizard, you specify whether you
want to store the symbols with the data. (The symbols take up
space, meaning that your database will be larger.)
-
Accept the default selection—to store the data without the
symbols—by clicking Finish.
(Clicking Next simply displays a page announcing that the
wizard has all the information it needs to create the mask.)
Access closes the wizard.
-
Press Enter to accept the mask.
Access changes the format of the mask to !\(000”)
“000\-0000;;#. Notice the two semicolons that separate
the mask into its three sections. Because you told Access to store
data without the symbols, nothing is displayed in the second
section of the mask. Notice also that Access added double
quotation marks to ensure that the closing parenthesis and
following space are treated as literal characters.
-
Save the table, and then switch to Datasheet view.
-
Click the ID field in the
first record, and press the Tab key to move to the fPhone field. Then type a series of
numbers to see how the mask works.
Access formats the first 10 numbers you enter as a telephone
number. If you type more than 10, Access ignores the additional
digits. If you type fewer than 10 and then press Tab or Enter,
Access warns that your entry doesn’t match the input mask.
Now let’s see how an input mask can be used to control the
display of data entries.
-
Switch to Design view, and click anywhere in the fText field.
-
In the Field
Properties area, click the Input Mask property, type the following,
and then press Enter:
>L<????????????????
(16 question marks)
Tip
When you press Enter, the Property Update Options button
appears. Clicking this button displays a list of options. In
this case, the only options are to apply the input mask
everywhere fText is used (which is called propagating
the property), and to display Access Help to find out
more about this task. This button disappears when you edit any
other property or change to a different field, so you can ignore
it.
The greater than symbol (>) forces
all following text to be uppercase. The L
requires a letter. The less than symbol (<) forces all
following text to be lowercase. Each question mark allows any
letter or no letter. The total number of characters (17) is one
fewer than the maximum number allowed in the field by this field’s
Field Size property.
-
Save your changes to the table, and switch back to Datasheet
view.
-
Delete the current entry in the fText field, type smith, and press Tab.
-
Replace the entry with SMITH, and then McDonald.
Regardless of how you type the name, only its first letter
is capitalized.
Tip
You can create custom input masks and have the Input Mask
wizard store them for future use. On the wizard’s first page,
click Edit List, and in the record navigation bar of the
Customize Input Mask Wizard dialog box, click the New Record
button. Then enter the information for the custom mask, and
click Close.
Clean Up
Close the FieldTest table. Retain the GardenCompany06 database
for use in later exercises.