Access_Notes

advertisement
Microsoft Access Overview
Microsoft Access is an RDBMS (relational database management system). A major
component of Microsoft Access is its database engine, called the Microsoft Jet engine.
Another component, the user interface, calls the engine to provide data services, such as
storage and retrieval of data.
Access database objects include:
1. Tables
2. Queries
3. Forms
4. Reports
5. Macros
6. Modules
The Jet engine stores all the objects of a given application in a single database file having
a .mdb extension.
Access Features provided by the Jet engine:
1. Data validation
2. Concurrency control using locks
3. Query optimization
4. Heterogeneous data access through ODBC (e.g. users on the "front-end" can issue
queries in Access to a non-Access database stored on the "back-end" in a client-server
architecture).
Other important Access features:
1. Access is a complete application development environment.
2. Wizards--interactive programs that guide the user through a series of questions in a
dialog mode--can be used to quickly develop forms and reports for input/output
operations.
3. Builders are interactive components that can quickly allow a user to build a
syntactically correct expression.
4. VBA, Visual Basic for Applications, is a programming language that can be used to
develop applications for Access. While macros can be used for some very basic
application development, for more complex applications, VBA is used to build modules.
Using VBA, a user has access to all of the Windows API functions (which cannot be
accessed using macros).
Page 2
5. OLE (object linking and embedding) data types can be used for a field type definition.
OLE objects refer to a class of Windows objects that can be linked and embedded in
other objects. OLE objects include graphs, sound, pictures, and spreadsheets.
Creating a New Database in Access
1. Select File|New Database from the Menu; click on OK button in the window, selecting
a blank database.
2. In the New Database Window, type in the name of your database, and click on the
Create button.
Creating Tables
1. Open the database in which the Table is to be stored.
2. From the database window, click on the Tables tab and Select New and use Design
View.
3. In Design View, type in Field Names and the Data type, together with descriptions of
the fields. Specify a primary key using the button with the key icon on it. The various
Access data types include: text, memo, date/time, currency, counter, yes/no, and OLE
object.
4. When specifying the data type you can specify field properties in the lower pane (use
the F6 key to move from upper to lower pane and vice versa).
Field properties include field size, format (allows you to customize the way the field will
appear; e.g. hyphens in social security numbers), input mask (used for typing in the actual
data), caption (a name used when typing in field data; default caption is the field name),
default value, validation rule (allowing you to specify condition for valid values),
validation text (text displayed when validation rule is violated), required (is data required
for this field?), allow zero length, and indexes (allow or disallow Access to create an
index for this field). From the menu bar, select File|Save As to save the table design.
5. Enter data in datasheet view (use the leftmost button under the File menu to change
from design view to datasheet view, or click on View from the menu bar, selecting
Datasheet view).
Note: It is important to complete the design of tables and fields before creating forms
and reports. Changes made to table and field properties after a form is created will not be
copied to the form (e.g. changing the caption, or description, which goes on status bar of
form).
To add a new field to a table: In design view, select Insert Row from menu bar (or use
toolbar button).
To move field to new location: click and drag using field selector button( arrow in
leftmost column).
Deleting a field: highlight and use delete key.
Note: Table Wizard may be used to create tables.
Page 3
Creating Relationships Between Tables
1. Open Relationships window (use toolbar button, or select Tools|Relationships from
menu).
2. Select tables involved in relationship (Show Tables dialog box helps you with this).
3. Click Close when finished selecting tables.
4. Click and drag field from one table to matching field of the other table (e.g. DNO from
Employee table to Dnumber from Department table) to indicate relationship.
5. The Edit Relationships dialog box appears asking if you want to establish referential
integrity, and whether you want to cascade update related fields or cascade delete related
records by enabling the appropriate check boxes.
6. Click create.
Creating Queries
Use SQL (Note in SQL queries, place names with spaces in brackets; for example:
SELECT UnitPrice FROM [Order Details] WHERE Quantity >15;
Use QBE (Query by Example) grid
Using QBE grid to create a "select query':
1. From the Database Window, click on the Query tab.
2. Select New and Design View.
3. From the Show Table Window, select (Add) the Tables (and/or Queries) to be used in
designing your new query.
4. Rows on the QBE grid include Field, Table, Sort, Show, Criteria, and "or".
On the first row (Field row) enter all field names to be used in your query (include fields
that you want to view (corresponding to fields in the Select clause of an SQL query), and
fields that will be used in a selection criteria (fields corresponding to fields in the Where
clause of an SQL query).
5. Use Sort and Show check boxes under the field names in the same column if you
want to sort on that field (you can select ascending or descending) or if you want to
display that field (i.e. checking the check box in the Show row under a Field name is
equivalent to entering the field name in the Select clause in an SQL query).
6. Fill in the criteria row. The criteria row is used to fill in conditions corresponding to
conditions in the Where clause in an SQL statement; for example, suppose Salary were
entered in the Field row of the QBE grid; if in the same column of Salary, in the criteria
row, you entered >30000, then you would be using a condition "salary > 30000" in the
corresponding SQL Where clause; if criteria are given under several field names (in the
same criteria row), the conditions are "and'd". Under the criteria row in the grid (the "or"
row) other conditions can be included. The compounded ("and'd") condition of this row
is "or'd" with the condition in the criteria row.
7. To run the query press the run button (!) or select datasheet view.
Page 4
Notes concerning queries.
1. Expressions and Access built-in functions may be used to create criteria expressions:
arithmetic operators: +, -, *, /, \ (integer division), mod
comparison operators: <, =, >, <=, >=, <>, LIKE, IN, BETWEEN
Concatenation operators: +,& e.g. [FirstName] + " " + [LastName]
Identifiers: [Employee] ! [LastName] (Employee is the table name and LastName is
the field name)
Functions: categories include math, date and time, financial, text manipulation, and
miscellaneous
some examples of functions in expressions: Int([Price]) > 5
(note field name in
brackets)
[Date of Last Order] < Date() - 30
Len([LastName]) > 10
Note: To express, e.g., the last criteria using the QBE grid, place the expression,
Len([LastName]) in the Field row, and the condition, > 10 in the criteria row
2. To use aggregate functions and grouping, click on the  button in Query Design and a
"Total Row" will appear in the grid; select "group by" under the field(s) used for
grouping, and select an aggregate function (sum, avg, etc.) under the field to which
you want to apply the function
3. Result of query referred to as a dynaset
4. A parameter query may be built by placing in a criteria row a prompt for a parameter
value to be used when building a query. For example, if there is a field name,
DepartmentNumber, and your selection criteria is based on a certain DepartmentNumber,
then in the criteria row under DepartmentNumber field enter the following: [enter Dept.
Num.] . When the query is run a prompt will be given to enter a value for a department
number to be used in the selection criteria of the query.
5. Besides select queries, in Access you can create action queries which include update
queries, delete queries, make-table queries, and append queries.
6. A crosstab query can be built to help tabulate results across groups of records. The
crosstab query will present summaries of these groups in a two-dimensional, crosstab
format. In one dimension appears one variable (field value) and in the other dimension
appears the other variable, both in groups. The crosstab format is easier to read than the
long list that results from a select query.
Building a Form
A form is a visually attractive way of displaying data; forms are useful in making the
input of data easier for the user. Forms use form controls. Form controls are graphical
objects for displaying data. Examples of form controls are labels, text boxes, lines,
rectangles, check boxes, drop down option lists and graphical objects. Form Wizards are
used to make forms. The different types of forms include: single column forms (for one
record at a time), tabular forms (for displaying multiple records), graph forms (for
numerical data), and main/subform forms (used to display matching records from two
different tables in a one to many relationship; for example, in the Department has
Employees relationship, the Department table would correspond to the main form and the
Page 5
Employee to the subform form). When creating a form, you specify the field list (which
fields to include and in what order (referred to as the tab order)). You also specify the
form style (standard (field name to left of a box where data to be entered), or boxed (field
name appears above a box where data is to be entered)). You may select chiseled,
shadowed, or embossed styles to add 3-D characteristics to your forms.
Steps in Building a Form using the Form Wizard:
1. Open the database.
2. Select Forms (on a tab in database window).
3. Click on New; select Form Wizard and choose table or query where object's data
comes from.
4. Wizard prompts for fields (and order of fields) to use.
5. Wizard prompts for type of form (column, tabular, etc.).
6. Wizard prompts for style of form (several choices given).
7. Wizard prompts for Title, which will appear at the top of the form.
After using the Form Wizard, you can modify the form using Design View. You can use
the toolbox to add controls to the form. You can use "handles" to grab the rectangular
field boxes to shrink, expand, or move them.
You can use the form to view and edit data. You can use the form to add records or
delete records or modify records. You can do this in Form View (or datasheet view) after
opening the desired form. Use Edit|Delete to delete a record from the form when viewing
it. Use Insert|New Record to insert a new record. To modify records, simply select the
desired record in
Building a Report
Reports provide a visually attractive means of presenting data in printed form, usually for
all records in a table or a query's dynaset. Reports are similar to forms but provide more
flexibility over summarized data and how the printed data will appear. You can combine
graphical information and Access controls with reports (as you can with forms).
However, you cannot use a report to input or edit data, as you can with a form.
A Report Wizard can be used for creating reports. There are different types of reports,
including single-column reports, groups/totals reports, mailing labels reports, and
summary reports (like group/totals without the detail records).
Steps in Building a Report using the Report Wizard:
1. Open the database.
2. Select Reports (on a tab in database window).
3. Click on New..
4. Report Wizard prompts for fields to display on Report.
5. Wizard asks if you want any Grouping Levels.
Page 6
6. Wizard prompts for a sort order (what field(s)) to sort records on; if groups selected
sorting done within groups). You can also select summary information (if group(s) have
been selected) to be included in the report (e.g. sum, avg, min, max) by clicking on
Summary Options button.
7. Wizard prompts for the layout type and orientation (portrait or landscape).
8. Wizard prompts for style type.
9. Wizard prompts for title of report.
After using the Report Wizard, you can modify the form using Design View. You can
use the toolbox to add controls to the form. You can use "handles" to grab the
rectangular field boxes to shrink, expand, or move them.
Building a Macro
A macro is a list of actions or tasks that you want Access to perform that have been
saved as a named macro. Access will execute the actions in the order they appear on
your list. You can execute a macro by selecting from the Menu Bar, Tools|Macro and
Run Macro. You can also place a command button into a form that causes a macro to
execute when the button is pushed. The macro can perform some actions, run a query,
invoke other macros, print a report, or open another form. You can do error checking
with a macro, employing more sophisticated rules than are possible with the Validation
Rule property of the form or a validation rule attached to a single field in a table. You
can also use a macro to do special look-ups, setting the value of other fields based upon
the values entered into fields on the form. For example, a tax rate could be set based on
the state name entered into a form. Macros can be used to automate certain repetitive
tasks that are done every time a certain event occurs, as when closing a form.
Steps in Building a Macro
1. Open the database.
2. Select Macros (on a tab in database window).
3. Click on New; Access opens an empty macro window.
4. The macro window has a column labeled "Action", and a column labeled "Comment".
5. Fill in successive rows under the "Action" column with the actions to be taken; actions
to the new macro can be added in at least two ways: you can select from the action list in
the macro window (press arrow button to drop down a list; there are several actions to
choose from: e.g. open a query, display a message box, run an application,...); or drag
objects from the Database window to a macro action cell.
6. If you choose an action from the action list, an action arguments pane opens in the
macro window. For example if you choose to open a query, the arguments pane will
open where you are to fill in the query name, view (datasheet, design or print preview),
and data mode (add, edit, or read only). Drop down list boxes help you make your
selection. You can select arguments from the lists Access presents, or you can type them
Page 7
in directly in the lower pane. You can also drag database objects to the argument area
where appropriate.
7. As with other database objects, use the Save or Save As commands from the file menu
to save the macro in the database.
8. You can execute the macro by selecting from the Menu Bar, Tools|Macro and Run
Macro.
Download