Practical exercises DB&SQL: short Access tutorial Henny Klein aug

advertisement
Practical exercises DB&SQL: short Access tutorial
Henny Klein aug 2005
Try to complete this tutorial in two weeks
This tutorial will help you to get acquainted with Access. Think about what is happening and ask
your questions in the lectures or practical sessions. Save what you do (G:) and when you
completed the whole tutorial, put your databasefile in the assignment on Nestor, so I can check
your activities.
Start copying library.zip to G:, in a new directory. Clicking the file activates Winzip, extract the file.
Start Access (Start menu), choose Open Existing Database and open library.mdb (or click on the
file to open it). If conversion is necessary, follow the instructions.
You see the main menu, the Database Window, with tabs for Tables, Forms, Queries, Reports
etcetera. Most of these object will be discussed in this tutorial. Modules are programming files
(they contain some examples, if you're curious)
Choose Tables. As a service, I copied/pasted already all tables, so a clean start after a mistake is
possible by copying the copy back.
Doubleclick BOOKS (or click BOOKS and click Open).
The table is shown in Datasheet View (the other option is Design View). Here you can inspect,
insert, change and delete data, and change the layout of the table.
Note: In Access all changes to data are saved automatically, changes to definitions, forms, all
other stuff must explicitly be saved (you will be reminded if you close an object: save?)
Try to sort BOOKS on Price. Make the column PubID somewhat smaller.
Are more manipulations possible in Datasheet View?
HELP
First thing to know is how to get HELP. Click on Help, and choose the context-sensitive help (F1:
the assistant, also accessable by clicking the ? on the Standard Toolbar). If a table is open,
clicking the assistant gives Table-information. You can also type search terms. Try "sort", and
look at the explanation of sorting in Datasheet View.
Type 'help', and look at 'ways to get assistence' , Contents and Index, and What's This?
Working in Access, pay always attention to texts appearing to warn you or explain something.
Wizards
Wizards are a great help in designing forms, reports, etc. Click in Design mode on the magic stick
on the task bar to activate wizards. In this manual, sometimes the method without a wizard is
explained as well, for better understanding.
Open, study and close the other tables, more than one can be open at the same time.
Do you understand why there exists a separate table BOOK/AUTHOR and not a separate table
BOOK/PUBLISHER?
1. Table design
Close all tables and open BOOKS by clicking on Design. (With the icon at the left of the table you
can switch between modes. )
In Design mode the table is defined. For each field of the table, the field name, datatype and an
optional description is entered here. Click in the columns and look at the information appearing
below. Find info about the distinct datatypes.
A key icon before the field name denotes the key field(s). The key toggle is on the task bar.
Each field has Field Properties (below). Clicking them gives information at the right. The available
properties depend on the datatype.
Click in an empty place for a Field Name and turn the wizard on. Chose a field name of the list
and look what happens. Delete the field again.
The whole table has also Properties, visible if you click the hand icon on the toolbar or click right
mouse key. The default sort order can be given here, try it. Somewhat cumbersome: you have to
give table + field name here, for instance BOOKS.Title.
Look at the design of BOOK/AUTHOR, what should be the key here? Find out how to make it.
2. Designing a new table.
A library has readers who lend books, so you will create a table for them. Go back to the
Database window, Tables, and choose Create in Design View. Create a simple table READERS
(ReaderID, ReaderName). Choose for ReaderID the type AutoNumber.
Define the key of the table and save it.
Enter some names in the table.
Create a table for the LENDINGS, in which can be noted who (ReaderID) lends which book
(ISBN) when (L_Date). Think about the appropriate data types and the key (several possibilities).
Give L_Date an appropriate format and input-mask (Properties )
ReaderID and ISBN denote relationships with the other tables, but as such are not informative.
Change these fields in LOOKUP fields with a combobox (see below). Then, looking at the
LENDINGS table, the name of the reader (c.q. title of book) is visible and inserting a new lending
is easy as the available names/ titles are visible.
In the recent Access versions you choose for the lookup fields the datatype lookup wizard,
chooses the key field and the field you want to see and hide the key thereafter.
Older versions:
The datatype for a reference to an autonumber field is always a number, long integer.
Click the Lookup tab and change textbox in combobox, make ROW SOURCE the table
READERS, click on … and make a query by doubleclicking the fields ReaderID and
ReaderName. Close and save the query. Properties: Bound Column 1 (the referencing key
ReaderID), Column Count 2 (ReaderID and ReaderName).
Save and look at the result in Table View: the number is visible, but the name only if you click the
field.
To hide the number, change the properties in Design view: Column Widths 0cm; now the first
field is invisible.
Using this table in Forms of Reports, the lookup effects are maintained. Note that hiding the
referencing key is not always useful: two readers with the same name cannot be separated.
So displaying a combination of fields (number and name, full name (combine fields into one, see
QBE)) is often necessary.
Make also a LOOKUP field for the ISBN field.
3. Relationships between tables
Get Help about relationships. Click the icon for the Relationships window, then Relationships Show Table, and click on your new tables to let them appear in the window. Then click Edit
Relationship and define the necessary relationships between pairs of tables. (or click a key field
and drag the mouse to the corresponding referencing key). Choose Enforce relational integrity,
the referencing key is then checked by inserts or updates. Leave the checkboxes unchecked, the
other tables relations have the same features.
Demo relational integrity: Close and save relationships, open both AUTHORS and
BOOK/AUTHOR (make them smaller). Try to change an AuID in BOOK/AUTHOR (in existing or
not-existing AuID), leave the field so the change will be saved - what happens? Try then to
change an AuID in AUTHORS, what happens? Is it possible to delete a BOOK/AUTHOR record
(the cross on the task bar)? An AUTHOR? Click Edit – Undo or close the table if you get stuck.
Close the tables and change the relation between AUTHORS and BOOK/AUTHORS, choose
Cascade update related fields, and Delete cascade. Demo: open both tables again. Change an
AuID of AUTHORS: what happens? Try to change an AuID in BOOK/AUTHORS. Try to delete
an AUTHOR. Do you understand what's happening?
4. Forms - 1
Forms are the user interfaces for an application, for instance to enter new data, or to read data.
Start designing a Form for the table READERS.
In Database view, go to the tab FORMS, choose New. Use the wizard for a form for the table
READERS, both fields. Choose a columnar form, have a look at the other layout-possibilities as
well. Choose one of the styles and save the Form READERS.
Open Form READERS and enter a few additional records
5. Form – 2
Make also a form for LENDINGS.
There are many possibilities to change the form, try some. Open the form Lendings in Design
view. Change the label ISBN in "Book", and ReaderID in "Reader". Clicking (double) a textbox
gives writing access.
Try moving the objects: change the order of book and reader data. Clicking the data field makes
moving both label + data field possible.
Entering data is easiest following the Tab Order of the fields, but changing field order requires
an adaptation. In Design view click on the Detail bar and then right mouse (RM). Change Tab
Order. Each object on the form has its own properties, reachable by RM. Too much to investigate
all, choose some that are intriguing, find info, test some possibilities (such as locking a textbox or
adding a Control Tip Text). Everything you explore now can be useful later on.
Go to the Form view of Lendings and enter a few records.
6. QBE-1 (Queries by Example) veranderd!!
Queries select raw data (from one or more tables), adapt data and summarize data.
Start with making a list of only titles and prices, a view on the table BOOKS:
Go to Database view, tab Queries, New, Design View. Add the table BOOKS, close 'Show Table'.
Doubleclick in BOOKS on the 2 wanted fields (or click-drag, or use the combobox below). Look at
the result by clicking ! (run) or changing to Datasheet View. Save as TITLE_PRICE.
Next query: make a query by Design or wizard that presents the combined information of
AUTHORS and BOOKS, sorted on the book title. You need 3(!) tables now, to include the
relations, which become visible in the QBE window. Include all fields and save the query as
AUTHORandBOOKS. Look at the result of the query. Try what happens if you change an AuID which one works? Then adapt the query: make the superfluous field invisible or remove them.
Make also a simple query AUTHOR_TITLES (just that)
Try other queries (don't save, just use ! and go back to Design)
You can include a criterium (look at Help), for instance with BOOKS only Publisher 1, and/or
only expensive books. It is not necessary to show a field, even if it contains a criterium.
You can rename a field: in the first field of the column, place the chosen name in front, a
colon and the original field name: Author: AUTHORS.AuName.
7. QBE - 2
Calculated field values:
Make a query BOOKS_FULLPRICE: consider Price as the netto price and add add to the
attributes of BOOKS two more columns, for the tax and the full price. in the first field of the
columns, give the following text. First the fieldname (default: Expr:), then after the colon the
calculation.
BTW: 19*Price/100
Bruto: Price + BTW
Formatting to Euro's in Field Properties.
Use the virtual table AUTHORandBOOKS and make a query AUTHOR_NUMBERofBOOKS,
giving a Summary of number of books for each author(name). Click the sum-icon on the toolbar,
group AUTHORS and choose COUNT in the Title column.
Make a query TOOLATE, telling which READER (Id, Name, ..) has which books (author,
title) more than 3 weeks in possession. Go to a new field and use the wizardstick (task bar) to
make your Date/Time related calculation:
In_Poss:
(the function Date() gives the system date, an interval (day, week etc) has to be given as a
string: "d")
and add the appropriate criterium to this field.
8. Report -1
Reports are used to summarize and print data.
Go to Database window, Reports, New, make a report of AUTHOR_TITLES, per Author. As in
Forms, the layout can be changed. (changing lable text, moving textbox + label together or
separately, enlarging, etcetera. Try out till you feel comfortable with these options.
Now try to change the order of fields and the sort order: not on author but on title. Moving the
fields does not change the order (use Print Preview, at the left, to check). You have to adapt the
grouping and sorting (under View ...). Use Help when needed.
9. Report - 2
Readers who don't bring back their books get a reminder. Make a report of TOO_LATE.
Change the heading in: "Reminders"
In View …. Sorting and Grouping choose sorting/grouping on the readername, chose group
header en group footer, and keep whole group together, then the books of one reader will be kept
on one page if possible.
In the group footer, put a new tekstbox (from the toolbar or toolbox).change the title of the label in
"Total number of books and put in the textbox = Count(Titel)
Save the report, make a copy with the name REMINDERS, and edit the latter report into a set of
letters. Each reader its own letter: Properties of the group footer: Force New Page after section.
Move labels and tekstboxes, remove labels, add some text (extra labels), until you have a real set
of reminder letters ready to print and send.
10. Form -3
Next job is to create a form to enter new books. Study Help info about subforms and create a
Form for BOOKS with a Subform for AUTHORS by wizard. To show data only, you don't need to
include the table BOOK/AUTHOR explicitly (the wizard will take care of that in defining the
source) but remember the findings in 7: to enter new authors, the BOOK/AUTHOR table is the
one to change so it really should be present! The ISBN and other AuID field can be removed
from the subform. Try out if you can add a new book, a new author (only existing data!)
(NB To show relating data, only a (sub)form can be included in a form. It is also possible to pick
the subform icon and define a subform yourself from a table or query. Always the referencing key
to the main form has to be included in the source of the subform (the wizard does this
automatically), although it may be deleted of the subform itself.)
Textboxes can be changed into listbox or combobox to facilitate entering data. So make a
combobox for the AuID on your subform (then the AuName is superfluous) and for the Publisher
(of coursde this can also be done by changing the underlying table, but let's assume for a
moment you don't have access to the tables themselves): RM-click the textbox and change to...
combobox, and edit the Properties (Format and Data) like you did in a LOOKUP field in Table
Design.
To invoke a Form to add a really new Publisher or Author some event programming is needed,
we will come to that later. The alternative now is to add them first to the adhering table, and than
insert a new book.
Download