Professional Documents
Culture Documents
1.
4.
5.
6.
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.
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.
3.
1.
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.
o Albums.ReleaseDate
o Albums.AlbumName
o Albums.Genre
o Artists.ArtistName
1.
2.
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.
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.
1.
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.
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.
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.
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.
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.
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.
Form Properties
Access provides the Property Sheet for setting properties against a
form and its elements. Here's an overview.
Go ahead and change some colors, fonts, etc. Next we will replace
the ArtistId field with a combo box containing artists' names.
Now that we've created our form, we could go right ahead and enter
data.
This means we'll need to enter the artist ID for every album we enter.
1.
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.
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.
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.
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.
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.
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.
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.
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.