The Default Value Property: Saving Data-Entry Time
Assigning a Default Value property to a
field causes a specified value to be filled in for the field in new
records. Setting a commonly used value as the Default Value
property facilitates the data entry process. When adding data, you can
accept the default entry or replace it with another value. For example,
if most of your customers are in California, you can assign a default
value of ”CA”. When doing data entry, if the customer is in
California you will not need to change the value for the state. If the
customer is in another state, you just replace the ”CA” with the appropriate state value.
Set the Default Value Property from Design View
To set a Default Value property (from Design view), follow these steps:
1. | Select the desired field from the top pane of the Design view window.
|
2. | Click the Default Value property text box in the Field Properties pane.
|
3. | Type the desired value.
|
A Default Value property can be constant, such as CA for California, or a function that returns a value, such as Date(), which displays the current date.
|
The data users enter in tables must be accurate if the database is to be valuable to you or your organization. You can use the Validation Rule property to add data entry rules to the fields in tables.
Date() is a built-in Visual Basic for
Applications (VBA) function that returns the current date and time.
When it is used as a default value for a field, Access enters the
current date into the field when the user adds a new row to the table.
|
The Validation Rule and Validation Text Properties: Controlling What the User Enters in a Field
The Default Value property suggests a value to the user, but the Validation Rule
property actually limits what the user can place in the field.
Validation rules cannot be violated; the database engine strictly
enforces them. As with the Default Value property, this
property can contain either text or a valid Access expression, but you
cannot include user-defined functions in the Validation Rule property. You also cannot include references to forms, queries, or tables in the Validation Rule property.
You can use operators to compare two values; the
less than (<) and greater than (>) symbols are examples of
comparison operators. And, Or, Is, Not, Between, and Like are called logical operators. Table 2 provides a few examples of validation rules.
Table 2. Examples of Validation Rules
Validation Rule | Validation Text Examples |
---|
>0 | Please enter a valid Employee ID Number. |
“H” or “S” or “Q” | Only H or S or Q codes will be accepted. |
Between Date()-365 and Date()+365 | Date cannot be later than one year ago today or more than one year from today. |
>0 or is Null | Enter a valid ID number or leave blank if not approved. |
Between 0 and 9 or is Null | Rating range is 0 through 9 or is blank. |
>Date() | Date must be after today. |
Whereas the validation rule limits what the user can
enter into the table, the validation text provides the error message
that appears when the user violates the validation rule.
If you set the Validation Rule property but do not set the Validation Text
property, Access automatically displays a standard error message
whenever the user violates the validation rule. To display a custom
message, you must enter message text in the Validation Text property.
|
Set the Validation Rule Property from Design View
To establish a field-level validation rule (from Design view), follow these steps:
1. | Select the desired field name from the top pane of the Design view window.
|
2. | Click the Validation Rule text box in the Field Properties pane.
|
3. | Type the desired validation rule (for example, Between 0 and 120).
|
Set the Validation Text Property from Design View
To add validation text, follow these steps:
1. | Click the Validation Text text box in the Field Properties pane.
|
2. | Type the desired text (for example, Age Must be Between 0 and 120).
|
You can require users of a database to
enter a valid value in selected fields when editing or adding records.
For example, you can require a user to enter a date for each record in
an Invoice table.