Choosing Field Data Types, Sizes, and Formats

You must assign a field data type to each field of a table, unless you want to use the Text data type that Jet assigns by default. One principle of relational database design is that all data in a single field consists of one data type. Jet provides a much wider variety of data types and formats from which to choose than most other PC database managers. In addition to setting the data type, you can set other field properties that determine the format, size, and other characteristics of the data that affect its appearance and the accuracy with which numerical values are stored. Table 5.1 describes the field data types that you can select for data contained in Jet tables.

Table 5.1. Field Data Types Available in Jet 4.0

Information

Data Type

Description of Data Type

Characters

Text

Text fields are most common, so Access assigns Text as the default data type. A Text field can contain as many as 255 characters, and you can designate a maximum length less than or equal to 255. Access assigns a default length of 50 characters.

Characters

Memo

Memo fields ordinarily can contain as many as 65,535 characters. You use them to provide descriptive comments. Access displays the contents of Memo fields in Datasheet view. A Memo field can't be a key field.

Numeric Values

Number

Several numeric data subtypes are available. You choose the appropriate data subtype by selecting one of the Field Size property settings listed in Table 5.2. You specify how to display the number by setting its Format property to one of the formats listed in Table 5.3.

 

AutoNumber

An AutoNumber field is a numeric (Long Integer) value that Jet automatically fills in for each new record you add to a table. Jet can increment the AutoNumber field by 1 for each new record, or fill in the field with a randomly generated number, depending on the New Values property setting that you choose. The maximum number of records in a table that can use the AutoNumber field with the Long Integer size is slightly more than two billion.

 

Yes/No

Logical (Boolean) fields in Access use numeric values: 1 for Yes (True) and 0 for No (False). You use the Format property to display Yes/No fields as Yes or No, True or False, On or Off, or 1 or 0. (You can also use any nonzero number to represent True.) Logical fields can't be key fields but can be indexed.

 

Currency

Currency is a special fixed format with four decimal places designed to prevent rounding errors that would affect accounting operations in which the value must match to the penny.

Dates and Times

Date/Time

Dates and times are stored in a special fixed format. The date is represented by the whole number portion of the Date/Time value, and the time is represented by its decimal fraction. You control how Access displays dates by selecting one of the Date/Time Format properties listed in Table 5.3.

Large Objects (Binary Data)

OLE Object

Includes bitmapped and vector-type graphics, and other BLOBs (binary large objects), such as waveform audio files and video files. You can't assign an OLE Object field as a key field, nor can you include an OLE Object field in an index. Clicking an OLE Object in Datasheet view opens the object in its editing application.

Web Addresses

Hyperlink

Hyperlink fields store Web page document addresses. A Web address stored in the Hyperlink field can refer to a Web page on the Internet or one stored locally on your computer or network. Clicking a Hyperlink field in datasheet view causes Access to start your Web browser and display the referenced Web page. Choose Insert, Hyperlink to add a new hyperlink address to a Hyperlink field. Hyperlink is an Access, not a Jet, data type.

Related Data

Lookup Wizard

Lookup Wizard isn't a legitimate data type; it's a property of a field. Selecting Lookup Wizard starts the Lookup Wizard to add a lookup feature to the table. Most lookup operations execute a query to obtain data from a field of a related table.

To learn how to use the Lookup Wizard, see "Using Lookup Fields in Tables," p. 412.


Note

The OLE Object field data type is unique to Access; other applications that use Jet designate the OLE Object field data type as Binary or Long Binary. When you add an OLE object, such as a bit-mapped graphic from Windows Paint or a Word document, Access adds a special header, which identifies the source application, to the binary graphics data. Other applications can't read data from OLE Object fields you create in Access. OLE Object fields won't upsize to SQL Server 2000, because SQL server's image fields don't support the OLE Object data type.


Choosing Field Sizes for Numeric and Text Data

The Field Size property of a field determines which data type a Number field uses or how many characters fixed-length text fields can accept. Field Size properties are called subtypes to distinguish them from the data types listed in Table 5.1. For numbers, you select a Field Size property value from the Field Size drop-down list in the Table Design window's Field Properties pane (see Figure 5.6).

Figure 5.6. You can select one of seven Field Size (data subtype) property values for fields having a Number data type from the drop-down list.

graphics/05fig06.jpg

Subtypes for Numeric Data

The Number data type of the previously shown Table 5.1 isn't a fully specified data type. You must select one of the subtypes from those listed in Table 5.2 for the Field Size property to define the numeric data type properly. To select a data subtype for a Number field, follow these steps:

  1. Select the Data Type cell of the Number field for which you want to select the subtype.

  2. Click the Field Size text box in the Field Properties window. You also can press F6 to switch windows, and then use the arrow keys to position the caret within the Field Size text box.

  3. Click the drop-down arrow to open the list of choices shown previously in Figure 5.6. You can also press the F4 key to open the list.

  4. Select the data subtype. (Table 5.2 describes data subtypes.) When you make a selection, the list closes.

After you select a Field Size property, you select a Format property from those listed in Table 5.3 (later in this chapter) to determine how to display the data. Table 5.2 includes the Currency data type because it also can be considered a subtype of the Number data type.

Regardless of how you format your data for display, the number of decimal digits, the range, and the storage requirement remains that specified by the Field Size property.

Note

These data types are available in Visual Basic for Applications 6.0. VBA includes all the data types listed in Table 5.2 as reserved words. You can't use a reserved data type word for any purpose in VBA functions and procedures other than to specify a data type.


Table 5.2. Subtypes of the Number Data Type Determined by the Field Size Property

Field Size

Decimals

Range of Values

Bytes

Decimal

28 places

10^-28 to 10^28 1

14

Double

15 places

1.797 * 10^308 to +1.797 * 10^308

8

Single

7 places

3.4 * 10^38 to +3.4 * 10^38

4

Long Integer

None

2,147,483,648 to +2,147,483,647

4

Integer

None

32,768 to 32,767

2

Byte

None

0 to 255

1

Replication ID

None

Not applicable

16

Currency

4 places

922337203685477.5808 to +922337203685477.5808

8

For more information on VBA reserved words for data types, see "Data Types and Database Objects in VBA," p. 1159.


As a rule, you select the Field Size property that results in the smallest number of bytes that encompasses the range of values you expect and that expresses the value in sufficient precision for your needs. Mathematical operations with Integer and Long Integer proceed more quickly than those with Single and Double data types (called floating-point numbers) or the Currency and Date/Time data types (fixed-point numbers). Microsoft added the Decimal data subtype for conformance with the SQL Server decimal data type.

Tip

Always use the Decimal data type for fractional values such as percentages that you intend to use for calculating Currency or other Decimal values. The Order Details table's Discount field uses the Jet Single data type, which is notorious for causing rounding errors in decimal calculations.


Note

You can apply the Replication ID field size to Number or AutoNumber fields. A replication ID is a specially formatted 32-character (16-byte) hexadecimal number (values 0 through 9 and A through F) surrounded by French braces. The more common name for a replication ID is globally unique identifier (GUID, pronounced "goo id" or "gwid"). A typical GUID looks like {8AA5F467-3AF5-4669-B4CB-5207CDC79EF4}. GUID values, which Windows calculates for you, supposedly are unique throughout the world. If you apply the Replication ID field size to an AutoNumber field, Access automatically adds a GUID value for each row of the table.


Fixed-Width Text Fields

You can create a fixed-width Text field by setting the value of the Field Size property. By default, Access creates a 50-character-wide Text field. Enter the number, from 1 to 255, in the Field Size cell corresponding to the fixed length that you want. If the data you import to the field is longer than the selected field size, Access truncates the data; so, you lose the far right characters that exceed your specified limit. You should enter a field length value that accommodates the maximum number of characters that you expect to enter in the field.

Note

The terms fixed-width and fixed-length have two different meanings in Access. Even if you specify a fixed-width for a field of the Text field data type, Access stores the data in the field in variable-length format. Thus, setting the Length value to 255 for all Text fields has no effect on the ultimate size of the database file.


Selecting a Display Format

You establish the Format property for the data types that you select so that Access displays them appropriately for your application. You select a format by selecting the field and then clicking the Format text box in the Field Properties window. Figure 5.7 shows the choices that Access offers for formatting the Long Integer data type. You format Number, Date/Time, and Yes/No data types by selecting a standard format or creating your own custom format. The following sections describe these two methods.

Figure 5.7. You can apply one of seven numeric display formats to fields of the Number Data type and the Long Integer subtype. Access 2000 added the Euro format.

graphics/05fig07.jpg

Standard Formats for Number, Date/Time, and Yes/No Data Types

Access provides 18 standard formats that apply to the numeric values in fields of the Number, Date/Time, and Yes/No data types. Format is an Access, not a Jet, field property. The standard formats shown in Table 5.3 should meet most of your needs.

Table 5.3. Standard Display Formats for Access's Number, Date/Time, and Yes/No Data Types

Data Type

Format

Appearance

Number

General Number

1234.5

 

Currency

$1,234.50

 

Euro

£1,234.50

 

Fixed

12345 or 12345.00 depending on Decimal Places setting

 

Standard

1,234.50

 

Percent

0.1234 = 12.34%

 

Scientific

1.23E+03

Date/Time

General Date

3/1/99 4:00:00 PM

 

Long Date

Thursday, March 1, 2003

 

Medium Date

1-Mar-2003

 

Short Date

3/1/2003

 

Long Time

4:00:00 PM

 

Medium Time

04:00 PM

 

Short Time

16:00

Yes/No

Yes/No

Yes or No

 

True/False

True or False

 

On/Off

On or Off

 

None

1 or 0

Microsoft's Year 2000 (Y2K) compliance features include an Access 2000 addition to the General page of the Options dialog (Tools menu). The Use Four-Digit Year Formatting frame has two check boxes: This Database and All Databases. Marking either check box changes Date/Time field formatting as shown in Table 5.4. Long Date and Time formats don't change; the formatting shown in the Access 2003 Default column is based on the standard Windows Short Date format, m/d/yy.

Table 5.4. A Comparison of Access 2003 Default and Four-Digit Year Formatting

Date/Time Format

Access 2003 Default

With Four-Digit Year

General Date (default)

1/15/03 10:10 AM

1/15/2003 10:10 AM

Short Date

1/15/03

1/15/2003

Long Date

Friday January 15, 2003

Friday January 15, 2003

Medium Date

15-Jan-03

15-Jan-2003

Medium Time

10:10 AM

10:10 AM

mm/dd/yy

01/15/03

01/15/2003

Marking the This Database check box sets a flag in the current database, so the formatting changes apply only to the current database. Marking the All Databases check box adds a Registry entry to your PC, so opening any Jet database in Access forces four-digit year formatting.

Tip

Access's Short Date (m/d/yy and mm/dd/yy) formats for the English (United States) locale default to two-digit years unless you change the default date format of Windows or set the Four-Digit Year Formatting option(s). Two-digit year presentation isn't Y2K compliant. To make the Windows short date format Y2K compliant for most applications, open Control Panel's Regional and Language Options tool, click the Customize button and the Date tab, and check the Short Date setting. If the Short Date style is M/d/yy, change it to M/d/yyyy.


The Null Value in Jet Tables

Fields in Jet tables can have a special value, Null, which is a new term for most spreadsheet users. The Null value indicates that the field contains no data at all. Null is similar but not equivalent to an empty string (a string of zero length, "", often called a null string). For now, the best synonym for Null is no entry or unknown.

The Null value is useful for determining whether a value has been entered in a field, especially a numeric field in which zero values are valid. The next section and the later "Setting Default Values of Fields" section use the Null value.

Custom Display Formats

To display a format that's not a standard format in Access, you must create a custom format. You can set a custom display format for any field type, except OLE Object, by creating an image of the format with combinations of a special set of characters called placeholders (see Table 5.5). Figure 5.8 shows an example of a custom format for date and time. If you type mmmm dd, yyyy - hh:nn as the format, the date 03/01/01 displays as March 1, 2001 - 00:00. Access automatically adds double quotes around the comma when you save the table.

Figure 5.8. If one of the standard Format property values doesn't meet your needs, you can type a string to represent a custom format in the Format text box.

graphics/05fig08.jpg

Except as noted, the sample numeric value that Table 5.4 uses is 1234.5. Bold type distinguishes the placeholders that you type from the surrounding text. The resulting display is shown in monospace type.

Table 5.5. Placeholders for Creating Custom Display Formats

Placeholder

Function

Empty string

Displays the number with no formatting. Enter an empty string by deleting the value in the Format field of the Field Properties pane.

0

Displays a digit if one exists in the position, or a zero if not. You can use the 0 placeholder to display leading zeros for whole numbers and trailing zeros in decimal fractions. 00000.000 displays 01234.500.

#

Displays a digit, if one exists in the position. The # placeholder is similar to 0, except that leading and trailing zeros aren't displayed. #####.### displays 1234.5.

$

Displays a dollar sign in the position. $###,###.00 displays $1,234.50.

.

Displays a decimal point at the indicated position in a string of 0 and # placeholders. ##.## displays 1234.5.

%

Multiplies the value by 100 and adds a percent sign in the position shown with 0 and # placeholders. #0.00% displays 0.12345 as 12.35% (12.345 is rounded to 12.35).

,

Adds commas as thousands separators in strings of 0 and # placeholders. ###,###,###.00 displays 1,234.50.

E- e-

Displays values in scientific format with the sign of exponent for negative values only. #.####E-00 displays 1.2345E03. 0.12345 is displayed as 1.2345E-01.

E+ e+

Displays values in scientific format with the sign of exponent for positive and negative values. #.####E+00 displays 1.2345E+03.

/

Separates the day, month, and year to format date values. Typing mm/dd/yyyy displays 03/06/2003. (You can substitute hyphens for virgules to display 03-06-2003.)

m

Specifies how to display months for dates. m displays 1, mm displays 01, mmm displays Jan, and mmmm displays January.

d

Specifies how to display days for dates. d displays 1, dd displays 01, ddd displays Mon, and dddd displays Monday.

y

Specifies how to display years for dates. yy displays 01; yyyy displays 2003.

:

Separates hours, minutes, and seconds in format time values. hh:mm:ss displays 02:02:02.

h

Specifies how to display hours for time. h displays 2; hh displays 02. If you use an AM/PM placeholder, h or hh displays 4 PM for 16:00 hours.

n

Minutes placeholder for time. n displays 1; nn displays 01. hhnn "hours" displays 1600 hours.

s

Seconds placeholder for time. s displays 1; ss displays 01.

AM/PM

Displays time in 12-hour time with AM or PM appended. h:nn AM/PM displays 4:00 PM. Alternative formats include am/pm, A/P, and a/p.

@

Indicates that a character is required in the position in a Text or Memo field. You can use @ to format telephone numbers in a Text field, as in @@@-@@@-@@@@ or (@@@) @@@-@@@@.

&

Indicates that a character in a Text or Memo field is optional.

>

Changes (custom display format) placeholder> all text characters in the field to uppercase.

<

Changes all text characters in the field to lowercase.

*

Displays the character following the asterisk as a fill character for empty spaces in a field. "ABCD"*x in an eight-character field appears as ABCDxxxx.

The Format property is one of the few examples in Access in which you can select from a list of options or type your own entry. Format uses a true drop-down combo list; lists that enable you to select only from the listed options are drop-down lists. The comma is a nonstandard formatting symbol for dates (but is standard for number fields). When you create nonstandard formatting characters in the Field Properties window, Access automatically encloses them in double quotation marks.

graphics/opening_table_displays.gif graphics/design_view.gif When you change Format or any other property field, and then change to Datasheet view to view the result of your work, you must first save the updated table design. The confirmation dialog shown at the top of Figure 5.9 asks you to confirm any design changes. Clicking No returns you to Table Design view. If you want to discard your changes, close Table Design view and click No when asked if you want to save your changes (see Figure 5.9, bottom).

Figure 5.9. Changing from Design to Datasheet view after making changes to the table's design displays the upper message box. If you close the table in Design view, the lower message box gives you the option of saving or discarding changes, or returning to Table Design view.

graphics/05fig09.jpg

If you apply the custom format string mmmm dd", "yyyy (refer to Figure 5.8) to the BirthDate field of the Employees table, the BirthDate field entries appear as shown in Figure 5.10. For example, Nancy Davolio's birth date appears as December 08, 1968. The original format of the BirthDate field was dd-mmm-yyyy, the format also used for the HireDate field. The BirthDate caption property value appears in the heading row.

Figure 5.10. The BirthDate field of the modified employees table displays the effect of applying mmmm dd", "yyyy as the custom date/time format.

graphics/05fig10.gif

You must expand the width of the BirthDate field to accommodate the additional characters in the Long Date format. You increase the field's width by dragging the field name header's right vertical bar to the right to display the entire field. Access displays the time of birth as 00:00 because the decimal fraction that determines time is 0 for all entries in the BirthDate field.

The following is an example that formats negative numbers enclosed in parentheses and replaces a Null entry with text:

 $###,###,##0.00;$(###,###,##0.00);0.00;"No Entry Here" 

The entries 1234567.89, 1234567.89, 0, and a Null default value appear as follows:

 $1,234,567.89 $(1,234,567.89) 0.00 No Entry Here 

Using Input Masks

Access 2003 lets you restrict entries in Text fields to numbers or to otherwise control the formatting of entered data. Access 2003's Input Mask property is used to format telephone numbers, Social Security numbers, ZIP codes, and similar data. Table 5.6 lists the placeholders that you can use in character strings for input masks in fields of the Text field data type.

Table 5.6. Placeholders for Creating Input Masks

Placeholder

Function

Empty string

No input mask

0

Number (0 9) required; sign (+/ )not allowed

9

Number (0 9) or space optional; sign (+/-) not allowed

#

Number (0 9) or space optional (a space if nothing is entered)

L

Letter (A Z) required

?

Letter (A Z) not required (a space if nothing is entered)

A

Letter (A Z) or number (0 9) required

a

Letter (A Z) or number (0 9) optional

&

Any character or a space required

C

Any character or a space optional

Password

Displays the characters you type as asterisks (***...) to prevent others from viewing the entry

. , : ; / ( )

Literal decimal, thousands, date, time, and special separators

>

All (input mask) placeholder> characters to the right are converted to uppercase

<

All characters to the right are converted to lowercase

!

Fills the mask from right to left

\

Precedes the other placeholders to include the literal character in a format string

For example, typing \(000") "000\-0000 as the value of the Input Mask property results in the appearance of (___) ___-____ for a blank telephone number cell of a table. Typing 000\-00\-0000 creates a mask for Social Security numbers, ___-__-____. When you type the telephone number or Social Security number, the digits that you type replace the underscores.

Note

The \ characters (often called escape characters) that precede parentheses and hyphens specify that the character that follows is a literal, not a formatting character. If the format includes spaces, enclose the spaces and adjacent literal characters in double quotation marks, as shown for the telephone number format.


graphics/builder.gif Access includes an Input Mask Wizard that opens when you move to the Input Mask field for the Text or Date/Time field data type and click the Builder (...) button at the extreme right of the text box. Figure 5.11 shows the opening dialog of the Input Mask Wizard, which lets you select from 10 common input mask formats.

Figure 5.11. The Input Mask Wizard lets you select one of 10 preset formats to specify a fixed data entry pattern for the selected field. In the second wizard dialog, you can add a custom format.

graphics/05fig11.jpg



Special Edition Using Microsoft Office Access 2003
Special Edition Using Microsoft Office Access 2003
ISBN: 0789729520
EAN: 2147483647
Year: 2005
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net