0% found this document useful (0 votes)
10 views20 pages

Introduction To MS Access

Tables are crucial in databases as they store all information, with each table dedicated to a specific subject. Proper planning and design involve creating tables first, defining their properties, data types, and establishing relationships through keys. Relationships enhance data consistency, efficiency, and comprehensibility, allowing for effective data management across multiple related tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

Introduction To MS Access

Tables are crucial in databases as they store all information, with each table dedicated to a specific subject. Proper planning and design involve creating tables first, defining their properties, data types, and establishing relationships through keys. Relationships enhance data consistency, efficiency, and comprehensibility, allowing for effective data management across multiple related tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Introduction to Tables

Tables are essential objects in a database because they hold all the information or data. For
example, a database for a business can have a Contacts table that stores the names of their
suppliers, e-mail addresses, and telephone numbers. Because other database objects depend so
heavily on tables, you should always start your design of a database by creating all of its tables
and then creating any other objects. Before you create tables in Access, consider your
requirements and determine all the tables that you might need. For an introduction to planning
and designing a database, see Database Design Basics.
 Overview
 Table and field properties
 Data Types
 Table relationships
 Keys
 Benefits of using relationships

Overview
A relational database like Access usually has several related tables. In a well-designed
database, each table stores data about a particular subject, such as employees or products. A
table has records (rows) and fields (columns). Fields have different types of data, such as text,
numbers, dates, and hyperlinks.

1. A record: Contains specific data, like information about a particular employee or a


product.
2. A field: Contains data about one aspect of the table subject, such as first name or e-mail
address.
3. A field value: Each record has a field value. For example, Contoso, Ltd.
or [email protected].

Table and field properties


Tables and fields also have properties that you can set to control their characteristics or
behavior.

1. Table properties
2. Field properties

1
In an Access database, table properties are attributes of a table that affect the appearance or
behavior of the table as a whole. Table properties are set in the table's property sheet, in Design
view. For example, you can set a table's Default View property to specify how the table is
displayed by default.

A field property applies to a particular field in a table and defines one of the field's
characteristics or an aspect of the field's behavior. You can set some field properties in
Datasheet view. You can also set any field property in Design view by using the Field
Properties pane.

Data types
Every field has a data type. A field's data type indicates the kind of data that the field stores,
such as large amounts of text or attached files.

A data type is a field property, but it differs from other field properties as follows:
 You set a field's data type in the table design grid, not in the Field Properties pane.
 A field's data type determines what other properties the field has.
 You must set a field's data type when you create the field.
You can create a new field in Access by entering data in a new column in Datasheet view.
When you create a field by entering data in Datasheet view, 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. If needed, you can change the data type
by using the Ribbon.

Examples of automatic data type detection


The following table shows how automatic data type detection works in Datasheet view.

If you enter: Access creates a field


with a data type of:
John Short Text
http://www.contoso.com Hyperlink
You can use any valid Internet protocol prefix. For example,
http://, https://, and mailto: are valid prefixes.
1 Number, Long Integer
50,000 Number, Long Integer
50,000.99 Number, Double
50000.389 Number, Double
12/67 Date/Time
The date and time formats recognized are those of your user
locale.
December 31, 2016 Date/Time
10:50:23 Date/Time
10:50 am Date/Time

2
If you enter: Access creates a field
with a data type of:
17:50 Date/Time
$12.50 Currency
The currency symbol recognized is that of your user locale.
21.75 Number, Double
123.00% Number, Double
3.46E+03 Number, Double

Table relationships
Although each table stores data about a different subject, tables in an Access database usually
store data about subjects that are related to each other. For example, a database might contain:
 A customers table that lists your company’s customers and their addresses.
 A products table that lists the products that you sell, including prices and pictures for each
item.
 An orders table that tracks customer orders.
Because you store data about different subjects in separate tables, you need some way to tie
the data together so that you can easily combine related data from those separate tables. To
connect the data stored in different tables, you create relationships. A relationship is a logical
connection between two tables that specifies fields that the tables have in common. For more
information, see Guide to table relationships.

Keys
Fields that are part of a table relationship are called keys. A key usually consists of one field,
but may consist of more than one field. There are two kinds of keys:
 Primary key: A table can have only one primary key. A primary key consists of one or
more fields that uniquely identify each record that you store in the table. Often, there is a
unique identification number, such as an ID number, a serial number, or a code, that serves
as a primary key. For example, you might have a Customers table where each customer
has a unique customer ID number. The customer ID field is the primary key of the
Customers table. When a primary key contains more than one field, it is usually composed
of pre-existing fields that, taken together, provide unique values. For example, you might
use a combination of last name, first name, and birth date as the primary key for a table
about people. For more information, see adding or changing a table’s primary key.
 Foreign key: A table can also have one or more foreign keys. A foreign key contains
values that correspond to values in the primary key of another table. For example, you
might have an Orders table in which each order has a customer ID number that corresponds
to a record in a Customers table. The customer ID field is a foreign key of the Orders table.

The correspondence of values between key fields forms the basis of a table relationship. You
use a table relationship to combine data from related tables. For example, suppose that you
have a Customers table and an Orders table. In your Customers table, each record is identified
by the primary key field, ID.

To associate each order with a customer, you add a foreign key field to the Orders table that
corresponds to the ID field of the Customers table, and then create a relationship between the
two keys. When you add a record to the Orders table, you use a value for customer ID that
comes from the Customers table. Whenever you want to view any information about an order's
3
customer, you use the relationship to identify which data from the Customers table corresponds
to which records in the Orders table.

1. A primary key, identified by the key icon next to the field name.
2. A foreign key — note the absence of the key icon.

Do not add a field if you expect that each unique entity represented in the table might require
more than value for the field. Continuing the preceding example, if you want to start tracking
orders placed by your customers, you do not add a field to the table, because each customer
will have more than one order. Instead, you create a new table to store orders, and then create
a relationship between the two tables.

Benefits of using relationships


Keeping data separated in related tables produces the following benefits:
 Consistency Because each item of data is recorded only once, in one table, there is less
opportunity for ambiguity or inconsistency. For example, you store a customer's name
only once, in a table about customers, rather than storing it repeatedly (and potentially
inconsistently) in a table that contains order data.
 Efficiency Recording data in only one place means you use less disk space. Moreover,
smaller tables tend to provide data more quickly than larger tables. Finally, if you don't
use separate tables for separate subjects, you will introduce null values (the absence of
data) and redundancy into your tables, both of which can waste space and impede
performance.
 Comprehensibility The design of a database is easier to understand if the subjects are
properly separated into tables.

Plan your tables with relationships in mind. You can use the Lookup Wizard to create a foreign
key field if the table that contains the corresponding primary key already exists. The Lookup
Wizard creates the relationship for you. For more information, see Create or delete a lookup
field.

4
Create a table and add fields
Applies To
When you create an Access database, you store your data in tables—subject-based lists that
contain rows and columns. For instance, you can create a Contacts table to store a list of names,
addresses, and telephone numbers, or a Products table to store information about products. This
article explains how to create a table, add fields to a table, set a table's primary key, and how
to set field and table properties.
Before you create tables and add fields, make sure you understand the background concepts.
In this article
Creating a table Setting a primary key Adding fields
— Create in a new database — Determine fields to use — Add by entering data
— Create in an existing — Set or change a primary — Add by using a field
database key template
— Importing or linking — Remove a primary key — Setting field properties
— — Use external data Move a field — Set in Datasheet view
— — Use a SharePoint site — — Rename a field
— — Use a Web service — — Change a data type
— Set a table's properties — — Change a format
— Save a table — — Set other properties
— Set properties in Design
view
— — Change a data type
— — Set other properties

Creating a table
A simple database, such as a contact list, might use only a single table. Many databases,
however, use several tables. When you create a new database, you create a new file on your
computer that acts as a container for all of the objects in your database, including your tables.
You can create a table by creating a new database, by inserting a table into an existing database,
or by importing or linking to a table from another data source — such as a Microsoft Excel
workbook, a Microsoft Word document, a text file, or another database. When you create a
new, blank database, a new, empty table is automatically inserted for you. You can then enter
data in the table to start defining your fields.

Create a new table in a new database


1. Click File > New, and then select Blank desktop database.
2. In the File Name box, type a file name for the new database.
3. To browse to a different location and save the database, click the folder icon.
4. Click Create.
The new database opens, and a new table named Table1 is created and opens in Datasheet
view.

Create a new table in an existing database


1. Click File > Open, and click the database if it is listed under Recent. If not, select one of
the browse options to locate the database.
2. In the Open dialog box, select the database that you want to open, and then click Open.
3. On the Create tab, in the Tables group, click Table.
A new table is inserted in the database and the table opens in Datasheet view.

5
Importing or linking to create a table
You can create a table by importing or linking to data that is stored elsewhere. You can import
or link to data in an Excel worksheet, a SharePoint list, an XML file, another Access database,
a Microsoft Outlook folder, and more.
When you import data, you create a copy of the data in a new table in the current database.
Subsequent changes to the source data will have no effect on the imported data, and changes
to the imported data do not affect the source data. After you connect to a data source and import
its data, you can then use the imported data without connecting to the source. You can change
the design of an imported table.
When you link to data, you create a linked table in the current database that represents a live
link to the existing information that is stored elsewhere. When you change data in a linked
table, you are changing it in the source. Whenever data changes in the source, that change is
shown in the linked table. You must be able to connect to the data source whenever you use a
linked table. You cannot change the design of a linked table.
Note: You cannot edit data in an Excel worksheet by using a linked table. As a workaround,
import the source data into an Access database, and then link to the database from Excel.

Create a new table by importing or linking to external data


1. Click File > Open.
2. In the Open dialog box, select and open the database in which you wish to create a new
table.
3. On the External Data tab, in the Import & Link group, select New Data Source and
then choose one of the available data sources in the submenus that appear. Note, for Access
2016 and earlier versions, the available data sources are listed as commands directly in
the Import & Link group.
4. Follow the instructions in the dialog boxes that appear at each step.
Access creates the new table and displays it in the Navigation Pane.

Use a SharePoint site to create a table


You can create a table in your database that imports from or links to a SharePoint list. You can
also create a new SharePoint list by using a predefined template. The predefined templates in
Access include Contacts, Tasks, Issues, and Events.
1. Click File > Open.
2. In the Open dialog box, select the database in which you want to create the new table, and
then click Open.
3. On the Create tab, in the Tables group, click SharePoint Lists.
4. Do one of the following:
Create a SharePoint list that is based on a template
a. Click either Contacts, Tasks, Issues, or Events.
b. In the Create New List dialog box, type the URL for the SharePoint site where you want
to create the list.
c. Enter a name for the new list and its description in the Specify a name for the new
list and Description boxes.
d. To open the linked table after it is created, select the Open the list when finished check
box (selected by default).
Create a custom list
e. Click Custom.
f. In the Create New List dialog box, type the URL for the SharePoint site where you want
to create the list.

6
g. Enter a name for the new list and its description in the Specify a name for the new
list and Description boxes.
h. To open the linked table after it is created, select the Open the list when finished check
box (selected by default).
Import the data from an existing list
i. Click Existing SharePoint List.
j. In the Get External Data dialog box, type the URL for the SharePoint site that contains
the data that you want to import.
k. Click Import the source data into a new table in the current database, and then
click Next.
l. Select the check box next to each SharePoint list that you want to import.
Link to a list
m. Click Existing SharePoint List.
n. In the Get External Data - SharePoint Site dialog box, type the URL for the SharePoint
site that contains the list to which you want to link.
o. Click Link to the data source by creating a linked table, and then click Next.
p. Select the check box next to each SharePoint list to which you want to link.

Use a Web service to create a table


You can create a table in your database that connects to data at a Web site that provides a Web
service interface. Web service tables are read-only.
1. On the External Data tab, in the Import & Link group, select New Data Source, then
select From Online Source, and then select Data Services.
Note, for Access 2016 and earlier versions, on the External Data tab, in the Import &
Link group, click More and then click Data Services.
2. If the connection you want to use has already been installed, skip to step 5. Otherwise,
continue with the next step.
3. Click Install new connection.
4. Select the connection file that you want to use, and then click OK.
5. In the Create Link to Web Service Data dialog box, expand the connection that you want
to use.
6. Select the table that you want to link to. Access displays the fields on the right side of the
dialog box.
7. Optionally, type a name for the linked table in the Specify link name box. Access will use
this name for the linked table in the Navigation Pane.
8. Click OK. Access creates the linked table.

Set a table's properties


In addition to setting properties fields, you can also set properties that apply to an entire table
or to entire records.
1. Select the table whose properties you want to set.
2. On the Home tab, in the Views group, click View, and then click Design View.
3. On the Table Design tab, in the Show/Hide group, click Property Sheet.

The table property sheet is shown.


4. On the property sheet, click the General tab.

7
5. Click the box to the left of the property that you want to set, and then enter a setting for
the property.
Don't forget to Press CTRL+S to save your changes.

Use this table To


property
Display Views On Specify whether views that are based on the table can be displayed
SharePoint Site on a SharePoint site.
Note: The effects of this setting depend on the setting of
the Display All Views On SharePoint Site database property.
Subdatasheet Expand all subdatasheets when you open the table.
Expanded
Subdatasheet Do one of the following:
Height  If you want the subdatasheet window to expand to display all
rows, leave this property set at 0".
 If you want to control the height of the subdatasheet, enter the
desired height in inches.
Orientation Set the view orientation, according to whether your language is read
left-to-right, or right-to-left.
Description Provide a description of the table. This description will appear in
tooltips for the table.
Validation Rule Enter an expression that must be true whenever you add or change
a record.
Validation Text Enter a message that is displayed when a record violates the
expression in the Validation Rule property.
Filter Define criteria to display only matching rows in Datasheet view.
Order By Select one or more fields to specify the default sort order of rows in
Datasheet view.
Subdatasheet Specify whether a subdatasheet should appear in Datasheet view,
Name and if so, which table or query should supply the rows in the
subdatasheet.
Link Child Fields List the fields in the table or query that are used for the subdatasheet
that match the Link Master Fields property that is specified for the
table.
Link Master Fields List the fields in the table that match the Link Child
Fields property that is specified for the table.
Filter On Load Automatically apply the filter criteria in the Filter property (by
setting to Yes) when the table is opened in Datasheet view.
Order By On Load Automatically apply the sort criteria in the Order By property (by
setting to Yes) when the table is opened in Datasheet view.
Tip: To provide more space to enter or edit a setting in the property box, press SHIFT+F2 to
display the Zoom box. If you are setting the Validation Rule property to an expression and

8
would like help in building it, click next to the ValidationRule property box to display the
Expression Builder.

Save a table
After you create or modify a table, you should save its design. When you save a table for the
first time, give it a name that describes the data that it contains. You can use up to 64
alphanumeric characters, including spaces. For example, you might name a table Customers,
Parts Inventory, or Products.
Access gives you lots of flexibility when it comes to naming your tables; however, there are
some restrictions to be aware of. A table name can be up to 64 characters long, can include any
combination of letters, numbers, spaces, and special characters except a period (.), exclamation
point (!), square brackets ([]), leading space, leading equal sign (=), or nonprintable character
such as a carriage return. The name also cannot contain any of the following characters:` / \ : ;
* ? " ' < > | # <TAB> { } % ~ &.
Tip: You should decide on a naming convention for the objects in your database, and use it
consistently.
1. Select File > Save, or press CTRL+S.
2. If you are saving the table for the first time, type a name for the table, and then click OK.

Setting a primary key


Unless you have a specific reason not to, you should specify a primary key for a table. Access
automatically creates an index for the primary key, which can help improve database
performance. Access also makes sure that every record has a value in the primary key field,
and that the value is always unique. 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.

Determine which fields to use as a primary key


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:
 Each record has a unique value for the field or combination of fields.
 The field or combination of fields is never empty or null — there is always a value.
 The values do not change.
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.

Set or change the primary key


1. Select the table whose primary key you want to set or change.
2. On the Home tab, in the Views group, click View, and then click Design View.
3. In the table design grid, select the field or fields that you want to use as the primary key.
To select one field, click the row selector for the field that you want.

9
To select more than one field, hold down CTRL, and then click the row selector for each
field.
4. On the Table Design tab, in the Tools group, click Primary Key.
A key indicator appears to the left of the field or fields that you specify as the primary key.

Remove the primary key


1. Select the table whose primary key you want to remove.
2. On the Home tab, in the Views group, click View, and then click Design View.
3. 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.
4. On the Table Design tab, in the Tools group, click Primary Key.
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.

Adding fields
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. If you want to start tracking each customer’s preferred means of communication,
you add a field to store that data.
You store each piece of data that you want to track in a field. For example, in a contacts table
you create fields for Last Name, First Name, Telephone Number, and Address. In a products
table you create fields for Product Name, Product ID, and Price.
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. For example, instead of a Full Name field, consider
creating separate fields for Last Name and First Name. 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.

Add a field by entering data


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.

10
To enter data in the Add New Field column:
1. 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.
2. In the Add New Field column, enter the name of the field that you want to create.
Use a descriptive name so that the field will be easier to identify.
3. Enter data in the new field.

Add a field by using a field template


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.
1. On the Home tab, in the Views group, click View, and then click Datasheet View.
2. On the Table Fields tab, in the Add & Delete group, click More Fields.

3. 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.

Setting field properties


After you create a field, you can set field properties to control its appearance and behavior.
For example, by setting field properties, you can:
 Control the appearance of data in a field
 Help prevent incorrect data entry in a field
 Specify default values for a field
 Help speed up searching and sorting on a field
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.
The properties that you can set, depend on the field’s data type.

Set field properties in Datasheet 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.
1. In the Navigation Pane, right-click the table that you want to open.
2. On the shortcut menu, click Datasheet view.

11
Rename a field
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.
1. Right-click the heading of the field that you want to rename (for example, Field1).
2. On the shortcut menu, click Rename Field.
3. Enter the new name in the field heading.
Field names can consist of up to 64 characters (letters or numbers), including spaces.

Change a field's data type


When you create a field by entering data in Datasheet view, Access examines that data to
determine the appropriate data type for the field. For example, if you enter 1/1/2024, Access
recognizes that data as a date and sets the data type for the field to Date/Time. If Access can't
definitively determine the data type, the data type is set to Short Text by default (Text if you're
using Access 2016 or earlier versions).
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
Long Text data type (Memo if you're using Access 2016 or earlier versions).
There may be cases where you want to manually change a field's data type. For example,
suppose you have room numbers that resemble dates, such as 10/2017. If you
enter 10/2017 into a new field in Datasheet view, the automatic data type detection feature
selects the Date/Time data type for the field. 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.
1. On the Ribbon, click the Table Fields tab.
2. In the Data Type list, in the Formatting group, select the data type that you want.
What data types are available?
See Data types for Access desktop databases for a complete list of available data types in
Access databases.
Tips on data types
 The maximum size of an Access database file is 2 gigabytes.
 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.

Change a field's format


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
10:50 a.m., Access sets the data type to Date/Time and the Format property to Medium Time.
To manually change a field's Format property, do the following:
1. On the Ribbon, click the Table Fields tab.
2. In the Format list, in the Formatting group, enter the format you want.

12
Note: The Format list may be unavailable for some fields (for example, Text), depending
on the data type of the field.

Set other field properties


1. In Datasheet view, click the field for which you want to set the property.
2. On the Table Fields tab, in the Properties, Formatting, or Field Validation groups,
select the properties that you want.

Set field properties in Design view


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.
1. In the Navigation Pane, right-click the table.
2. On the shortcut menu, click Design view.

Change a field's data type


You can change various aspects of a field after you create it.
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.
1. In the table design grid, locate the field for which you want to set the data type.
2. In the Data Type column, choose a data type from the list.
What data types are available?
See Data types for Access desktop databases for a complete list of available data types in
Access databases.
Tips on data types
 The maximum size of an Access database file is 2 gigabytes.
 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, 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.

Set other field properties


Note: Not all formats are available for all data types. Set the data type first, and then, if needed,
set the format.
1. 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.
The data type of the field determines the properties that you can set.
2. 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.
What field properties are available?
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.
3. To provide more space for entering or editing a property setting in the property box, press
SHIFT+F2 to display the Zoom box.
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.

13
4. To save your changes, press CTRL+S.

Move a field
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.

Introduction to queries
Using a query makes it easier to view, add, delete, or change data in your Access database.
Some other reasons for using queries:
 Find specific quickly data by filtering on specific criteria (conditions)
 Calculate or summarize data
 Automate data management tasks, such as reviewing the most current data on a recurring
basis.
 Queries help you find and work with your data
 Create a select query
 Create a parameter query
 Create a totals query
 Create a crosstab query
 Create a make table query
 Create an append query
 Create an update query
 Create a delete query

Queries help you find and work with your data


In a well-designed database, the data that you want to present through a form or report is usually
located in multiple tables. A query can pull the information from various tables and assemble
it for display in the form or report. A query can either be a request for data results from your
database or for action on the data, or for both. A query can give you an answer to a simple
question, perform calculations, combine data from different tables, add, change, or delete data
from a database. Since queries are so versatile, there are many types of queries and you would
create a type of query based on the task.
Major query types Use
Select To retrieve data from a table or make calculations.
Action Add, change, or delete data. Each task has a specific type of action query.
Create a select query
If you want to review data from only certain fields in a table, or review data from multiple
tables simultaneously or maybe just see the data based on certain criteria, a select query type
would be your choice. For more information, see create a simple select query.

Review data from select fields


For example, if your database has a table with a lot of information about products and you want
to review a list of products and their prices, here’s how you’d create a select query to return
just the product names and the respective price:

14
1. Open the database and on the Create tab, click Query Design.
2. On the Tables tab, double-click the Products table.
3. In the Products table, let’s say that you have Product Name and List Price fields. Double-
click the Product Name and List Price to add these fields to the query design grid.
4. On the Query Design tab, click Run. The query runs, and displays a list of products and
their prices.

Review data from multiple related tables simultaneously


For example, if you have a database for a store that sells food items and you want to review
orders for customers who live in a particular city. Say that the data about orders and data about
customers are stored in two tables named Customers and Orders respectively. If each table has
a Customer ID field, which forms the basis of a one-to-many relationship between the two
tables. You can create a query that returns orders for customers in a particular city, for example,
Las Vegas, by using the following procedure:
1. Open the database. On the Create tab, in the Query group, click Query Design.
2. On the Tables tab, double-click Customers and Orders.
Note the line (called a join) that connects the ID field in the Customers table and the
Customer ID field in the Orders table. This line shows the relationship between the two
tables.
3. In the Customers table, double-click Company and City to add these fields to the query
design grid.
4. In the query design grid, in the City column, clear the check box in the Show row.
5. In the Criteria row of the City column, type Las Vegas.
Clearing the Show check box prevents the query from displaying the city in its results, and
typing Las Vegas in the Criteria row specifies that you want to see only records where
the value of the City field is Las Vegas. In this case, the query returns only the customers
that are located in Las Vegas. You don’t need to display a field to use it with a criterion.
6. In the Orders table, double-click Order ID and Order Date to add these fields to the next
two columns of the query design grid.
7. On the Query Design tab, in the Results group, click Run. The query runs, and then
displays a list of orders for customers in Las Vegas.
8. Press CTRL+S to save the query.

Create a parameter query


If you frequently want to run variations of a particular query, consider using a parameter
query. When you run a parameter query, the query prompts you for field values, and then
uses the values that you supply to create criteria for your query.
Continuing from the previous example where you learnt to create a select query that returns
orders for customers located in Las Vegas, you can modify the select query to prompt you to
specify the city each time that you run the query. To follow along, open the database that you
created in the previous example:
1. In the Navigation Pane, right-click the query named Orders by City (that you created in
the previous section), and then click Design View on the shortcut menu.
2. In the query design grid, in the Criteria row of the City column, delete Las Vegas, and
then type [For what city?].
The string [For what city?] is your parameter prompt. The square brackets indicate that
you want the query to ask for input, and the text (in this case, For what city?) is the
question that the parameter prompt displays.
Note: Neither a period (.) nor an exclamation point (!) can be used as text in a parameter
prompt.

15
3. Select the check box in the Show row of the City column, so that the query results will
display the city.
4. On the Query Design tab, in the Results group, click Run. The query prompts you to
enter a value for City.
5. Type New York, and then press ENTER to see orders for customers in New York.
What if you don't know what values you can specify? You can use wildcard characters
as part of the prompt:
6. On the Home tab, in the Views group, click View, and then click Design View.
7. In the query design grid, in the Criteria row of the City column, type Like [For what
city?]&"*".
In this parameter prompt, the Like keyword, the ampersand (&), and the asterisk (*)
enclosed in quotation marks allow the user to type a combination of characters, including
wildcard characters, to return a variety of results. For example, if the user types *, the
query returns all cities; if the user types L, the query returns all cities that start with the
letter "L;" and if the user types *s*, the query returns all cities that contain the letter "s."
8. On the Query Design tab, in the Results group, click Run, and at the query prompt,
type New, and press ENTER.
The query runs, and then displays orders for customers in New York.
Specify parameter data types
You can also specify what type of data a parameter should accept. You can set the data type
for any parameter, but it is especially important to set the data type for numeric, currency, or
date/time data. When you specify the data type that a parameter should accept, users see a
more helpful error message if they enter the wrong type of data, such as entering text when
currency is expected.
If a parameter is set to accept text data, any input is interpreted as text, and no error message
is displayed.
To specify the data type for parameters in a query, use the following procedure:
1. With the query open in Design view, on the Query Design tab, in the Show/Hide group,
click Parameters.
2. In the Query Parameters dialog box, in the Parameter column, type the prompt for
each parameter for which you want to specify the data type. Make sure that each
parameter matches the prompt that you use in the Criteria row of the query design grid.
3. In the Data Type column, select the data type for each parameter.
For more information, see using parameters to ask for input when running a query.
Top of Page
Create a totals query
The Total row in a datasheet is very useful, but for more complex questions, you use a totals
query. A totals query is a select query that allows you to group and summarize data, like when
you want to see total sales per product. In a totals query, you can use the Sum function (an
aggregate function), to see total sales per product.
Use the following procedure to modify the Product Subtotals query that you created in the
previous example so that it summarizes product subtotals by product.
1. On the Home tab, click View > Design View.
The Product Subtotals query opens in Design view.
2. On the Query Design tab, in the Show/Hide group, click Totals.
The Totals row is displayed in the query design grid.
Note: Although they have similar names, the Totals row in the design grid and the Total row
in a datasheet are not the same:
 You can group by field values by using the Totals row in the design grid.
 You can add a datasheet Total row to the results of a totals query.

16
 When you use the Totals row in the design grid, you must choose an aggregate function
for each field. If you do not want to perform a calculation on a field, you can group by the
field.
 In the second column of the design grid, in the Total row, select Sum from the drop-down
list.
 On the Query Design tab, in the Results group, click Run. The query runs, and then
displays a list of products with subtotals.
 Press CTRL+S to save the query. Leave the query open.
For more information, see Display column totals in a datasheet using a Totals row.

Make calculations based on your data


You usually would not use tables to store calculated values, like subtotals, even if they are
based on data in the same database, because calculated values can become outdated if the values
that they are based on changes. For example, you would not store someone's age in a table,
because every year you would have to update the value; instead, you store the person's date of
birth, and then use a query to calculate the person's age.
For example if you have a database for some products you’d like to sell. This database has a
table called Orders Details that has information about the products in fields such as, price of
each product and the quantities. You can calculate the subtotal by using a query that multiplies
the quantity of each product by the unit price for that product, multiplies the quantity of each
product by the unit price and discount for that product, and then subtracts the total discount
from the total unit price. If you created the sample database in the previous example, open it
and follow along:
1. On the Create tab, click Query Design.
2. On the Tables tab, double-click Order Details.
3. In the Order Details table, double-click Product ID to add this field to the first column of
the query design grid.
4. In the second column of the grid, right-click the Field row, and then click Zoom on the
shortcut menu.
5. In the Zoom box, type or paste the following: Subtotal: ([Quantity]*[Unit Price])-
([Quantity]*[Unit Price]*[Discount])
6. Click OK.
7. On the Query Design tab, click Run. The query runs, and then displays a list of products
and subtotals, per order.
8. Press CTRL+S to save the query, and then name the query Product Subtotals.
For more information, see Display column totals in a datasheet using a Totals row.

Display summarized or aggregate data


When you use tables to record transactions or store regularly occurring numeric data, it is useful
to be able to review that data in aggregate, such as sums or averages. In Access, you can add a
Totals row to a datasheet. Total row is a row at the bottom of the datasheet that can display a
running total or other aggregate value.
1. Run the Product Subtotals query you created earlier, and leave the results open in
Datasheet view.
2. On the Home tab, click Totals. A new row appears at the bottom of the datasheet, with
the word Total in the first column.
3. Click the cell in the last row of the datasheet named Total.
4. Click the arrow to view the available aggregate functions. Because the column contains
text data, there are only two choices: None and Count.
5. Select Count. The content of the cell changes from Total to a count of the column values.

17
6. Click the adjoining cell (the second column). Note that an arrow appears in the cell.
7. Click the arrow, and then click Sum. The field displays a sum of the column values.
8. Leave the query open in Datasheet view.

Create a crosstab query


Now suppose that you want to review product subtotals, but you also want to aggregate by
month, so that each row shows subtotals for a product, and each column shows product
subtotals for a month. To show subtotals for a product and to show product subtotals for a
month, use a crosstab query.
You can modify the Product Subtotals query again so that the query returns rows of product
subtotals and columns of monthly subtotals.
1. On the Home tab, in the Views group, click View, and then click Design View.
2. In the Query Setup group, click Add Tables.
3. Double-click Orders, and then click Close.
4. On the Query Design tab, in the Query Type group, click Crosstab. In the design grid,
the Show row is hidden, and the Crosstab row is displayed.
5. In the third column of the design grid, right-click the Field row, and then click Zoom on
the shortcut menu. The Zoom box opens.
6. In the Zoom box, type or paste the following: Month: "Month " & DatePart("m",
[Order Date])
7. Click OK.
8. In the Crosstab row, select the following values from the drop-down list: Row
Heading for the first column, Value for the second column, and Column Heading for the
third column.
9. On the Query Design tab, in the Results group, click Run. The query runs, and then
displays product subtotals, aggregated by month.
10. Press CTRL+S to save the query.
For more information about crosstab queries, see Make summary data easier to read by
using a crosstab query.

Create a make table query


You can use a make-table query to create a new table from data that is stored in other tables.
For example, suppose that you want to send data for Chicago orders to a Chicago business
partner who uses Access to prepare reports. Instead of sending all your order data, you want to
restrict the data that you send to data specific to Chicago orders.
You can build a select query that contains Chicago order data, and then use the select query to
create the new table by using the following procedure:
1. Open the example database from the previous example.
To run a make-table query, you may need to enable the database content.
Note: If you see a message beneath the Ribbon about enabling the database, click Enable
content.If your database is already in a trusted location, you will not see the Message Bar.
2. On the Create tab, in the Query group, click Query Design.
3. Double-click Order Details and Orders.
4. In the Orders table, double-click Customer ID and Ship City to add these fields to the
design grid.
5. In the Order Details table, double-click Order ID, Product ID, Quantity, Unit Price,
and Discount to add these fields to the design grid.
6. In the Ship City column of the design grid, clear the box in the Show row. In
the Criteria row, type 'Chicago' (include the single quotation marks). Verify the query
results before you use them to create the table.

18
7. On the Query Design tab, in the Results group, click Run.
8. Press Ctrl + S to save the query.
9. In the Query Name box, type Chicago Orders Query, and then click OK.
10. On the Home tab, in the Views group, click View, and then click Design View.
11. On the Query Design tab, in the Query Type group, click Make Table.
12. In the Make Table dialog box, in the Table Name box, type Chicago Orders, and then
click OK.
13. On the Query Design tab, in the Results group, click Run.
14. In the confirmation dialog box, click Yes, and see the new table displayed in the
Navigation Pane.
Note: If there is already a table with the same name that you specified, Access deletes that
table before running the query.
For more information about using make table queries, see Create a make table query.

Create an append query


You can use an append query to retrieve data from one or more tables and add that data to
another table.
For example, suppose that you created a table to share with a Chicago business associate, but
you realize that the associate also works with clients in the Milwaukee area. You want to add
rows that contain Milwaukee area data to the table before you share the table with your
associate. You can add Milwaukee area data to the Chicago Orders table by using the following
procedure:
1. Open the query named "Chicago Orders Query" you created earlier in Design view.
2. On the Query Design tab, in the Query Type group, click Append. The Append dialog
box opens.
3. In the Append dialog box, click the arrow in the Table Name box, select Chicago
Orders from the drop-down list, and then click OK.
4. In the design grid, in the Criteria row of the Ship City column, delete 'Chicago', and then
type 'Milwaukee'.
5. In the Append To row, select the appropriate field for each column.
In this example, the Append To row values should match the Field row values, but that
is not required for append queries to work.
6. On the Query Design tab, in the Results group, click Run.
Note: While running a query that returns a large amount of data you might get an error message
indicating that you will not be able to undo the query. Try increasing the limit on the memory
segment to 3MB to allow the query to go through.
For more information about append queries, see Add records to a table by using an append
query.

Create an update query


You can use an update query to change the data in your tables, and you can use an update query
to enter criteria to specify which rows should be updated. An update query provides you an
opportunity to review the updated data before you perform the update.
Important: An action query cannot be undone. You should consider making a backup of any
tables that you will update by using an update query.
In the previous example, you appended rows to the Chicago Orders table. In the Chicago Orders
table, the Product ID field shows the numeric Product ID. To make the data more useful in
reports, you can replace the product IDs with product names, use the following procedure:
1. Open the Chicago Orders table in Design view.
2. In the Product ID row, change the Data Type from Number to Text.

19
3. Save and close the Chicago Orders table.
4. On the Create tab, in the Query group, click Query Design.
5. Double-click Chicago Orders and Products.
6. On the Query Design tab, in the Query Type group, click Update.
7. In the design grid, the Sort and Show rows disappear, and the Update To row appears.
8. In the Chicago Orders table, double-click Product ID to add this field to the design grid.
9. In the design grid, in the Update To row of the Product ID column, type or paste the
following: [Products].[Product Name]
Tip: You can use an update query to delete field values by using an empty string ("") or
NULL in the Update To row.
10. In the Criteria row, type or paste the following: [Product ID] Like ([Products].[ID])
11. You can review which values will be changed by an update query by viewing the query in
Datasheet view.
12. On the Design tab, click View > Datasheet View. The query returns a list of Product IDs
that will be updated.
13. On the Query Design tab, click Run.
When you open the Chicago Orders table, you will see that the numeric values in the
Product ID field have been replaced by the product names from the Products table.
For more information about update queries, see Create and run an update query.

Create a delete query


You can use a delete query to delete data from your tables, and you can use a delete query to
enter criteria to specify which rows should be deleted. A delete query provides you an
opportunity to review the rows that will be deleted before you perform the deletion.
For example, say that while you were preparing to send the Chicago Orders table from the
previous example, to your Chicago business associate, you notice that some of the rows contain
a number of empty fields. You decided to remove these rows before you send the table. You
could just open the table and delete the rows manually, but if you have many rows to delete
and you have clear criteria for which rows should be deleted, you might find it helpful to use a
delete query.
You can use a query to delete rows in the Chicago Orders table that do not have a value for
Order ID by using the following procedure:
1. On the Create tab, click Query Design.
2. Double-click Chicago Orders.
3. On the Query Design tab, in the Query Type group, click Delete. In the design grid,
the Sort and Show rows disappear, and the Delete row appears.
4. In the Chicago Orders table, double-click Order ID to add it to the grid.
5. In the design grid, in the Criteria row of the Order ID column, type Is Null.
6. On the Query Design tab, in the Results group, click Run.
For more information about delete queries, see Create and run a delete query.

20

You might also like