Queries: Stored Questions or Actions You Apply to Data
Queries in Access are powerful
and multifaceted. A query retrieves data from your database based on
criteria you specify. An example is a query that retrieves all employees
who live in Florida. Select queries allow you to view, summarize, and
perform calculations on the data in tables. Action queries let you add
to, update, and delete table data. To run a query, you select Queries
from the Objects list and then double-click the query you want to run,
or you can click in the list of queries to select the query you want to
run and then right-click and select Open. When you run a Select query, a
datasheet appears, containing all the fields specified in the query and
all the records meeting the query’s criteria (see Figure 8).
When you run an Action query, Access runs the specified action, such as
making a new table or appending data to an existing table. In general,
you can update the data in a query result because the result of a query
is actually a dynamic set of records, called a dynaset, that is based on the tables’ data. A dynaset is a subset of data on which you can base a form or report.
When you store a query, Access
stores only the query’s definition, layout, or formatting properties in
the database. Access offers an intuitive, user-friendly tool that helps
you design queries: the Query Design window (see Figure 9).
To open this window, select Queries from the Objects list in the
Navigation Pane, choose the query you want to modify, right-click, and
select Design View.
The query pictured in Figure 1.9
selects data from the CustomersExtended query and the Orders and Order
Details tables. It displays several fields, including the ProductID,
Quantity, Unit Price, and Discount fields from the Order Details table,
and the OrderID, Order Date, Shipped Date, and CustomerID fields from
the Orders table. The query’s output displays the data in order by order
date. It displays only records with order dates within a specific date
range. This special type of query is called a parameter query.
It prompts for criteria at runtime, using the criteria entered by the
user to determine which records it includes in the output.
Forms: A Means of Displaying, Modifying, and Adding Data
Although
you can enter and modify data in a table’s Datasheet view, you can’t
control the user’s actions very well, nor can you do much to facilitate
the data-entry process. This is where forms come in. Access forms can
have many traits, and they’re very flexible and powerful.
To view a form, you select
Forms from the Objects list. Then you double-click the form you want to
view or right-click in the list of forms to select the form you want to
view and then click Open. Figure 10
illustrates a form in Form view. This Customer Details form is actually
two forms in one: one main form and one subform. The main form displays
information from the Customers table, and the subform displays
information from the Orders table (a table that is related to the
Customers table). As the user moves from customer to customer, the form
displays the orders associated with that customer. When the user clicks
to select an order, the form displays the entire order.
Like tables and queries,
you can also view forms in Design view. The Design view provides tools
you may use to edit the layout of your form. To view the design of a
form, you select Forms from the Objects list, choose the form whose
design you want to modify, and then right-click and select Design View. Figure 11 shows the Customer Details form in Design view.
Reports: Turning Data into Information
Forms allow you to enter and edit information, but with reports, you can display information, usually to a printer. Figure 12
shows a report in Preview mode. To preview any report, you select
Reports from the Objects list. You double-click the report you want to
preview or right-click the report you want to preview from the list of
reports in the Navigation Pane, and then you click Open. Notice the
chart in the report in Figure 12. Like forms, reports can be elaborate and exciting, and they can contain valuable information.
As you may have guessed, you can view reports in Design view, as shown in Figure 13.
To view the design of a report, you select Reports from the Objects
list, select the report you want to view, and then right-click and
select Design View. Figure 1.13
illustrates a report with many sections; in the figure you can see the
Page Header, Order ID Header, Detail section, Order ID Footer, and Page
Footer (just a few of the many sections available on a report). Just as a
form can contain subforms, a report can contain subreports.
Macros: A Means of Automating a System
Macros
in Access aren’t like the macros in other Office products. You can’t
record them, as you can in Microsoft Word or Excel, and Access does not
save them as Visual Basic for Applications (VBA) code. With Access
macros, you can perform most of the tasks that you can manually perform
from the keyboard, Ribbon, and QuickAccess toolbar. Macros allow you to
build logic in to your application flow.
To run a macro, select
Macros from the Objects list, and then double-click the macro you want
to run (or you can right-click the macro and click Run). Access then
executes the actions in the macro. To view a macro’s design, you select
Macros from the Objects list, select the macro you want to modify,
right-click and select Design View to open the Macro Design window (see Figure 14). The macro pictured in Figure 14 performs one action, the MessageBox action. The MessageBox action accepts four arguments: Message, Beep, Type, and Title.
Modules: The Foundation of the Application Development Process
Modules,
the foundation of any complex Access application, let you create
libraries of functions that you can use throughout an application. You
usually include subroutines and functions in the modules that you build.
A function always returns a value; a subroutine does not. By using code
modules, you can do just about anything with an Access application. Figure 15 shows an example of a module.