MS Access (Tables, Forms, Queries, Reports)

advertisement
Selected Topics in Management
Information Systems
[MOIS470]
Fall 2004
Nahed Amin
MS Access
(Tables, Forms, Queries, Reports)
1
Introduction
 What’s Access?
 Access is a relational database application
designed to handle thousand or a few records
depending upon the user needs
 Access can simply list a collection of names and
addresses in a single table, or manage complex
relationships between data that pan several
tables
2
Introduction
 Database tables are collections or lists of
records, each record holding individual
pieces of information in fields
 As a database Management System
(DBMS), Access enables a user to create
and maintain these tables, employing
sophisticated, yet easy-to-use, tools to
facilitate defining, constructing, and
manipulating data
3
Introduction
 To plan a database you need to do the
following:
 Decide what the function of the database will be
 Determine what information you need, where this
information will come from and how it will be
entered into the database
 Determine the structure of the database and the
relationships between the various tables
 Decide on the structure of the tables including
field names, field types and input validation.
 Decide which forms, reports and queries you will
4
use
Getting Started …
 In Access, a database application is
composed of database objects, which are:
 Tables
 Forms
 Queries
 Reports
 Pages
 Macros
 Modules
5
Getting Started …
 Access centralizes all database application into a single
database file. All the tables, forms, reports, and other
Access objects, designed for the database, are stored in
this file
 Having a single database file not only eliminates
keeping tracks of a large number of individual files,
but also permits an Access database to be transported
much easier than other database types
 Before you can begin constructing the individual
database object types, you must first create an Access
database file to contain the objects
6
Tables
 Design a table:
 You can add individual fields to a blank table structure
using the panes in Table Design View
 The top pane is the grid and is used to specify a field’s
name, descriptive text, and data type.
 The bottom pane is the field properties section and is used to
specify field size and other attributes
 Each row in the grid corresponds to one field; a collection
of all fields is grouped into a record
 Access denotes that one field in every table contains unique
entries that distinguish that record from others, this is the
primary key field. The primary key field is denoted by a key
symbol in the field’s row selector box to the left of the 7field
row
Tables
 Design a table:
 Within each field, there are three corresponding
columns in the grid:
 The first is the field name column, which defines the name of
the field
 The second is the data type column, which regulates what type
of data a field can hold, such as numbers, text, and dates
 The third column is the description; information in the
description column appears in the status bar when the field is
selected in either a form or datasheet view
 Field names appear as column headers in Table
Datasheet View
8
Tables
 Design a table:
 A fundamental part of designing an Access database
table is knowing the proper data type to assign to a
field.
 A correctly defined field not only lends itself to
greater efficiency, but can also reduce compatibility
problems when your database application places
more demands on the data, such that in calculations
or concatenation
9
Tables
 Design a table:
 Access data types are:
 AutoNumber holds records numbering (either incremental
or random)
 Date/Time
holds dates & times (in a variety of common
formats)
 Currency
holds currency values
 Text
holds characters, numbers, punctuation
marks, and special symbols
 Memo
stores a large quantity of text information
 Number
holds only numbers, + or – signs, and
decimal places
10
Tables
 Design a table:
 Access data types (cont.):
 Yes/No
stores either a Yes or No value; appears in
datasheet view as a checkbox (also called a
logical or Boolean type)
 OLE
holds graphics, sounds, and other object
Linking & Embedding objects
 Hyperlink
stores URLs for Internet locations
 Lookup
Wizard
creates a field that allows you to retrieve
value from another table
11
Tables
 Enter & delete data:
 Creating a database table is a two-part process; the first part
defines the structure of the table. The second part of the
process involves input from the user; this usually demands
physically typing the data into the table
 Data can be entered directly into a database table using the
table’s datasheet view; this view displays a table in a grid
format. Each row in the grid is a record, with each cell being
an individual field. You enter data by typing values directly
into these cells
 When you advance to a previous or new blank row, Access
writes the data to the table
 A single new blank record row will appear at the bottom of the
grid and is denoted by an asterisk (*) in the row selector
12
Tables
 Format the datasheet:
 You can format the datasheet view to make data entry and viewing
easier and more productive
 You can adjust each field (or column width) in the grid to better
view its data. The data may not fully display because the default
column width is too narrow. You can click and drag a grid line to
manually resize a column or modify the column’s properties
 Access also enables you to change the text attributes (font size &
style, and color of text) of data appearing in the grid. The
modification of these characteristics is applied to all text in the grid
 Changing the column order of fields in the datasheet view will not
modify field order in the actual table structure
 Renaming a column header will rename the actual field name
13
Tables
 Filter & sort tables:
 Records display in the order in which they were created by default
 Access provides sorting and filtering features to assist you in
organizing and delineating table data as the number of records
increases
 Records can be sorted in either ascending (low to high) or
descending (high to low) order based on a selected field
 Filtering data involves specifying conditions that a record must
meet to be selected. Filters can contain single or multiple conditions
(tests) that must be met for a record to be listed. A set of records
produced as a result of a filter is known as a dynaset
 You can apply two or more filters to the same table; in such case
logical conditions such as AND or OR are used
 When a table is closed, sorted or filtered records return to their
14
original state
Tables
 Edit field attributes:
 You can make permanent modifications to the
structure of a new or an existing table using design
view. A new table may need modification after initial
data entry has exposed problems. Existing tables may
have to adapt to new data or be modified to function
under different circumstances
 Access enables you to change the data type of fields that
already contain values. However, certain changes don’t
provide for successful data conversions, such as
attempting to convert a field full of characters (text) to
a number value
15
Tables
 Change format properties:
 Access by default displays data in the same format as
it was entered. You can have data displayed in a
different format.
 Access allows you to alter the display format but not
the value of data
 Located in the field properties pane of table design
view, the format property can access and implement
predefined formats (dependent on data type), or allow
you to construct a custom layout of your own
16
Forms
 Forms provide a user interface for database
tables. They are used as the primary method of
entering, editing, and displaying data contained
in database tables
 While you can use the datasheet to perform
many of the same functions,forms present
information in an organized manner, thus
enhancing data entry
 Forms are generally layout documents that
combine graphic objects and table data
17
Forms
 Forms are layout documents that use
special object types, called controls, to
provide access to table data
 The controls are linked to specific fields in
the underlying table. The controls allow
you to modify the display of data and
perform data entry
18
Forms
 Access supports three primary views for
working with forms:
 Form Design View, where the controls and layout can
be modified, but the table data is not displayed
 Form View, where controls linked to table fields
display the field’s value for the current record
 Datasheet View, where you can work directly with the
table used by the form
19
Forms
 Use a Form Wizard
 The Form Wizard can assist you in constructing basic form
layouts. Instead of constructing a form manually, you can use the
Form Wizard to quickly produce a basic form layout
 The Form Wizard prompts you for necessary information to
construct a form according to one of several predefined layout
types. After answering each question, you can advance to the
next window or return to previous windows to select different
options
 The Form Wizard allows you to define the underlying table(s),
field inclusion, and basic layout schemes. You can quickly create
and link controls to an underlying table(s)
 If necessary, you can later customize the form in Form Design
View
20
Forms
 Design a Form
 Constructing a layout manually allows you to create
more specialized forms
 Creating or editing a form manually can be performed
using the Form Design View
 There are four design features in the Form Design
View:
 Field List Dialog Box: contains a list of fields supported by
the form underlying database table(s). Drag a field to the
form area to instantly create label and text-box controls for
that field
 Property Sheet: list properties of the controls on the form and
allows you to modify them
 Palette: contains buttons to control the appearance and
colors of forms and objects
21
 Toolbox: contains tools to help you insert controls on the
form layout
Forms
 Design a Form
 The form area (appearing as a grid) can be divided
into several sections
 The form supports only a Detail section by default,
which is the main body of the form. Controls linked to
field in this section change every time the form
displays a new record
 The Footer and Header sections permit you to create
controls that are not dependent on the current record,
such as title header
22
Queries
 Access uses a query to extract information from a
database table. You construct a query using a
design grid
 Queries are designed and executed from the
Query window. The window consists of two
panes:
 Top pane: displays the field-list boxes and any
relationships or links between tables
 Lower pane: contains the design grid and is used to specify
the fields, sort order, and search criteria. Search criteria
must be specified within the column of the field the criteria
will test. Query criteria can be string, numbers or a range
23
of values
Queries
 Access displays the results of a query in a layout
called a dynaset, which is a temporary list of
records that match the current query.
 Dynasets are linked to the tables used to create
them. If you change the value of a field in a
dynaset, the corresponding field in the underlying
table is changed.
 As the data in a dynaset’s base table change, so
does the dynaset
 You can save the query and its specifications, but
you cannot directly save a dynaset as a database
24
table
Queries
 Sort Data
 Access does not sort the records it displays by default.
The records are displayed in the order found
 Sorting data presents it in an organized manner.
Sorted results help identify groups and trends
 Although Access permits you to use datasheet-oriented
sorted controls, the resulting sort remains only while
the dynaset table is active. If the underlying query is
re-run, the sort order is lost. You can establish a
permanent sort order by specifying it in the design
grid
 Each column in the design grid can have a sort order
to be chosen, permitting you to specify a sort order for
25
one or all fields appearing in the resulting dynaset
Queries
 Sort Data
 Complex sorts can be defined by applying sort orders
to multiple fields
 Click and drag the column selector of a field (in the
design grid) to reposition the field
 The left-most field in the design grid with a sort order
defined will control the primary sort order. If
subsequent fields also have a sort order defined, these
orders will be secondary
26
Queries
 Use Multiple Criteria: ANDs / ORs
 Single criteria queries are often too limited to extract
specific information. Sophisticated queries use multiple
criteria. Access enables queries to contain multiple criteria
through use of AND and OR conditions
 The AND condition is used to join two or more query
criteria. The first criterion and the second one must
both test true before a record is retrieved
 AND conditions help construct queries that are restrictive,
reducing the number of records appearing in a dynaset.
When you need queries to be more inclusive, considering
one or more alternative values, OR condition must be used
27
Queries
 Use Multiple Criteria: ANDs / ORs
 A multiple-criteria query using OR conditions displays
a record if one or the other (or both) of its criteria
tests as true. Records do not display if all the criteria
tests as false
 The design grid itself supports AND and OR
conditions. If, however, you need to specify an AND
condition for values in the same field, you must
manually key in the AND operator using the
Expression Builder or Zoom window
28
Queries
 Create a Parameter Query
 Parameter queries allow you to have Access prompt
you for search values before execution. You can easily
enter different parameter values when prompted,
rather than modifying criteria values in the design
grid for each new search value
 Parameter query is useful when you expect to run a
query repeatedly with changes to the query criteria,
but not to the actual structure of the query
 Parameter queries can search for different values in
the same fields. This is possible through the
substitution of parameter statements with actual field
criteria
29
Queries
 Create a Parameter Query
 The text in the parameter statements should be
enclosed in square brackets, and the last character in
the text must be a colon
 Example: [Enter City:]
 When a parameter query is encountered, the Enter
Parameter Value dialog box will open, prompting you
for a value. When concluded, the query internally
substitutes the parameter statements with the actual
values you entered and returns the resulting matches
30
Queries
 Create a Parameter Query
 To prevent you from entering a parameter value that
is of the wrong type (i.e. a string value for a number
field), Access permits you to define data types for the
parameter values. You can define the data type a
parameter will accept in the Query Parameter dialog
box, reducing the possibility of error
 Ranges can be used with parameter statements. You
must define the comparison operator before adding
the parameter statement. Access will evaluate the
entire statement as a single expression
31
Reports
 A report is a design document that displays
formatted data from one or more database tables.
Reports are designed for printing and
distribution to an audience
 Reports are used to summarize large quantities of
data into meaningful information using
calculations, record lists, totals, and other reports
objects
 Reports produce paper forms such as invoices
and inventory lists
32
Reports
 Access provides several Report Wizards to assist
you in the construction of reports. Report Wizards
prompt you for basic information about the
report, and then create a basic report layout
 Access permits you to view and modify the results
of a Report Wizard just as you would do for an
report created manually. You can use the tools
supported by Report Design View to move, insert,
and modify objects to customize the report
33
Reports
 Reports have a Design View and a Run View.
Reports are designed under the assumption that
the data and formatting will be printed. Access
supports a Print Preview that allows you to insect
the report before printing it
 Reports can be based on either a database table
or the result of a query
34
Download