Preventing Form Errors by Validating Data
If, as the cooks say, a recipe is only as good as its
ingredients
, a database is only as good as its data. Viewing, summarizing, and analyzing the data are meaningless if the table you're working with contains erroneous or improper data. For basic data errors (for example, entering the wrong date or transposing a number's digits), there's not a lot you can do other than exhorting yourself or the people who use your forms to enter data
carefully
. Fortunately, you have a bit more control when it comes to preventing improper data entry. By "improper," I mean data that
falls
into either of the following categories:
-
Data that is the wrong type. For example, entering a text string in a
cell
that requires a number.
-
Data that falls outside of an
allowable
range. For example, entering 200 in a cell that requires a number between 1 and 100.
The
next
few sections show you several techniques that can help you reduce these types of errors.
Helping Users with Text Prompts
You can prevent improper entries to a certain extent by adding text that provides details on what is allowable inside a particular cell. You have two choices:
-
Add status bar text
This is a string that appears in the Access status bar when users enter the field. You specify this text by opening the field's property sheet, displaying the Other tab, and then entering the string in the Status Bar Text property.
-
Add a label
Place a Label control near the field and use it to enter text that describes the field's data requirements or shortcut keys. For example, if the field requires a date, the label might say
Press Ctrl+; to enter today's date
.
For instance, Figure 4.25 shows a form used as a Mortgage Calculator. Notice the labels added beside the Interest Rate and Term text boxes that specify to the users that they must enter the interest rate per annum and the
term
in
years
. Note, too, the status bar text that appears when the users enter the Interest Rate field.
Preventing Errors with Data Validation Expressions
The problem with text prompts is they require other people to both read
and
act on the text. The better solution for preventing
data-entry
errors is the Access data validation feature. With data validation, you create rules that specify exactly what kind of data can be entered and in what range that data can fall. You can also specify pop-up input messages that appear when a cell is selected, as well as error messages that appear when data is entered improperly.
Follow these steps to define the settings for a data validation rule:
|
1.
|
Display the property sheet of the field to which you want to apply the data validation rule.
|
|
2.
|
Display the Data tab.
|
|
3.
|
Click inside the Validation Rule property.
|
|
4.
|
Enter a formula that specifies the validation criteria. You can either enter the formula directly into the property box or you can click the ellipsis (...) button and enter the formula using the Expression Builder.
|
|
5.
|
If you want a dialog box to appear when the users enter invalid data, click inside the Validation Text property and then specify the message that appears.
|
|
6.
|
Close the property sheet to apply the data validation rule.
|
For example, suppose you want the users to enter an interest rate. This should be a positive quantity, of course, but it should also be less than 1. (That is, you want to users to enter 6% as 0.06 instead of 6.) Figure 4.26 shows the property sheet for a field named InterestRate that meets these criteria by defining the following expression in the Validation Rule property:
>0 And <1
Figure 4.26 also shows a string in the Validation Text property. If the users enter invalid data (that is, any value for which the Validation Rule expression returns False), the Validation Text appears in a dialog box, as shown in Figure 4.27.
Using Input Masks for Consistent and Accurate Data Entry
One of the major
headaches
that database administrators have to deal with is data entered in an inconsistent way. For example, consider the following phone
numbers
:
(123)555-6789
(123) 555-6789
(123)5556789
123555-6789
1235556789
These kinds of inconsistencies might appear trivial, but they can cause all kinds of problems, from other users misreading the data, to improper sorting, to difficulties analyzing or querying the data. And it isn't just phone numbers that cause problems. You also see them with social security numbers, ZIP Codes, dates, times, account numbers, and more.
One way to avoid such inconsistencies is to add a label or status bar message that specifies the correct format to use. As with data validation, however, these prompts are not
guaranteed
to work every time (or even most of the time).
A better solution is to apply an
input mask
to the field. An input mask is a kind of template that shows users how to enter the data and
prevents
them from entering incorrect
characters
(such as a letter where a number is required). For example, here's an input mask for a phone number:
(___)___-____
Each
underscore
(_) acts as a placeholder for (in this case) a digit, and the parentheses and dash appear automatically as the
user
enters the number.
Using the Input Mask Wizard
The
easiest
way to create an input mask is to use the Input Mask Wizard. Here are the steps to follow:
|
1.
|
Display the property sheet of the field to which you want to apply the input data.
|
|
2.
|
Display the Data tab.
|
|
3.
|
Click inside the Input Mask property.
|
|
4.
|
Click the ellipsis (...) button to start the Input Mask Wizard, shown in Figure 4.28.
|
|
5.
|
In the Input Mask list, click the input mask you want (or one that's close to what you want) and then click Next.
|
|
6.
|
Use the Input Mask box to make changes to the mask (see "Creating a Custom Input Mask Expression" in the next section, for the specifics of which symbols to use), use the Placeholder Character list to choose the character you want to appear in the input mask as a placeholder, and then click Next.
|
|
7.
|
Click the option that matches how you want the field data stored in the table (click Next after you've made your choice):
-
With the Symbols in the MaskClick this option if you want the extra symbols (such as the parentheses and dash in a phone number mask) stored along with the data.
-
Without the Symbols in the MaskClick this option to store only the data.
|
|
8.
|
Click Finish.
|
Creating a Custom Input Mask Expression
If your data doesn't fit any of the predefined input masks, you need to create a custom mask that
suits
your needs. You do this by creating an expression that consists of three kinds of characters:
-
Data placeholders
These characters are
replaced
by the actual data typed by the users. The different placeholders specify the type of character the users must enter (such as a digit or letter) and whether the character is optional or required.
-
Modifiers
These characters aren't displayed in the mask; instead, they're used to modify the mask in some way (such as converting all the entered characters to lowercase).
-
Literals
These are extra characters that appear in the mask the same as you enter them in the expression. For example, you might use parentheses as literals to surround the area code portion of a phone number.
Table 4.3 lists the data placeholders you can use to build your input mask expressions.
Table 4.3. Data Placeholders to Use for Custom Input Masks
|
Placeholder
|
Data Type
|
Description
|
|
|
Digit (09)
|
The character is required; the users are not allowed to include a plus sign (+) or a minus sign ().
|
|
9
|
Digit or space
|
The character is optional; the users are not allowed to include a plus sign (+) or a minus sign ().
|
|
#
|
Digit or space
|
The character is optional; the users are allowed to include a plus sign (+) or minus sign ().
|
|
L
|
Letter (az or AZ)
|
The character is required.
|
|
?
|
Letter (az or AZ)
|
The character is optional.
|
|
a
|
Letter or digit
|
The character is required.
|
|
A
|
Letter or digit
|
The character is optional.
|
|
&
|
Any character or space
|
The character is required.
|
|
C
|
Any character or space
|
The character is optional.
|
Table 4.4 lists the modifiers and literals you can use to build your input mask expressions.
Table 4.4. Modifiers and Literals to Use for Custom Input Masks
|
Modifier
|
Description
|
|
\
|
Displays the following character as a literal; for example, \( is displayed as (.
|
|
"
text
"
|
Displays the string
text
as a literal; for example, "MB" is displayed as MB.
|
|
.
|
Decimal separator.
|
|
,
|
Thousands separator.
|
|
: ; - /
|
Date and time separators.
|
|
<
|
Displays all the following
letters
as lowercase.
|
|
>
|
Displays all the following letters as uppercase.
|
|
!
|
Displays the input mask from right to left when you have optional data placeholders on the left.
|
|
Password
|
Displays the characters as
asterisks
so that other people can't read the data.
|
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}
You can enter your input mask expressions directly into the Input Mask property, or you can modify a predefined input mask using the Input Mask Wizard.
For example, suppose your company uses account numbers that consist of four uppercase letters and four digits, with a dash () in between. Here's an input mask suitable for entering such numbers:
>aaaa\-0000
Note, too, that input masks can contain up to three sections separated by semicolons (;):
first;second;third
first
This section holds the input mask expression.
second
This optional section specifies whether Access stores the literals in the table when you enter data. Use 0 to include the literals; use 1 (or nothing) to store only the data.
third
This optional section specifies the placeholder character. The default is the underscore (_).
For example, following is an input mask for a ZIP Code that stores the dash separator and displays dots (.) as placeholders:
00000\-9999;0;.
|