Restricting the Type of Data in a Field





Restricting the Type of Data in a Field

The Data Type setting restricts entries in a field to a specific type of data, such as text, numbers, or dates. If, for example, the data type is set to Number and you attempt to enter text, Access refuses the entry and displays a warning.

The field properties you can set to control input are:

  • Required

  • Allow Zero Length

  • Field Size

  • Input Mask

  • Validation Rule

The Required and Allow Zero Length properties are fairly obvious. If the Required property is set to Yes, the field can't be left blank. However, Access differentiates between a blank field (which it refers to as a Null field) and a field that looks blank, but contains an empty string. If Allow Zero Length is set to Yes, you can enter an empty string (two quotation marks with nothing in between), which looks like a blank field, but it is classified as empty rather than Null. This differentiation might seem silly, but if you are using programming code to work with an Access database, you will find that some commands produce different results for Null fields than they do for empty fields.

The Field Size, Input Mask, and Validation Rule properties are more complex, so the exercises in this chapter will focus on them.

Tip

Each field property has many options. For more information, search for field property in Access Help.


In this exercise, you will add fields of the most common data types to a table, and then use the Data Type setting and Field Size property to restrict the data that can be entered into the table.

USE the 01_FieldTest database. This practice file is located in the Chapter07 subfolder under SBS_Access2007.

OPEN the 01_FieldTest database. Then display the Field Property Test table in Design view.


1.
Click in the first available Field Name cell (below the automatically-generated ID field), type fText, and then press the key to move to the Data Type cell.

The data type defaults to Text.

2.
In the second Field Name cell, type fNumber, and then press .

3.
Click the Data Type arrow, and in the list, click Number.

Tip

You can scroll the list to the data type you want by typing the first letter of its name in the cell.

4.
Repeat Steps 2 and 3 to add the following fields:

Field

Data type

fDate

Date/Time

fCurrency

Currency

fBoolean

Yes/No


Tip

The data type referred to as Yes/No in Access is more commonly called Boolean (in honor of George Boole, an early mathematician and logistician). This data type can hold either of two mutually exclusive values, often expressed as yes/no, 1/0, on/off, or true/false.

5.
Click the fText field name to select it.

Access displays the properties of the selected field in the lower portion of the dialog box.

6.
Repeat Step 5 to review the properties of each field, and then on the Quick Access Toolbar, click the Save button.

Save

7.
On the View toolbar, click the Datasheet View button.

Datasheet View

8.
In the fText cell, type This entry is 32 characters long.

9.
In the fNumber cell, type Five hundred.

The data type for this field is Number. Access does not accept your text entry, and displays a warning offering you several options.

10.
In the Microsoft Office Access message box, click Enter new value. Then replace Five Hundred with 500.

11.
In the fDate cell, type date, and then press .

Access does not accept the unexpected data format.

12.
In the Microsoft Office Access message box that appears, click Enter new value, type Jan 1, and then press .

The fDate field accepts almost any entry that can be recognized as a date, and displays it in the default date format. Depending on the default format on your computer, Jan 1 might be displayed as 1/1/2007, 1/1/07, or in some other format.

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 30, Access assumes the year is 2000 through 2030. If you enter a two-digit year that is greater than 30, Access assumes you mean 1931 through 1999.

13.
In the fCurrency field, type the word currency, and then press .

Access does not accept the unexpected data format.

14.
In the Microsoft Office Access message box that appears, click Enter new value, type 45.3456, and then press .

Access stores the number you entered but displays ($45.35), the default format for negative currency numbers.

Tip

Access uses the regional settings in the Windows Control Panel to determine the display format for date, time, currency, and other numbers. You can create custom formats to ensure that the correct currency symbol is always displayed with your values. Otherwise, the numbers won't change, but the currency symbol might, for instance from dollars to pounds, pesos, or euros.

15.
In the fBoolean field, enter 123. Then click anywhere in the field to toggle the check box between No (not checked) and Yes (checked), finishing with the field in the checked state.

This field won't accept anything you type; you can switch only between two predefined values.

Tip

In Design view, you can open the Properties dialog box, and on the Lookup tab, set the Boolean field to display as a check box, text box, or combo box. You can set the Format property on the General tab to use True/False, Yes/No, or On/Off as the displayed values in this field (though the stored values will always be -1 and 0).

CLOSE the table without saving your changes, and then close the 01_FieldTest database.




 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows