Databases For A Purpose 03
Databases For A Purpose 03
Yann Vautrin
Running queries and reports
• Designing a simple query
• Designing a multi-table query
• More query design options
• Creating reports
Yann Vautrin
Designing a Simple Query
The real power of a relational database lies in its ability to quickly retrieve
and analyze your data by running a query. Queries allow you to pull
information from one or more tables based on a set of search conditions you
define. In this lesson, you will learn how to create a simple one-table query.
Yann Vautrin
What are queries?
Queries are a way of searching for and compiling data from one or more
tables. Running a query is like asking a detailed question of your database.
When you build a query in Access, you are defining specific search conditions
to find exactly the data you want.
Yann Vautrin
How are queries used?
• Queries are far more powerful than the simple searches or filters you
might use to find data within a table. This is because queries can draw
their information from multiple tables. For example, while you could use a
search in the Customers table to find the name of one customer at your
business or a filter on the Orders table to view only orders placed within
the past week, neither would let you view both customers and orders at
once. However, you could easily run a query to find the name and phone
number of every customer who's made a purchase within the past week.
A well-designed query can give information you might not be able to find
out just by examining the data in your tables.
• When you run a query, the results are presented to you in a table, but
when you design one you use a different view. This is called Query Design
view, and it lets you see how your query is put together.
Yann Vautrin
Query design view
Yann Vautrin
Query design view
Yann Vautrin
Query design view
Yann Vautrin
Query design view
Yann Vautrin
Query design view
Yann Vautrin
Query design view
Yann Vautrin
Query design view
Yann Vautrin
Query design view
Yann Vautrin
One-table queries
Let's familiarize ourselves with the query-building process by
building the simplest query possible: a one-table query.
If you think this sounds a little like applying a filter, you're right. A
one-table query is actually just an advanced filter applied to a
table.
Yann Vautrin
To create a simple one-table query:
Select the Create tab on the Ribbon and locate the Queries
group.
Yann Vautrin
To create a simple one-table query:
Access will switch to Query Design view. In the Show Table
dialog box that appears, select the table you want to run a query
on. We are running a query on our customers, so we'll select the
Customers table.
Yann Vautrin
To create a simple one-table query:
Click Add, then click Close.
Yann Vautrin
To create a simple one-table query:
Set the search criteria by clicking the cell in the Criteria: row of
each field you want to filter. Typing criteria into more than one
field in the Criteria: row will set your query to include only results
that meet all criteria. If you want to set multiple criteria but don't
need the records shown in your results to meet all of them, type
the first criteria in the Criteria: row and additional criteria in the
or: row and the rows beneath it. Because we want to find
customers who either live in Raleigh or in the 27513 zip code, we'll
type "Raleigh" in the City field and "27513" into the or: row of the
Zip Code field. The quotation marks will search these fields for an
exact match.
Yann Vautrin
To create a simple one-table query:
After you have set your criteria, run the query by clicking the Run
command on the Design tab.
Yann Vautrin
To create a simple one-table query:
The query results will be displayed in the query's Datasheet view,
which looks like a table. If you want, save your query by clicking
the Save command in the Quick Access Toolbar. When prompted
to name it, type the desired name, then click OK.
Yann Vautrin
Designing a Multi-table Query
In the previous lesson, you learned how to create a simple query
with one table. Most queries you design in Access will likely use
multiple tables, allowing you to answer more complex questions.
In this lesson, you'll learn how to design and create a multi-table
query.
Yann Vautrin
Designing a multi-table query
Queries can be difficult to understand and build if you don't have
a good idea of what you're trying to find and how to find it. A
one-table query can be simple enough to make up as you go
along, but to build anything more powerful you'll need to plan
the query in advance.
Yann Vautrin
Planning a query
When planning a query that uses more than one table, follow
these four steps:
• Pinpoint exactly what you want to know. If you could ask your
database any question, what would it be? Building a query is
more complicated than just asking a question, but knowing
precisely what question you want to answer is essential to
building a useful query.
Yann Vautrin
Step 1: Pinpointing the question we
want to ask
Our bakery database contains many customers, some of whom
have never placed an order but who are in our database because
they signed up for our mailing list. Most of them live within the
city limits, but others live out of town or even out of state. We
want to get our out-of-town customers who've placed orders in
the past to come back and give us another try, so we're going to
mail them some coupons. We don't actually want our list to
include customers who live too far away; sending a coupon to
someone who doesn't live in our area probably won't make that
person come in. So we just want to find people who don't live in
our city but who still live in our area.
Yann Vautrin
Step 1: Pinpointing the question we
want to ask
In short, the question we want our query to answer is this: Which
customers live in our area, are outside the city limits, and have
placed an order at our bakery?
Yann Vautrin
Step 2: Identifying the information
we need
What information might we want to see in a list about these
customers? Obviously, we'll need the customers' names and their
contact information: their addresses, phone numbers, and email
addresses. But how are we going to know if they've placed
orders? Each record of an order identifies the customer who
placed that order. If we include the order ID numbers, we should
be able to narrow our list down to only customers who have
previously placed orders.
Yann Vautrin
Step 3: Locating the tables
containing the information we need
In order to write a query, you need to be familiar with the
different tables in your database. From working extensively with
our own database, we know that the customer information we
need is located in fields in the Customers table. Our Order ID
numbers are in a field in the Orders table. We only need to
include these two tables to find all of the information we need.
Yann Vautrin
Step 4: Determining the criteria our
query should search for
When you set criteria for a field in a query, you are basically
applying a filter to it that tells the query to retrieve only
information that matches your criteria. Review the list of fields
we are including in this query. How and where can we set criteria
that will best help us answer our question?
We won't set a criteria for the order ID field or any other fields
because we want to see all of the orders made by people who
meet the two criteria we just set.
Yann Vautrin
Step 4: Determining the criteria our
query should search for
Yann Vautrin
Joining tables in queries
The final thing you need to consider when designing a query is
the way you link, or join, the tables you're working with. When
you add two tables to an Access query, this is what you'll see in
the Object Relationship pane:
Yann Vautrin
Joining tables in queries
• The line connecting the two tables is called the join line. See
how the join line is actually an arrow? This is because it
indicates the order in which the query looks at data from the
two tables. In the image above, the arrow is pointing from left
to right, which means the query will look at data in the left
table first, then look at only the data in the right table that
relates to the records it's already seen in the left table.
Yann Vautrin
Joining tables in queries
Yann Vautrin
Joining tables in queries
What do you notice when you look at these lists? First of all, every
single order in the Orders table is linked to someone in the
Customers table—the customer who placed that order.
However, when you look at the Customers table, you'll see that
the customers who've placed multiple orders are linked to more
than one order, and those who've never placed an order are
linked to no orders. As you can see, even when two tables are
linked it's possible to have records in one table that have no
relationship to any record in the other table.
Yann Vautrin
Joining tables in queries
So what happens when Access tries to run our query with the
current join, left to right? It pulls every record from the table to
the left: our Customers table.
Yann Vautrin
Joining tables in queries
It then retrieves every record from the right table that has a
relationship with a record Access has already taken from the left
table.
Yann Vautrin
Joining tables in queries
Because our join began with the Customers table, our query will
include records for all of our customers, including those who've
never placed orders. This is more information than we need. We
only want to see records for customers who have placed orders.
Fortunately, we can fix this problem by changing the direction of
the join line. If we join the tables from right to left instead,
Access will first retrieve the orders from the right table, our
Orders table:
Yann Vautrin
Joining tables in queries
Then Access will look at the left table and retrieve only the
records of customers who are linked to an order on the right.
Yann Vautrin
Joining tables in queries
We now have exactly the information we want: all of the
customers who have placed an order, and only those customers.
As you can see, we had to join our tables in the correct direction
to obtain the information we wanted.
Yann Vautrin
Creating a multi-table query
Now that we've planned our query, we're ready to design and run
it. If you have created written plans for your query, be sure to
reference them often throughout the query design process.
Yann Vautrin
To create a multi-table query:
Select the Query Design command from the Create tab on the
Ribbon.
Yann Vautrin
To create a multi-table query:
In the dialog box that appears, select each table you want to
include in your query and click Add. You can press and hold the
Ctrl key on your keyboard to select more than one table. When
we planned our query, we decided we needed information from
the Customers and Orders tables, so we'll add these.
Yann Vautrin
To create a multi-table query:
After you have added all of the tables you want, click Close.
Yann Vautrin
To create a multi-table query:
The Join Properties dialog box will appear. Select an option to
choose the direction of your join. In our example, we'll choose
option 3 because we want a right-to-left join.
Yann Vautrin
To create a multi-table query:
In the table windows, double-click the field names you want to
include in your query. They will be added to the design grid in the
bottom part of the screen. In our example, we'll include most of
the fields from the Customers table: First Name, Last Name,
Street Address, City, State, Zip Code, and Phone Number. We'll
also include the ID number from the Orders table.
Yann Vautrin
To create a multi-table query:
Set field criteria by entering the desired criteria in the criteria row
of each field. We want to set two criteria: Not in ("Raleigh") in
the City field, and Like ("919*") in the Phone Number field. This
will find customers who do not live in Raleigh but who do live in
the 919 area code.
Yann Vautrin
To create a multi-table query:
After you have set your criteria, run the query by clicking the Run
command on the Design tab.
Yann Vautrin
To create a multi-table query:
The query results will be displayed in the query's Datasheet view,
which looks like a table. If you want, save your query by clicking
the Save command on the Quick Access Toolbar. When prompted
to name it, type the desired name, then click OK.
Yann Vautrin
More Query Design Options
• Access offers several options that let you design and run
queries that return exactly the information you're looking for.
For instance, what if you need to find out how many of
something exists within your database? Or what if you would
like your query results to automatically be sorted a certain
way? If you know how to use query options in Access, you can
design almost any query you want.
• In this lesson, you'll learn how to modify and sort your queries
within Query Design view, as well as how to use the Totals
function to create a query that can perform calculations with
your data. You'll also learn about additional query-building
options offered in Access.
Yann Vautrin
Modifying queries
• Access offers several options for making your queries work
better for you. In addition to modifying your query criteria
and joins after you build them, you can choose to sort and
hide fields in your query results.
Yann Vautrin
To modify your query:
When you open an existing query in Access, it is displayed in
Datasheet view, meaning you will see your query results in a
table. To modify your query, you must enter Design view, the
view you used when creating it. There are two ways to switch to
Design view:
•On the Home tab of the Ribbon, click the View command. Select
Design View from the drop-down menu that appears.
Yann Vautrin
To modify your query:
When you open an existing query in Access, it is displayed in
Datasheet view, meaning you will see your query results in a
table. To modify your query, you must enter Design view, the
view you used when creating it. There are two ways to switch to
Design view:
•On the Home tab of the Ribbon, click the View command. Select
Design View from the drop-down menu that appears.
Yann Vautrin
To modify your query:
In the bottom-right corner of your Access window, locate the
small view icons. Click the Design View icon, which is the icon
farthest to the right.
Once in Design view, make the desired changes, then select the
Run command to view your updated results.
Yann Vautrin
Sorting queries
• Access allows you to apply multiple sorts simultaneously while
designing your query. This allows you to view your data
exactly the way you want.
Yann Vautrin
Sorting queries
• When more than one sort is included in a query, Access reads
the sorts from left to right. This means the leftmost sort will
be applied first. In the example below, customers will be
sorted first by the City where they live and then by the Zip
Code within that city.
Yann Vautrin
To apply a multilevel sort:
• Open the query and switch to Design view.
• Locate the field you want to sort first. In the Sort: row, click
the drop-down arrow to select either an Ascending or
Descending sort.
Yann Vautrin
To apply a multilevel sort:
• Repeat the process in the other fields to add additional sorts.
Remember, the sorts are applied from left to right, so any
additional sorts must be applied to fields located to the right
of your primary sort. If necessary, you can rearrange the fields
by clicking the top of a field and dragging it to a new location.
Yann Vautrin
To apply a multilevel sort:
• To apply the sort, click the Run command.
Yann Vautrin
To apply a multilevel sort:
• Your query results will appear with the desired sort.
Yann Vautrin
Hiding fields within queries
• Sometimes you might have fields that contain important
criteria, but you might not need to actually see the
information from that field in the final results. For example,
take one of the queries we built in our last lesson: a query to
find the names and contact information of customers who
had placed orders. We included order ID numbers in our query
because we wanted to make sure we only pulled customers
who had placed orders.
Yann Vautrin
To hide a field within a query:
• Open the query and switch to Design view.
Yann Vautrin
More types of queries
• By this point, you should understand how to create a simple
one-table or multi-table query using multiple criteria.
Additional queries offer you the ability to perform even more
complex actions with your database. One of these is the totals
query, which lets you perform calculations with your data.
Yann Vautrin
Totals queries
• Sometimes setting simple criteria won't give you the results
you need, especially when you're working with numerical
values. You may want to see your query results grouped or
counted in some way. For example, let's say we want to find
out how many of each menu item at our bakery has been
ordered—how many Almond Croissants, Apple Pies, and so
on. To do this, we could create a totals query to find the sum
of the quantities for each item.
• First, the totals query will group all similar menu items from
separate orders (for example, Almond Croissants). Then, the
Sum function will add the values in the Quantity field to
calculate the total number sold for that item.
Yann Vautrin
Totals queries
Yann Vautrin
Totals queries
The Sum function helped us find the desired information in this
example, but in other situations you may need to use a different
function to find the answer you need. There are several functions
you can choose from:
Yann Vautrin
To create a totals query:
For our example, we want to find the total number we've sold of
each of our menu items, so we'll use a query showing us all of the
menu items we've sold. If you want to follow along in our
database, open the Menu Items Ordered query.
• From the Design tab, locate the Show/Hide group, then select
the Totals command.
Yann Vautrin
To create a totals query:
A row will be added to the table in the design grid, with all values
in that row set to Group By. Select the cell in the Total: row of the
field you want to perform a calculation on, then click the drop-
down arrow that appears.
Yann Vautrin
To create a totals query:
Select the calculation you want to be performed in that field. In
our example, we want to add the quantities of products we've
sold, so we'll select the Sum option.
Yann Vautrin
To create a totals query:
When you are satisfied with your query design, select the Run
command on the Query Tools Design tab to run the query.
Yann Vautrin
To create a totals query:
The query results will be displayed in the query's Datasheet view,
which looks like a table. If you want, save your query by clicking
the Save command on the Quick Access Toolbar.
Yann Vautrin
Creating Reports
If you need to share information from your database with
someone but don't want that person actually working with your
database, consider creating a report. Reports allow you to
organize and present your data in a reader-friendly, visually
appealing format. Access makes it easy to create and customize a
report using data from any query or table in your database.
In this lesson, you will learn how to create, modify, and print
reports.
Yann Vautrin
To create a report:
Reports give you the ability to present components of your
database in an easy-to-read, printable format. Access lets you
create reports from both tables and queries.
Yann Vautrin
To create a report:
Select the Create tab on the Ribbon. Locate the Reports group,
then click the Report command.
Yann Vautrin
To create a report:
Access will create a new report based on your object.
It's likely that some of your data will be located on the other side
of the page break. To fix this, resize your fields. Simply select a
field, then click and drag its edge until the field is the desired size.
Repeat with additional fields until all of your fields fit.
Yann Vautrin
To create a report:
To save your report, click the Save command on the Quick Access
Toolbar. When prompted, type a name for your report, then click
OK.
Yann Vautrin
Deleting fields
You might find that your report contains some fields you don't
really need to view. For instance, our report contains the Zip
Code field, which isn't necessary in a list of orders. Fortunately,
you can delete fields in reports without affecting the table or
query where you grabbed your data.
Yann Vautrin
To delete a field in a report:
Click any cell in the field you want to delete, then press the Delete
key on your keyboard.
Yann Vautrin
Printing and saving reports in Print
Preview
While you can print reports using commands in Backstage view,
you can also use Print Preview. Print Preview shows you how
your report will appear on the printed page. It also allows you to
modify the way your report is displayed, print it, and even save it
as a different file type.
Yann Vautrin
To print a report:
From the Home tab, click the View command, then select Print
Preview from the drop-down list. Your report will be shown as it
will appear on the printed page.
Yann Vautrin
To print a report:
If necessary, modify the page size, margin width, and page
orientation using the related commands on the Ribbon.
Yann Vautrin
To print a report:
Click the Print command.
Yann Vautrin
Saving reports
You can save reports in other formats so they'll be viewable
outside of Access. This is called exporting a file, and it allows you
to view and even modify reports in other formats and programs.
Access offers options to save your report as an Excel file, text
file, PDF, and HTML document, among other file types.
Experiment with the different export options to find the one that
best suits your needs.
Yann Vautrin
To export a report:
• From the Home tab, click the View command, then select
Print Preview from the drop-down list.
Yann Vautrin
To export a report:
• A dialog box will appear. Select the location where you want
to save the report.
Yann Vautrin
To export a report:
• Some export options will cause the Export Wizard to appear.
Simply follow the instructions to export your report.
Yann Vautrin
Advanced Report Options
Access offers several advanced options for creating and
modifying reports. The Report Wizard is a tool that guides you
through the process of creating complex reports. Once you've
created a report—whether through the Report Wizard or the
Report command—you can then format it to make it look exactly
how you want.
In this lesson, you'll learn how to use the Report Wizard to create
complex reports. You'll also learn how to use formatting options
to format text, change report colors and fonts, and add a logo.
Yann Vautrin
The Report Wizard
While using the Report command is a quick way to create reports
from the current object, it's not as helpful if you want to create a
report with data from multiple objects. The Report Wizard makes
it easy to create reports using fields from multiple tables and
queries. It even lets you choose how your data will be organized.
Yann Vautrin
To create a report with the Report
Wizard:
Select the Create tab and locate the Reports group. Click the
Report Wizard command.
Yann Vautrin
To create a report with the Report
Wizard:
The Report Wizard will appear. In the procedures below, we'll
discuss the different pages in the Report Wizard.
Yann Vautrin
Step 1: Select the fields to include
in your report
Click the drop-down arrow to select the table or query that
contains the desired field(s).
Yann Vautrin
Step 1: Select the fields to include
in your report
Select a field from the list on the left, and click the right arrow to
add it to the report.
Yann Vautrin
Step 1: Select the fields to include
in your report
You can add fields from more than one table or query by
repeating the above steps. Once you've added the desired fields,
click Next.
Yann Vautrin
Step 2: Organize the report
• The Report Wizard will provide you with options that let you
choose how to view and organize your data. These options
group similar data within your fields and organize these fields
into multiple levels, like in an outline or bulleted list.
• If you are building a report from only one table or query, you
can skip to Step 3 below.
Yann Vautrin
Step 2: Organize the report
• Access will offer a list of several organization options. Select
an option from the list to preview it.
Yann Vautrin
Step 2: Organize the report
• Click Next when you are satisfied with the basic organization
of your data.
• If you're not satisfied with the way your data is organized, you
can now modify the grouping levels. Select a field from the
list, then click the right arrow to add it as a new level.
Yann Vautrin
Step 2: Organize the report
• If necessary, modify the order of your grouped fields by
selecting a field and clicking the up or down Priority arrow to
move it up or down a level.
Yann Vautrin
Step 3: Sort your report data
• Click the top drop-down arrow, then select the name of the
first field you want to sort.
Yann Vautrin
Step 3: Sort your report data
• Add any additional sorts. You can sort up to four fields. The
sort will be applied from top to bottom, meaning the sort at
the top of the list will be the main sort.
• When you are satisfied with the way your data is sorted, click
Next.
Yann Vautrin
Step 4: Select a layout and title
• Click the various layout options to see how they look, then
select one to use in your report.
Yann Vautrin
Step 4: Select a layout and title
• Select either a portrait (tall) or landscape (wide) orientation
for your report.
Yann Vautrin
Step 4: Select a layout and title
• Once you are satisfied with your report layout, click Next.
• Select the text box, then type the title you want for your
report.
Yann Vautrin
Formatting reports
• One of the strengths of reports is that you can modify their
appearance to make them look how you want. You can add
headers and footers, apply new colors, and even add a logo.
All of these things can help you create visually appealing
reports.
Yann Vautrin
Modifying report text
• The bulk of the information in your report comes straight
from the query or table you built it from, which means you
can't edit it within the report. However, you can change, add,
or delete label text, headers, and footers to make your report
clearer and easier to read. For example, in our report we
decided we didn't need the field headings to understand our
data, so we simply deleted them.
Yann Vautrin
Modifying the page header and footer
To view and modify the header and footer that appear on each
page of your report, select the View command on the Ribbon and
switch to Design view. The header and footer are located in the
white space beneath the Page Header and Page Footer bars.
Yann Vautrin
Modifying the page header and footer
Depending on your report's design, sometimes you may find that
there is no white space in the page header and footer, as in the
image above. If this is the case, you must resize the header and
footer before you can add anything to them. Simply click and
drag the bottom border of the header or footer to make it larger.
Yann Vautrin
To add text to a header or footer:
Select the Design tab, locate the Controls group, then click the
Label command.
Yann Vautrin
To add text to a header or footer:
Click and drag the mouse inside the white area to create your
label. Release the mouse when it is the desired size.
Yann Vautrin
To add text to a header or footer:
Click the text box, then type the desired text.
Yann Vautrin
To add the date and time to a header
or footer:
Select the Design tab, locate the Header/Footer group, then click
the Date and Time command.
Yann Vautrin
To add the date and time to a header
or footer:
A dialog box will appear. Select the desired formatting options. A
preview of the text that will be included in your report will
appear.
When you are satisfied with the appearance of the date and time,
click OK.
Yann Vautrin
To add page numbers to a header or
footer:
Select the Design tab, then locate the Header/Footer group.
Yann Vautrin
To add page numbers to a header or
footer:
• The Page Numbers dialog box will appear. Under Format,
choose Page N to display the number of only the current
page, or Page N of M to display the number of the current
page and the number of total pages.
Yann Vautrin
To add a logo:
• From the Design tab, click the View command, then select
Layout View from the drop-down list.
Yann Vautrin
To add a logo:
• Locate the Header/Footer group, then click the Logo
command.
Yann Vautrin
To add a logo:
• A dialog box will appear. Locate and select the desired file,
then click OK to add it to your report.
Yann Vautrin
To add a logo:
• A small version of the image will appear in the header. Click
and drag the image border to resize it.
Yann Vautrin
To add a logo:
• If necessary, move your logo to the desired location by
clicking and dragging it.
Yann Vautrin
Themes and fonts
• A theme is a set of colors and fonts that applies to the entire
database to give it a consistent, professional look. By default,
databases use the Office theme. When you change the theme,
all of the theme fonts and colors in your database change to
match the new theme. Designing and modifying reports using
theme elements can help you keep the appearance of your
reports consistent.
Yann Vautrin
To change the theme:
• Select the Design tab, locate the Themes group, then click the
Themes command.
Yann Vautrin
To change the theme:
• A drop-down menu will appear. Select the desired theme.
Yann Vautrin
To change the theme:
• The theme will be applied to your entire database.
Yann Vautrin
To change the theme fonts:
• Select the Design tab, locate the Themes group, then click the
Fonts command.
Yann Vautrin
To change the theme fonts:
• A drop-down menu will appear. Select a set of theme fonts.
Yann Vautrin
To change the theme fonts:
• The fonts will be applied to your entire database.
Yann Vautrin