You are on page 1of 76

MIS 390 MS Access Tutorial 2

How to use Query Design


We will now create a query that queries and returns data from both
the Artists and Albums tables.

1.

Launch the Query Designer


Click Query Design from the Create tab on the Ribbon.
The Show Table dialog box will appear.
2.

Select the Tables for the Query


Select both the Artists and Albums tables and click Add.
Click Close to close the dialog box.
3.

Design the Query


Now we get to design our query. We'll keep it simple, and create
a query that returns all albums from Iron Maiden.
Select the options as per the screenshot.
Run the query by clicking the ! Run button on the Ribbon.
If you can't read the screenshot, the query options are:
o Fields:
 Albums.AlbumName
 Albums.Genre
 Albums.ReleaseDate
 Artists.ArtistName

o Show: All except Artists.ArtistName.


o Criteria: Apply "Iron Maiden" to the Artists.ArtistName field.

4.

View the Query Results


The query results are displayed in Datasheet View.
Notice that the results only display the fields for which we
selected Show against.
We specified criteria for the Artists.ArtistName field but we chose
not to display that field in the results. If we had, Iron Maiden would
be repeated five times — once for every record.
You can also run the query by clicking the View button.
You can switch between the query design and the results by clicking
this button (just as you do when switching between Design View and
Datasheet View when working with tables).

5.

View the Query in SQL View


You can view the code that Access generates behind the scenes
by switching to SQL View.
To switch to SQL View, click on SQL in the bottom right corner.
Alternatively, you can use the View button on the Ribbon to
switch to SQL View. To use this option, click the little "down
arrow" below the word View. This will expand all of the "View"
options. Click SQL View from the list.
You can also modify the query in this view if you know SQL.

6.

Save the Query


You can save a query so that you can run it again later. Any time
you run the query, it will return the current (up to date) data. In
other words, the query results will reflect whatever data is in the
table/s at the time it is run.
To save the query, right-click on the query tab and select Save.
Name the query Iron Maiden Albums. The query will appear in the left
menu under a Queries heading.
Alternatively, you can click the X icon to close the tab and you will be
prompted to save the query.

Now it's unlikely that the user will want to spend all their time
searching for Iron Maiden albums.

So what we'll do next, is take a copy of the above query, then modify it
so that the user can specify which artist they want to see albums from.

You can create a query that relies on user input. The


value that the user provides determines the result set.

In Access, queries can accept parameters that can be used to determine the results of the query.
When the query is run, the user is first prompted to enter a parameter (or multiple parameters).
Once the parameter has been supplied, the query can return the results, based on the parameter
provided.

For example, we previously created a query that returns all albums from Iron Maiden. This query
could be modified so that it returns all albums from a given artist. The artist can be specified by
the user.

Access makes this extremely easy to do. In fact, all you need to do is replace "Iron Maiden" with
some text enclosed in square brackets.

So let's take a copy of the above query, and modify it so that the user gets prompted to enter an
artist name when running the query.

Make a Copy of the Existing Query


Our new query is going to be almost the same as the previous one. So we can save some time
and start with a copy of the existing one.
1.

Copy the Existing Query


In the left navigation pane, right-click on the Iron Maiden Albums query and
select Copyfrom the contextual menu.
2.

Paste the Query


Now right-click again inside the navigation pane and select Paste.

3.

Name the new Query


Name the query at the prompt and click OK.
Modify the Query
Now we can modify the copied query to prompt the user for a parameter.

1.

Open the Query in Design View


In the left navigation pane, right-click the query and
select Design View.
2.

The Query Design


Replace Iron Maiden with [Which Artist?].
The square brackets will cause a parameter dialog box to
open when the user runs the query. The text inside the
square brackets will be displayed to the user, and an input
field will be displayed for their input.
3.

Run the Query


Now run the query (either by clicking Run or View in the
Ribbon, or clicking the Datasheet View icon in the bottom
right corner of the screen).
A prompt will display, asking for an artist. Enter an artist.

The Expression Builder helps you build


expressions for queries, validation rules, default
values, and more.

Access includes an Expression Builder that simplifies the process of


building expressions. You can use the Expression Builder to create
queries, set a default value on a field, set a validation rule against a
field, and more.
The Expression Builder is kind of like a lookup wizard, where you can
browse a list of expressions that you have trouble remembering. It
allows you to combine expressions into a larger, more complex
expression, that can help you create queries or apply data rules
against a field.

An expression is any legal combination of symbols that results in a


value. The Expression Builder includes the following to help you build
an expression:

 Built-in functions. For example, Count(), Date(), and MsgBox().


 Constants. Such as True, False, NULL.
 Operators. For example >=, &, And
 Fields. From tables, queries, forms, and reports.
Previously, we created a simple query. We will now create another
query, this time with the help of the Expression Builder.

Using the Expression Builder to help


Create a Query
Our database is full of old-school music. Someone has asked if our
database contains any albums that were actually released in the last
25 years. So we decide to build a query to find out.

But we want to make it 25 years from the date the query is run. That
way, if someone else asks the same question in say, 10 years time,
we can re-run the same query and it will look back 25 years from that
date.

But we can't quite remember how to construct that query. So... time
for the Expression Builder!
First, Start a New Query
Because we're going to use the Expression Builder to help build a
query, we need to start a query first. Then we can launch the
Expression Builder from within that query.

1.
Launch the Query Designer
Click Query Design from the Create tab on the Ribbon.
The Show Table dialog box will appear.

2.

Select the Tables for the Query


Select both the Artists and Albums tables and click Add.
Click Close to close the dialog box.
3.

Select the Fields to Display


Add the following fields:

o Albums.ReleaseDate
o Albums.AlbumName
o Albums.Genre
o Artists.ArtistName

Keep Show checked against all fields.

Now for the Expression Builder


Now that we've got a query open, we can go ahead and launch the
Expression Builder.

1.

Launch the Expression Builder


Under the ReleaseDate column, click inside the Criteria cell.
Now click the Builder button on the Ribbon. This will launch the
Expression Builder.

2.

Add a Built-in Function


First, click Functions on the left pane to expand its options.
Click Built-in functionsand then Date/Time in the middle pane.
On the right pane, scroll down until you see the Year() function.
Double-click Year() so that it appears in the top pane.
The Year() function returns the "year" part of whatever date is specified
between the brackets.
3.

Add a Field
In the top pane, click «date» inside the function's brackets so that it
becomes highlighted.
Now, expand Music.accdb, then expand Tables, then click on Albums to
reveal all the fields of that table. Double-click ReleaseDateso that it
replaces «date» at the top pane.
4.

Add an Operator
Now click to the right of the function so that you can continue
building the expression.
On the left pane, click Operators, then Comparison in the middle pane
to reveal the comparison operators.
Double-click the greater than sign (>) so that it's added to the top
pane.
5.

Return the Current Year


Our query will be comparing two "Year" values, so add
another Year() function, and click the «date» to highlight it, like
before.
Now, in the right pane, scroll up to the Date()function. Double-
click it so that it replaces «date».
The Date() function returns the current system date.
The Year() function returns the "year" part of that date.
6.

Add another Operator


Click to the right side of the expression so that you can add a
"less than" operator (-).
On the left pane, click Operators, then Arithmetic in the middle pane
to reveal the arithmetic operators.
Double-click - so that it's added to the top pane.
7.

Add a Fixed Value


Type 25 at the end of the expression. This how many years we
want the query to cover. For a longer or shorter time span, just
change it to however many years you need to search.
The expression is now complete. Click OK to add the expression
to your query and close the Expression Builder.
8.

Run the Query


Your query now has the full expression listed in the Criteria field.
Feel free to expand the width of the column so that you can see
the whole expression.
Click View or Run to run the query.
9.

Your Query Results


You can now see that yes, we do in fact have a number of
albums that were released within the last 25 years :)
Feel free to save the query as Albums from the last 25 Years or similar.

Same Query, Different Design


There's often more than one way to construct a query in Design View. Often,
the same result can be achieved from different query designs.

Here's an example of the above expression, but using a slightly different query
construction in Design View.
Parameter Query
We could also turn this query into a parameter query. That would make it
more useful.

We could get the user to specify how many years back they want the query to
go.

All you need to do is replace 25 with [How many years back?] or something
similar.

Building an Expression Manually


You don't need to use the Expression Builder if you already know the
expression to use. You can simply type it directly into the Criteria field
of the query designer.
And even if you do use the Expression Builder, you can still type
characters directly into your expression in the top pane. For example,
there's no need to navigate all the way to the - symbol if you already
know you need it. Just type that part yourself.

Here are two examples of basic expressions that should be easy to


remember without needing the Expression Builder:

1.

After a Certain Date


Query for all albums released after 01/01/1980.
Turn this into a parameter query by replacing >#1/1/1980# with
>[Starting From]
2.

Between Two Dates


Query for all albums from the 80s (i.e. released between 1980
and 1990).
Turn this into a parameter query by replacing: >#1/1/1980# And
<#1/1/1990#
with
Between [From] And [To]

You can export data from Access to a variety of


formats by using the External Data tab on the
Ribbon.

Access includes options for exporting data to other sources. You can
export to a variety of formats, including:

 Excel file
 Text file (CSV, TSV, etc)
 XML file
 PDF file
 XPS file
 Email
 Word document
 Sharepoint list
 HTML document
 Another Access database
 ODBC datasource (such as SQL Server)
You can export from a table, a query, a form, or a report.

Today we'll export two different queries to two different formats:

1. We'll export our Iron Maiden Albums query to an Excel spreadsheet.


2. We'll export our Albums from the last 25 years query to an XML file.

Export a Query to Excel


Let's export the results of the Iron Maiden Albums query that we
saved previously.

In this case, we'll export the query results to an Excel spreadsheet.


1.

Launch the Export Dialog


Right-click on the query in the left menu (in our case, the Iron
Maiden Albums query) and select Export > Excel from the contextual
menu.
Alternatively, you can select the query in the left menu, then
click Excel from the export options in the Ribbon (on the External
Data tab).

2.

Export Options
Review the options. In our case, we'll keep the file name as
Access has suggested. However, we'll put a tick next to Export
data with formatting and layout.
Click OK to export the data.
3.

Save Export Steps?


Access gives you the option of saving the steps you just took, so
that you can re-run them again later.
In our case, we'll give it a miss. Click Close to finish.
4.

View the Exported File


You can now check your exported file in Excel. Navigate to the
location you chose at step 2 and open the file.
You can save yourself from having to navigate to the file by
checking Open the destination file after the export operation is
complete at step 2.

Access provides many options that make it easy


to create forms.

One thing that separates Access (and other desktop database


systems) from most client/server database management systems
(DBMSs), is the ability to create forms. Most client/server systems
require you to use separate tools to build forms. With Access, you can
create forms straight from within your database.

In Access, you have the option of creating a blank form from scratch,
using the Form Wizard, or having Access automatically create a form
from a table or query.

Create a Form from a Table


Let's see how quickly we can get a form up and running. We'll create a
form based on the Albums table that we created previously.

This will allow users to enter data into the table via the form.

1.
Create the Form
Select the table in the left menu that you want to use for the form
(in our case, the Albums table) and click Form from the Createtab on
the Ribbon.

2.

The Form
Access immediately creates a form based on the selected table.
The form displays all fields as label/field pairs.

o The label allows users to see which field is which.


o The field is presented in an editable form element such as
a text box, combo box, etc. The user can then edit the
underlying data using the form elements.

Close the left navigation pane (using the « icon) so that there's more
room to display the form.

Form Views
There are three different views for working with forms:

 Design View
 Layout View
 Form View
When you first create a form from a table, Access opens it in Layout
View. You can customize the form however you like. You can switch
between views as often as you like while customizing your form.

Switching views can be done by using the View button on the Ribbon,
or the three icons at the bottom right of the Access window.

Here's an explanation of each view.


Design View
Design View provides a more detailed view of the form's
structure than Layout View.
Design View also allows you to modify the form without having
any data getting in the way.
Most tasks can be done in either Design View or Layout View,
but there are some form properties that can only be changed in
Design View.

Layout View
Layout View provides a more visual layout for editing the form. It
bears a close resemblance to the actual form that the user will
see.
Layout View allows you to edit the form properties while it has
real data.
As mentioned, there are some things that can't be done in
Layout View. In some cases, Access will tell you that you need to
switch to Design View if you want to make a certain change.

Form View
Form View is how the user will see the form. You can't edit the
form's properties, but you can use the form just as a user would.

The Form Tabs


When you edit a form in either Layout View or Design View, three tabs
appear on the Ribbon with specific options for editing forms.

Here's an overview of these tabs.


The Design Tab


You can use the Design tab to change the color of the form,
change fonts, or change the whole theme.
You can also upload a logo, add/edit a title, add the date and
time, etc.
The Design tab also allows you to add form controls such as
buttons, text input, combo box, etc.

The Arrange Tab


You can use the Arrange tab on the Ribbon to arrange individual
elements. Select the element/s and click an option to re-arrange
them.
For example, you can move a field under the field beneath it by
using the Move Down option. You can also set padding, margins,
add arbitrary spaces, etc.

The Format Tab


You can use the Format tab on the Ribbon to format individual
elements.
To style an individual element, simply select the element and
apply a style.
Select multiple labels or fields by holding down the Shift key while
selecting them.

Form Properties
Access provides the Property Sheet for setting properties against a
form and its elements. Here's an overview.

The Property Sheet


You can use the Property Sheet to apply settings to individual
elements, or the whole form. There are many different properties
that can be applied across a form. These aren't just limited to
formatting properties, they include data validation properties,
event based properties, and more.
The Property Sheet displays properties for the selected element.
When you select another element, the properties in the Property
Sheet will update to reflect the properties for that element.
To open the Property Sheet, click Property Sheet from the Design tab
in the Ribbon.
You can also open the Property Sheet by right-clicking on an element
and selecting Properties from the contextual menu.

The Date Picker vs The Input Mask


You can use the Property Sheet to set a date picker for date
fields. In fact, Access automatically sets date fields to use a date
picker, so in many cases, you won't need to do anything.
However, if you have set an input mask (like we did previously),
the date picker won't be shown on the form. In Access, you can't
use a date picker on a field that has an input mask applied.
So, if you want to use a date picker, you'll need to remove the
input mask. You can do this via the Property Sheet. The Input
Mask is under the Data tab.
Removing the input mask on the form doesn't remove it on the table
field. If you find yourself changing data rules on a form, you might want
to consider whether it's better to change/remove it on the table instead.
You can do this by opening the table in Design View and adjusting the
property.

Go ahead and change some colors, fonts, etc. Next we will replace
the ArtistId field with a combo box containing artists' names.

ccess provides a "Combo Box Wizard" to walk


you through adding a combo box to a form.

Now that we've created our form, we could go right ahead and enter
data.

But we might encounter a small issue. We'll have to remember the


artist IDs of all artists. This is because the only artist information on
the form is the ArtistId. The artists' names are nowhere to be seen.

This means we'll need to enter the artist ID for every album we enter.

This could become an inconvenience. Whenever we enter a new


album, unless we remember the ID of every artist, we'll probably need
to open up the Artists table to look up the ArtistId for the album we're
entering.

Time for a slight redesign of the form.

Add a Combo Box with the Artists'


Names
Here, we will add a combo box containing a list of all artists' names to
the form. This will allow users to select artists by their name, rather
than their ID. However, the form will still insert the ID into
the Albums.ArtistId field.

1.

Add the Form Control


Open the form in Layout View.
Click the Combo Box icon from the Design tab on the Ribbon.
Now click the form in the location that you want the combo box to
be inserted. The Combo Box Wizard will open.
Before you click, as you hover your cursor over the form, Access will
display a red line wherever you move the cursor, to help you see where
the combo box will be inserted.
2.

The Combo Box Wizard: Select a source


for the Values
As mentioned, clicking on the form automatically opens the
Combo Box Wizard.
Select the first option: I
want the combo box to get the values
from another table or query and click Next >
3.

Select a Table
Select Table: Artists and click Next >

4.
Select Fields
Click >> to move both fields across to the right pane, then
click Next >

5.

Select Sort Order


This determines the order in which the items will appear in the
list. We'll order them by the artists name.
Select ArtistName and Ascending, then click Next >
6.

Column Width
Adjust the column width as desired then click Next >

7.
Select where to store the Value
Click Store that value in this field: and select ArtistId
Then click Next >

This means that the ArtistId for the selected artist will be inserted into
the Albums.ArtistId field.
So even though the user only selected the artist's name from the combo
box, the ID is what's inserted into the table.

8.

Combo Box Label


Type a label for the combo box and click Finish
9.

The Combo Box


Access will close the wizard and you'll see the form with the new
combo box.

Delete the old Control


You have the option of keeping the ArtistId form control or


removing it.
To delete the old control (and its label) click on each one while
holding down the Shift key. Then right-click and select Delete from
the contextual menu.

Test the Form/Combo Box


Go ahead and use the form to add new records to the database —
now's your chance to add something from the last 25 years ;).
1.

Go to a New Record
Switch to Form View (either by clicking the View button on the
Ribbon, or the Form View icon at the bottom right of the screen)
and open a blank record.
To open a blank record, click the New (blank) record icon at the
bottom of the screen.
2.
Enter Data
Enter data as a user would.
Tabbing or pressing Enter past the last field will submit the form
and insert the contents into the table.
Note that, to display the date picker icon, you'll need to click inside
the Release Date input control.

Access has several methods for creating reports.


Any user can create a report quickly in Access,
whether they're a novice or an expert.
In Access, you can create a report from a table or query with the click
of a button. You can also use the Report Wizard to walk you through
the process. And more experienced users can start with a blank report
and build it exactly as they wish.

Create a Report based on a Query


We'll create a report based on the Albums by Artist query we
created previously.

The interesting thing with this query is that it asks for user input — the
user tells the query which artist they'd like to see albums from.
However, the query doesn't display the artist name in its result set.

So, we'll need to display this parameter on the report too — otherwise
the reader might not know which artist the albums belong to.
1.

Create the Report


Ensuring the report Albums by Artist is selected in the left
navigation pane, click the Report button in the Ribbon.
2.

The Parameter
Access immediately attempts to create a report based on the
selected query.
But because it's a parameter query, you need to enter a
parameter before the report can be displayed.
In this case, enter the name of an artist and click OK (or just
click OK to display a blank report).
3.

The Report
The report displays the fields in columns, with the field names as
column headers.
It has basic formatting applied, and this can easily be changed
using the various formatting options.
But there's one problem with this report. It doesn't display the
artist name. If a stack of reports were printed or emailed to
another person, they'd have no idea who each report is
displaying albums for.
So next, we'll add the artist name to the report.
Save the report by right-clicking on the report's tab and clicking Save.
Add the Query Parameter to the
Report
The problem with the above report is that it doesn't display the artist
name. So there's no indication of which artist the albums belong to.

This is because when we wrote the query, we chose not to display the
artist name in the results. If we did, the artist name would be repeated
for each album. If there were say, 20 albums, the artist name would be
repeated 20 times.

Now that's OK for the query. The user who runs the query will know
which artist they entered.

But it's different with reports. The report could be one of many reports
sitting on someone's desk (or in their email inbox). They're probably
not going to know who each album belongs to without the artist name
being displayed somewhere on the report.

To address this problem, we will need to work out how to add the
query parameter to the report.

And that's exactly what we'll do now.


1.

Switch to Design View


Switch to Design View by clicking on the Design View icon in the
bottom right corner of the screen.
Close the left navigation pane by clicking its « icon to allow more room
for the report.
2.

Increase the Height of the Report Header


Click on the bottom edge of the report header and drag it down
to make it a little higher.
We're doing this so that we have enough room to display the
artist name under the report title.
3.

Select a new Text Box Control


From the Design tab of the Ribbon, click the Controls button and
select the text box option.
4.

Add the Text Box to the Report


You can click and drag to place the text box where you want,
and at the size that you want.
Click in the report header, under the title, and drag it so that it's
wide enough to display the longest text that will be displayed.
The text box will be inserted where you click.
You can always change the width or height of any element, either by
clicking and dragging, or by opening the Properties Sheet and entering
a specific width/height.
5.

Add the Parameter


Add the parameter to the text box by entering the following:
=[Reports]![Albums by Artist]![Which Artist?]

Also change the label to read Artist:


Alternatively, you can add the parameter into Control Source on the
Properties Sheet.
6.

Run the Report


Run the report by clicking the Report View icon in the bottom
right corner of the screen.
You will be prompted to enter an artist name (as required by the
query).
Enter an artist's name and click OK to display the report.
7.

The Report
The report is now displayed, and it includes the artist's name in
the header.
Use some of the features listed below to give the report a fresh new
look.

Report Design
Reports can be designed and formatted in a similar way to forms.
When you create a report, four new tabs appear in the Ribbon
specifically for working with reports.
Report Header & Footer
The report header and footer are displayed only once in the report.

The report header is displayed at the top of the first page, and the
report footer is displayed at the bottom of on the last page. If a report
contains say, 10 pages, the header and footer will be displayed on all
10 pages.

So, in our example above, the artist name will only be displayed once
— at the top of the first page of the report.

Page Header & Footer


The page header and footer are displayed on every page of the report.
If a report contains say, 10 pages, the header and footer will be
displayed on all 10 pages.

So, in our example above, if we moved the artist name to the page
header, it would then be displayed on every page of the report.

Detail
The report detail section is where the actual data is displayed. This
can run across many pages if required, and each page will continue
where the previous left off.

The Property Sheet


As with other Access objects, you can use the Property Sheet to
modify your report's properties. You can modify properties for the
whole report, or just for selected elements.
You can change formatting properties (such as height, width,
color, etc), data properties (such as where a control's data is
sourced from), event properties (such as when a user clicks on
the element), and more.
The Property Sheet can be accessed by clicking on Property
Sheet on the Design tab of the Ribbon, or by right-clicking on an
element and selecting Properties.
The Property Sheet only displays properties for the selected
item.

Report Views
Access provides four different views for reports.

As with all other Access objects, you can toggle the view using the
buttons at the bottom right of the screen, or by using the View button on
the Ribbon.

Design View
Allows you to view and design the report structure. Doesn't
display live data. Instead, it displays any expressions that are
used to generate content.
For example, when you look at the above report in Design View,
you can see that various fields use in-built functions to display
dynamic data. The Date() function is placed in the right corner of
the header to display the current date (i.e. the date the report
was generated). Similarly, the Time()function displays the current
time. And the Count() function is used in the footer to display a
count of the records returned in the report.

Layout View
Provides a view that more closely resembles what the report will
actually look like to the user. Layout View allows you to edit the
report design while it contains live data.

Print Preview
Allows you to see what the report will look like when printed. You
can also go ahead and print from this view by clicking
the Print button in the Ribbon.

Report View
Displays the report as the user sees it.

You might also like