1. Restricting the Type of Data
For
example, if the data type is set to Number and you try to enter text,
Access refuses the entry and displays a warning.
When setting the data type of a field in a table in Design view,
you can choose from the following types:
-
Text
. Use for text fields that require up to 255 alphanumeric
characters. -
Memo
. Use for text fields that require up to 65,535 alphanumeric
characters. -
Number
. Use for numeric values. The size of the entry is
controlled by the Field Size property.
-
Date/Time
. Use for dates from January 2, 100 through December 31,
9999. Dates and times can be expressed in a variety of
formats. -
Currency
. Use for decimal values with up to 15 digits to the left of
the decimal point and up to 4 digits to the right. -
AutoNumber
. Use when you want Access to assign a unique number to each
new record. If you delete a record, its AutoNumber value is not
reused, and remaining records are not updated. -
Yes/No
. Use for fields that can have only two possible mutually
exclusive values, such as True or False.
Tip
In the database world, the Yes/No data type is more
commonly called Boolean, in honor of George
Boole, an early mathematician and logistician.
-
OLE Object
. Use to hold a graphic or other object. The object can be
linked or embedded. -
Hyperlink
. Use to hold a clickable path to a folder on your hard
disk, a network location, or a Web site. -
Attachment
. Use to attach a file to a record in the same way that you
might attach a file to an e-mail message.
Tip
The Attachment data type can be assigned to a
field only when the field is first created. You can’t assign the
Attachment type to an existing field, nor can you change an
Attachment field to another data type. For information about the
Attachment data type, search for Attach files and
graphics to the records in your database in Access
Help.
-
Calculated
. Use to hold the results of a calculation based on other
fields in the same table.
When adding a field in Datasheet view, you can choose what seems
to be an additional data type:
This is actually the Memo data type with the Text Format
property set to Rich Text instead of Plain Text.
You can also click the More Fields button in the Add &
Delete group on the Fields contextual tab to display a gallery of data
types with predefined properties that produce fields with common data
type refinements.
In this exercise, you’ll use various methods to add fields of
the most common data types to a table. Then you’ll enter data to test
the data type restrictions.
Set Up
You need the GardenCompany06_start database located in your
Chapter06 practice file folder to complete this exercise. Open the
GardenCompany06_start database, and save it as
GardenCompany06. Then follow the steps.
-
On the Create tab, in the
Tables group, click the
Table button.
Access opens a new blank table. As is normal with new
tables, Access has automatically generated an ID field that has
been assigned the AutoNumber data type. -
On the Fields contextual
tab, in the Add & Delete
group, click the Text button.
Then change the selected field name to fText.
If you use the name of a data type as the name of a field,
Access warns you that the name might cause problems. We will
identify all the fields in this table by their data type, preceded
by the letter f. -
Display the Click to Add
list, click Currency, and then
change the field name to fMoney. -
In the Add &
Delete group, click the More
Fields button.
A gallery of data types, some with refinements,
appears. -
In the gallery, under Number, click Standard. Then change the field name to
fNumber. -
Display the More Fields
gallery, click Medium Date
under Date and Time, and change
the field name to fDate. Then
display the gallery again, click Check
Box under Yes/No,
and name the field fBoolean.
The table now has six fields. -
Save the table with the name FieldTest, and then switch to Design
view.
Keyboard Shortcut
Press Ctrl+S to display the Save As dialog box.
-
In turn, click each field in the list at the top of the
design page, noticing the setting in the Data Type column and the property
settings in the Field
Properties area.
Only the properties that you can set for each type of field
are displayed. For the fNumber, fMoney, and fDate fields, the
Format property reflects the choice you made when creating the
field. -
Switch back to Datasheet view. Then in the fText cell, type This entry is 32 characters long. -
In the fMoney field, type
the word currency, and press
Tab.
Access tells you that it cannot accept the unexpected type
of data. -
In the list, click Enter new
value. Then type –45.3456, and press Tab.
Access stores the number the way you entered it but displays
($45.35), the default format for negative currency numbers.
Tip
Access uses the region and language settings in Windows
Control Panel to determine the display format for date, time,
currency, and other numbers. So although the numbers won’t
change, your currency symbol might be different; for example,
Access might display the pound, peso, or euro symbol. If you
want the symbol to remain the same no matter what the region and
language settings, you can create a custom format to ensure that
currency values always display a specific symbol.
-
In the fNumber
cell, type Five hundred, and
then press Tab.
Again, Access tells you that it cannot accept this type of
entry in this field. -
In the list of options, click Enter
new value, type 500,
and press Tab. -
In the fDate cell, type
123456, and press Tab. When
Access prompts for the correct type of entry, click Enter new value, click the Calendar button to the right of the
field, and click Today to
insert today’s date. Then press Tab.
All Date/Time fields come with an associated interactive
calendar. Using the Calendar button to insert a date works well if
the date you want is close to the current date. (You can click the
arrows at either end of the title bar to display the previous or
next month.) However, for distant dates such as birthdates, it is
easier to ignore the Calendar button and type the date. Access
accepts almost any entry that can be recognized as a date and
displays it in the format you specified when you created the
field.
Tip
If you enter a month and day but no year in a date field,
Access assumes the date is in the current year. If you enter a
month, day, and two-digit year from 00 through 29, Access
assumes the year is 2000 through 2029. If you enter a two-digit
year that is greater than 29, Access assumes you mean 1930
through 1999.
-
In the fBoolean field,
try to enter abc and 123. Then click several times anywhere in
the field to toggle the check box between the checked and not
checked states, finishing with the field in the checked
state.
This field won’t accept anything you type; you can only
switch between two predefined values.
Tip
The stored value of a Yes/No field is always either 1
(Yes) or 0 (No). On the General page of the Field Properties
area in Design view, you can set the Format property of the
field to interpret the stored value as True/False, Yes/No, or
On/Off. On the Lookup page, you can set the field to display as
a check box, text box, or combo box. (In a combo box, you can
select from a list or type an entry.)
Clean Up
Save the FieldTest table, and then close it. Retain the
GardenCompany06 database for use in later exercises.
|