Introduction to data types and field properties.Data types for Access desktop databases
Unique values are crucial, because otherwise there is no way to reliably distinguish a particular row from other rows. When you create a new table in Datasheet view, Access automatically creates a primary key for you and assigns it a field name of ID and the AutoNumber data type. In Design view, you can change or remove the primary key, or set the primary key for a table that doesn’t already have one. Sometimes, you might already have data that you want to use as a primary key.
For example, you may have existing ID numbers for your employees. If you create a table to track employee information, you might decide to use the existing employee ID as the primary key for the table. Or, perhaps employee ID is only unique in combination with department ID, requiring that you use both fields together as the primary key.
A good candidate for the primary key has the following characteristics:. If no suitable data exists to use as a primary key, you can create a new field to use as a primary key. When you create a new field to use as a primary key, set the field’s data type to AutoNumber to help make sure that it meets the three characteristics in the preceding list.
On the Design tab, in the Tools group, click Primary Key. Click the row selector for the current primary key. If the primary key consists of multiple fields, hold down CTRL, and then click the row selector for each field. The key indicator is removed from the field or fields that you previously specified as the primary key. When you save a new table without setting a primary key, Access prompts you to create a new field for the primary key. If you click Yes , Access creates an ID field that uses the AutoNumber data type to provide a unique value for each record.
If your table already includes an AutoNumber field, Access uses it as the primary key. If you click No , Access does not add a field, and no primary key is set.
To store a new piece of data about something for which you already have an Access table, consider adding a field to the table.
For example, suppose you have a table that stores the last name, first name, email address, telephone number, and mailing address of each of your customers. You store each piece of data that you want to track in a field. Every field has certain essential characteristics, such as a name that uniquely identifies the field within a table, a data type that defines the nature of the data, the operations that can be performed on the data, and how much storage space to set aside for each value.
Before you create fields, try to separate data into its smallest useful parts. It is much easier to combine data later than it is to pull it apart. Then, you can easily search or sort by First Name, Last Name, or both. If you plan to report, sort, search, or calculate on an item of data, put that item in a field by itself. After you create a field, you can also set field properties to control its appearance and behavior. For example, the Format property defines how the data appears in a datasheet or form that contains that field.
When you create a new table or open an existing table in Datasheet view, you can add a field to the table by entering data in the Add New Field column of the datasheet 1. Access automatically assigns a data type for the field, based on the value that you enter.
If no other data type is implied by your input, Access sets the data type to Text but you can change the data type. Create or open a table in Datasheet view by right-clicking the table that you want in the Navigation Pane and then clicking Datasheet view from the shortcut menu.
In the Add New Field column, enter the name of the field that you want to create. Sometimes it is easier to choose from a predefined list of fields that fit your needs than to manually create a field. You can use the More Fields list to choose from a list of field templates. A field template is a predefined set of characteristics and properties that describes a field.
The field template definition includes a field name, a data type, a setting for the field’s Format property, and other field properties. Select a field in the More Fields list to insert the new column. Access places the field to the right of the column where your cursor is currently located. If you choose one of the field options under the Quick Start heading, such as Address, Access creates multiple fields in your table to contain the various parts of an address.
You can set some of the available field properties while you work in Datasheet view. To have access to and set the complete list of field properties; however, you must use Design view.
You can rename a field, change its data type, change its Format property, and change some of a field’s other properties while you work in Datasheet view. When you add a field by entering data in Datasheet view, Access automatically assigns a generic name to the field. Access assigns the name Field1 to the first new field, Field2 to the second new field, and so on.
By default, a field’s name is used as its label wherever the field is displayed, such as a column heading on a datasheet. Renaming fields so that they have more descriptive names helps make them easier to use when you view or edit records. When you create a field by entering data in Datasheet view, Access examines that data to determine the appropriate data type for the field. If Access can’t definitively determine the data type, the data type is set to Text by default Short Text if you’re using Access The data type of the field determines which other field properties you can set.
For example, you can set only the Append Only property for a field that has the Hyperlink data type or the Memo data type Long Text if you’re using Access There may be cases where you want to manually change a field’s data type. Because room numbers are labels, and not dates, they should use the Text data type. Use the following procedure to change a field’s data type. In the Data Type list, in the Formatting group, select the data type that you want. See Data types for Access desktop databases for a complete list of available data types in Access databases.
To optimize performance, you should use the most appropriate Field Size when you create a Text or Number field. For example, if you expect to store postal codes of a predictable length, specify that length as the field size.
You can specify the field size by setting a value in the Field Size property box. For more information, see the section, Set other field properties. For phone numbers, part numbers, and other numbers that you don’t intend to use for mathematical calculations, you should select the Text data type instead of the Number data type. A numeric value that is stored as text can be sorted and filtered more logically.
In addition to determining the data type of a new field, Access may also set the Format property for the field, depending on what you enter. For example, if you enter a. To manually change a field’s Format property, do the following:.
In the Format list, in the Formatting group, enter the format you want. Note: The Format list may be unavailable for some fields for example, Text , depending on the data type of the field.
On the Fields tab, in the Properties , Formatting , or Field Validation groups, select the properties that you want. You can set any field property while you work with a table in Design view. In Design view, you set a field’s data type in the table design grid, and you set other properties in the Field Properties pane.
Warning: You can also change these aspects of a field that you did not just create. However, if data already exists in the field some actions will not be available, or may cause data loss. A numeric value that is stored as text can be sorted and filtered more logically, but cannot be easily used in calculations.
For the Text and Number data types, you can specify the field size or data type more precisely by setting a value in the Field Size property box. Note: Not all formats are available for all data types. Set the data type first, and then, if needed, set the format. In the table design grid, select the field for which you want to set properties. Access displays the properties for this field in the Field Properties pane.
In the Field Properties pane, enter the settings that you want for each property, or press F6 and then use the arrow keys to select a property. See Introduction to data types and field properties for a complete list of field properties available for each data type in Access databases.
Note: Not all properties are available for every field. A field’s data type determines which properties it has. Tip: If you are entering an input mask or validation expression and would like help in building it, click next to the property box to display the appropriate builder.
To move a field, drag it to the position that you want. To select multiple contiguous fields to move, click the first field, hold down the SHIFT key, and then click the last field. You can then drag the selected group of fields to a new position. Dragging a field changes its position on the Datasheet, but does not change the order of the fields in the table design. If you programmatically access the fields, the original order applies.
For example, if you drag a field to a new position on the Datasheet, and then create a form from the table by using the Form button, the field will be in its original position. Add a calculated field to a table. Add a field to a form or report. Creating tables. Create a table and add fields. Need more help? Expand your skills. Get new features first. Was this information helpful? Yes No. Thank you! Any more feedback? The more you tell us the more we can help. Can you help us improve? Add a validation rule to a table.
Test existing data against a new validation rule. Add a validation rule to a control on a form. Validation rule reference. This article explains how to use validation rules and validation text in table fields and form controls. A validation rule is one way to restrict input in a table field or a control such as a text box on a form.
Validation text lets you provide a message to help users who input data that is not valid. When data is entered, Access checks to see whether the input breaks a validation rule — if so, the input is not accepted, and Access displays a message. Data types Every table field has a data type that restricts what users can enter. Field properties Some field properties restrict data input. For example, the Field Size property of a field restricts input by limiting the amount of data. You can also use the Validation Rule property to require specific values, and the Validation Text property to alert your users to any mistakes.
Entering text such as “Enter values between and 1,” or “Enter an ending date on or after the start date” in the Validation Text property tells users when they have made a mistake and how to fix the error. Input masks You can use an input mask to validate data by forcing users to enter values in a specific way. For example, an input mask can force users to enter dates in a European format, such as You can use these methods of validating data alone or in combination with each other.
Data types are not optional, and provide the most basic type of data validation. For more information about data types, field sizes, and input masks, see the article Introduction to data types and field properties.
Field validation rules Use a field validation rule to check the value that you enter in a field when you leave the field. Your rule now requires users to enter dates on or after January 1, If you enter a date earlier than and then try to place the focus on another field, Access prevents you from leaving the current field until you fix the problem.
Record validation rules Use a record validation rule to control when you can save a record a row in a table. Unlike a field validation rule, a record validation rule refers to other fields in the same table. You create record validation rules when you need to check the values in one field against the values in another.
For example, suppose your business requires you to ship products within 30 days and, if you don’t ship within that time, you must refund part of the purchase price to your customer. If the syntax for validation rules looks cryptic, see the section What you can put in a validation rule for an explanation of the syntax and some example validation rules. You can define validation rules for table fields and for controls on forms.
When you define rules for tables, those rules apply when you import data. To add validation rules to a table, you open the table and use commands on the Fields tab of the ribbon. To add validation rules to a form, you open the form in Layout view and add rules to the properties of individual controls. The steps in the section Add a validation rule to a table explain how to add validation rules to table fields.
The steps in the section Add a validation rule to a control on a form , later in this article, explain how to add rules to the properties in individual controls. Your validation rules can contain expressions — functions that return a single value.
You can use an expression to perform calculations, manipulate characters, or test data. A validation rule expression tests data. Expressions can also perform mathematical operations. The expression [OrderDate] – [ShipDate] calculates the number of days that elapsed between the time an order was placed and the time it shipped. For more information about expressions, see the article Build an expression. Top of Page. A field validation rule checks input to a field, and is applied when the focus leaves the field.
A record validation rule checks input to one or more fields is applied when the focus leaves the record. Usually, a record validation rule compares the values of two or more fields. Notes: The following field types do not support validation rules:. Use the Expression Builder to create the rule. Select the field that needs a message for input that is not valid. The field should already have a validation rule.
Enter an appropriate message. For some examples of field validation rules and messages, see the section Validation rule reference. Open the table that needs a message for input that is not valid. The table should already have a record validation rule. If you add a validation rule to an existing table, you might want to test the rule to see whether any existing data is not valid.
You might see a variety of other alert messages as you proceed. Read the instructions in each message, and then click Yes or No , as appropriate, to complete or stop the testing. You can use the Validation Rule property and the Validation Text property of a form control to validate data that is input to that control and to help users who input data that is not valid. Tip: If you create a form automatically from a table by using one of the Form commands on the ribbon, any validation for fields in the underlying table are inherited by the corresponding controls on the form.
A control can have a different validation rule from the table field to which the control is bound. This is useful if you want the form to be more restrictive than the table.
The form rule is applied, and then the table rule is applied. If the table is more restrictive than the form, the rule defined for the table field takes precedence. If the rules are mutually exclusive, they prevent you from entering any data at all.
The date field now requires values earlier than the year , but the form control requires dates have that year or later, thus preventing you from entering any data at all.
Right-click the control that you want to change, and then click Properties to open the property sheet for the control.
– Set the field size
Every table in Access is made up of fields. The properties of a field describe the characteristics and behavior of data added to that field.
A field’s data type is the most important property because it determines what kind of data the field can store. This article describes the data types and other field properties available in Access, and includes additional information in a detailed data type reference section. When to use which data type. Field size property. Data types in relationships and joins.
Data type reference. Data types can seem confusing for example, if a field’s data type is Text, it can store data that consists of either text or numerical characters. But a field whose data type is Number can store only numerical data.
So, you have to know what properties are used with each data type. A field’s data type can be predefined or you will select a data type depending on how you create the new field. For example, if you create a field from the Datasheet view and:. Use an existing field from another table, the data type is already defined in the template or in the other table. Enter data in a blank column or field , Access assigns a data type to the field based on the values that you enter or you can assign the data type and format for the field.
Think of a field’s data type as a set of qualities that applies to all the values that are contained in the field. For example, values that are stored in a Text field can contain only letters, numbers, and a limited set of punctuation characters, and a Text field can only contain a maximum of characters.
Tip: Sometimes, the data in a field may appear to be one data type, but is actually another. For example, a field may seem to contain numeric values but may actually contain text values, such as room numbers.
You can often use an expression to compare or convert values of different data types. The following tables show you the formats available for each data type and explain the effect of the formatting option. Short, alphanumeric values, such as a last name or a street address.
Note, beginning in Access , Text data types have been renamed to Short Text. Results of a calculation. The calculation must refer to other fields in the same table. You would use the Expression Builder to create the calculation. Note, Calculated fields were first introduced in Access Attached images, spreadsheet files, documents, charts, and other types of supported files to the records in your database, similar to attaching files to e-mail messages.
Long blocks of text. A typical use of a Memo field would be a detailed product description. Note, beginning in Access , Memo data types have been renamed to Long Text. Displays either a list of values that is retrieved from a table or query, or a set of values that you specified when you created the field.
The Lookup Wizard starts and you can create a Lookup field. The data type of a Lookup field is either Text or Number, depending on the choices that you make in the wizard.
Lookup fields have an additional set of field properties, which are located on the Lookup tab in the Field Properties pane. Note: Attachment and Calculated data types aren’t available in. Display the date in a short format.
Depends on your regional date and time settings. Display the date in a long format. Depends on you’re the regional date and time settings. Display the time only using a 12 hour format that will respond to changes in the regional date and time settings. Display the time only using a 24 hour format that will respond to changes in the regional date and time settings.
After you create a field and set its data type, you can set additional field properties. The field’s data type determines which other properties that you can set. For example, you can control the size of a Text field by setting its Field Size property. For Number and Currency fields, the Field Size property is especially important, because it determines the range of field values.
For example, a one-bit Number field can store only integers ranging from 0 to The Field Size property also determines how much disk space each Number field value requires.
Depending on the field size, the number can use exactly 1, 2, 4, 8, 12, or 16 bytes. Note: Text and Memo fields have variable field value sizes. For these data types, Field Size sets the maximum space available for any one value. For more details about field properties and how they work with the different data types go to the Data type reference section.
Also read the article Set the field size. A table relationship is an association between common fields in two tables. A relationship can be one-to-one, one-to-many, or many-to-many. A join is a SQL operation that combines data from two sources into one record in a query recordset based on values in a specified field that the sources have in common.
A join can be an inner join, a left outer join, or a right outer join. When you create a table relationship or add a join to a query, the fields that you connect must have the same or compatible data types.
For example, you cannot create a join between a Number field and a Text field, even if the values in those fields match. In a relationship or a join, fields that are set to the AutoNumber data type are compatible with fields that are set to the Number data type if the Field Size property of the latter is Long Integer.
You cannot change the data type or the Field Size property of a field that is involved in a table relationship. You can temporarily delete the relationship to change the Field Size property. However, if you change the data type, you won’t be able to re-create the relationship without first also changing the data type of the related field. For more information on tables see the article, Introduction to tables. When you apply a data type to a field, it contains a set of properties that you can select.
Click on data types below for more information. Large Number. OLE Object. Purpose Used in a field that allows attaching files or images to a record. For example, if you have a job contacts database, you can use an attachment field to attach a photo of the contact, or attach documents such as a resume. For some file types, Access compresses each attachment as you add it.
Attachment data types are available only in. You can attach many kinds of files to a record. However, some file types that may pose security risks are blocked. As a rule, you can attach any file that was created in one of the Microsoft Office programs. You can also attach log files. For a list of supported image file formats, see the table later in this section. The label text that is displayed for this field by default in forms, reports, and queries. If this property is empty, the name of the field is used.
Any text string is allowed. Access supports the following graphic file formats without the need for additional software being installed on your computer.
In addition, file names must follow the following guidelines:. Names cannot contain the following characters: question marks? Purpose Use an AutoNumber field to provide a unique value that serves no other purpose than to make each record unique.
The most common use for an AutoNumber field is as a primary key, especially when no suitable natural key a key that is based on a data field is available. An AutoNumber field value requires 4 or 16 bytes, depending on the value of its Field Size property. Suppose that you have a table that stores contacts’ information.
You can use contact names as the primary key for that table, but how do you handle two contacts with exactly the same name? Names are unsuitable natural keys, because they are often not unique. If you use an AutoNumber field, each record is guaranteed to have a unique identifier. Note: You should not use an AutoNumber field to keep a count of the records in a table. AutoNumber values are not reused, so deleted records can result in gaps in your count.
Moreover, an accurate count of records can be easily obtained by using a Totals row in a datasheet. Determines the amount of space that is allocated for each value. For AutoNumber fields, only two values are allowed:. This is the default value. You should not change this value unless you are creating a replication ID field.
Note: Replication is not supported in databases that use a new file format, such as. This setting makes AutoNumber fields compatible with other Long Integer Number fields when they are used in relationships or joins.
Add or change a table’s primary key in Access
Overview of data types and field properties in Access, and detailed data type reference. including Memo, Date/Time, and Text. Note: When you create a new table in Datasheet view, Access automatically creates a primary key for you and assigns it the AutoNumber data type.