Introduction To MS Access
Introduction To MS Access
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. 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
12
Note: The Format list may be unavailable for some fields (for example, Text), depending
on the data type of the field.
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
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.
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.
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.
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.
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.
20