TABLE OF CONTENTS 1 2 3 INTRO............................................................................................................................................................ 2 MINI WORLD .............................................................................................................................................. 3 CREATING THE TABLES ......................................................................................................................... 3 3.1 3.2 3.3 3.4 3.5 4 CREATING THE INSTITUTION TABLE IN DESIGN VIEW, STEP BY STEP ......................................................... 4 CREATING THE COURSE-TABLE IN DESIGN VIEW, STEP BY STEP. .............................................................. 5 CREATING THE STUDENT-TABLE IN DESIGN VIEW, STEP BY STEP. ............................................................ 5 CREATING THE PARTICIPATE-TABLE IN DESIGN VIEW, STEP BY STEP ........................................................ 6 CREATING REFERENTIAL INTEGRITY CONSTRAINTS.................................................................................. 7
CREATING FORMS.................................................................................................................................... 7 4.1 COURSE FORM ......................................................................................................................................... 7 4.1.1 How to get rid of displayed keys in the form ................................................................................... 9 4.2 STUDENT FORM WITH COURSE INFORMATION ....................................................................................... 11 4.2.1 Adding a button for editing course information ............................................................................ 12
REPORTS AND QUERIES ....................................................................................................................... 12 5.1 SIMPLE REPORT ..................................................................................................................................... 12 5.2 REPORT BASED ON A QUERY CREATED FROM A FORM ........................................................................... 14 5.2.1 Create a query with simple parameters to test .............................................................................. 14 5.2.2 Create a report based on the query............................................................................................... 15 5.2.3 Create a report from a form that supplies the parameters............................................................ 15
Almut Herzog
TDDB38/TDDI60 HT 2002
2(16)
1 Intro
When you run Access on any other than an IDA-PC, make sure that the installation of Access included also the wizards (at least in Access97, this was optional). Otherwise, you will not be able to follow this tutorial. You can do the labs using Access97 as well. The main windows when starting access look somewhat different but the wizards and the very important property windows look reasonably the same. Note that an Access2000 database cannot be opened in Access97, but vice versa works. When you first start Access, choose to open a blank database in the first window that is displayed on start-up.
Figure 1: Opening a blank database Access 2000 after start-up: This is the main window from which you will reach all the objects that we create within Access.
Figure 2: Access 2000 start window First, you will need to create tables. They are the basic modules you needalso in Access when you build a database application.
Almut Herzog
TDDB38/TDDI60 HT 2002
3(16) Later, you will learn about Forms as GUIs for entering data into tables, and Reports for showing data from tables. The strange thing is that you can go a long way in Access without explicitly working with Queries! In this lecture and also the labs, we are not dealing with Macros or VisualBasic. If you are interested, you are encouraged to read about that yourself. The labs can be done without both.
2 Mini World
In this lecture, we will create a database that implements the ER-diagram of Figure 3.
FirstName ID LastName
Student
n
participate
m
Abbreviation
1
Course
ID Abbreviation Level
held by
Institution
Name
Name
Figure 3: ER-diagram In very short words, we have students that participate in courses that are held by institutions. We need to implement the following tables (primary keys underlined, foreign keys in italics): Institution Course Student Participate InstID CourseID StudentID InstAbbr InstName CourseName CourseLevel CourseInstID StudentAdmissionDate
CourseAbbr StudentLastName
StudentFirstName PartYear
StudentBirthdate
PartCourseID
PartStudentID
Almut Herzog
TDDB38/TDDI60 HT 2002
4(16)
Figure 5: Institution-table in design view, switch to datasheet view from tool bar Open the table in datasheet view and enter a few records (cf. Figure 6). Closing the window will automatically save1 the table data. Close the window.
One of the really annoying features of Access. You cannot be sure when it starts saving! Your database file on disk may change simply by you browsing (NOT editing) table data.
Almut Herzog
TDDB38/TDDI60 HT 2002
5(16)
When you choose the lookup wizard as data type, choose values from the InstID-column of the Institution-table as acceptable values for this column. The wizard guides you through that process. In the Lookup-tab, set Limit to List to Yes so that only values from the combo box are allowed.
Almut Herzog
TDDB38/TDDI60 HT 2002
6(16)
Figure 7: Student table with all columns and view on the general tab of the birthday column The student admission date should default to the current date and be a required field (cf. Figure 8).
Almut Herzog
TDDB38/TDDI60 HT 2002
7(16)
Figure 10: Relationships between tables Note that this diagram is not an ER-diagram although it has some similarities. Now double-click on each of the connecting black lines between foreign keys and primary keys. After a double-click, an Edit Relationships-window appears (cf. Figure 11). You should then enforce referential integrity by checking the check boxes as shown below. Now, when an institution is deleted, all depending courses will be deleted.
Figure 11: Enforce referential integrity between Institution and Course. Make equivalent changes for the other two connections.
4 Creating Forms
So far, we have used the table in datasheet view for entering data into our database. However, there is a more advanced way of doing this. The GUI for entering data is called a form.
Almut Herzog
TDDB38/TDDI60 HT 2002
8(16)
Figure 12: First wizard window In Figure 12, choose the table that you want to create a form for, in our case Course, then choose all the fields so that they are moved from the Available Fields to Selected Fields. In the figure, this is already done. Press Next. In the second wizard window, choose a layout form (Columnar is our choice). Press Next. In the third wizard window, choose a style (Standard is our choice). Press Next. In the fourth wizard window, choose a name for this form (Course is fine), and keep the option Open the form to view or enter data. Press Finish. A window as in Figure 13 appears. It contains data from our Course table. You can browse through the entries with the buttons on the bottom part of the window. Note that the shown labels contain the strings that we chose in the Caption field when the created the table.
Figure 13: Form for the Course table as created by the form wizard We can now adjust the layout of the form by entering the form in design view, e.g. by pressing the left-most button on the tool bar.
Almut Herzog
TDDB38/TDDI60 HT 2002
9(16)
Figure 14: Course form in design view Adjust the length of fields by dragging their borders to an appropriate size. Prevent users from editing the primary key by (a) right-clicking in the CourseID field and choosing Properties from the popup menu or (b) by double-clicking on the CourseID field. Properties are your way of managing forms. Get used to the ugly display of information and the sparse information. This is the place where you do all the important changes to a form. Disable the CourseID field by setting the property Enable (in tab All or Data) to No. Close the Properties-window, save the form and open it in form view. It now looks like Figure 15.
Figure 15: Course form after changes to field sizes and disabling of entering data into the primary key field. Now, the form looks quite reasonable. However, to enter new courses, you have to remember the primary key of the institution ID. This is something which your users will not like and which the lab assistants will not accept in your mini project! The next section explains how to turn the CourseInstID field into a combo box that contains textual information, i.e. the institution abbreviation instead of the institution id.
Almut Herzog
TDDB38/TDDI60 HT 2002
10(16)
Figure 16: Query editor for showing the institution id Drag the text InstAbbr from the upper part of the window to the empty column on the lower part of the window so that it appears next to InstID.
Figure 17: Addition institution abbreviation to the query Close the query builder and save the query. You are now back in the Properties window for CourseInstID. In tab Data, make sure that Bound Column contains a 1 (digit one) if InstID is the first column returned from your query. Switch to tab Format. Change the value for Column Count from 1 to 2 (our query returns now two columns not one). Change the value for Column Widths from something like 2.54cm to 0cm;3cm (meaning that you do not at all want to display the first column (0cm) but the second column with a width of 3cm). This was straightforward, right? Close the Properties-window. Change the label of CourseInstID to e.g. Responsible Institution. Save the form and display it in form view (cf. Figure 18).
Almut Herzog
TDDB38/TDDI60 HT 2002
11(16)
Figure 18: Form view of the Course-form with textual information about the responsible institution (instead of numerical primary key)
Figure 19: First form view of Student-form Now edit the form in design view: Disable the StudentID-field. Adjust the length of some fields and labels if necessary. Do not display the PartCourseID but the course abbreviation. Open the properties of the field PartCourseID. Change the query to include not only Course.CourseID but also Course.CourseAbbr. Follow the subsequent steps from above (4.1.1 How to get rid of displayed keys in the form) by checking andif necessaryediting the properties Bound Column (new value: probably 1), Column Count (new value: 2), Column Widths (new value e.g.: 0cm;3cm).
Almut Herzog
TDDB38/TDDI60 HT 2002
12(16)
Save the form and display it in form view (cf. Figure 20)
Figure 20: Form view for Student-formno more keys in the course subform
Almut Herzog
TDDB38/TDDI60 HT 2002
13(16)
In the Access main window, choose Reports. Click on Create report by using wizard. In the first wizard window, you have to choose which columns from which tables you want to display on your report (just as on a form). Choose the following columns: From table Course: CourseAbbr, CourseName From table Participate: PartYear, From table Student: StudentLastName, StudentFirstName Click Next. View data by Course. Click Next. Choose a second grouping level on the year the students to the course. Choose PartYear as an additional grouping level. Press on the > button to update the report preview on the right-hand side of the window (cf. Figure 21).
Figure 21: After adding the second grouping level PartYear to the report Click Next. Order the students by last and first name. Click Next. Choose e.g. a Stepped lay out. Click Next. Choose e.g. Formal style. Click Next. Set or accept the name for the report, e.g. CourseReport. Click Finish. A report as in Figure 22 displays:
Figure 22: Simple multi-table report as created by the report wizard You can now choose to open the report to make changes to the layout, e.g. choosing a different font or size for some fields, adjusting the lengths of fields, etc. The handling of a report layout is similar to the handling of format layouts.
Almut Herzog
TDDB38/TDDI60 HT 2002
14(16)
Figure 23: Query in design view Now, make the query dependent on parameters you supply. This first step is a bit ugly but it helps you find early errors in your query design. Make the query depend on unbound variables (and when you open the query Access will ask you about the values). Enter an unused variable name in the criteria field of CourseAbbr and PartYear as shown in Figure 24. Do not use the same name as the column name. It may be recognised as a column name and the query does not work as we intend it.
Almut Herzog
TDDB38/TDDI60 HT 2002
15(16)
Figure 24: Parameterised query If you open this query, two windows are displayed that prompt you for a value for CABBR and PYEAR.
Almut Herzog
TDDB38/TDDI60 HT 2002
16(16)
Open the Properties-window of the combo box, go to tab All and edit the following fields and values Field Name Value Name ComboYear Row Source SELECT DISTINCT Participate.PartYear FROM Participate; ColumnWidths 3cm Limit to List Yes Close the properties window. Now we need a button that will open the report. Choose a command button from the toolbox and draw it on the form. Associate category Report Operations and action Preview Report. Choose to open the report CourseWithParameter. Associate meaningful names to the button. Save the form with name StudentsByCourse. Now the form is finished and looks similar to this Figure 26 in design view.
Figure 26: StudentsByCourse-form in design view We are not quite finished yet. We need to modify the query to take data from the form before the query is processed. Open the query Course Query in design view. Modify the conditions to look as in Figure 27 below. You do not need to write the brackets around the names. Access will add that automatically when you are done editing.
Figure 27: Make a query dependent on form data Save the query and close it. Open the form StudentsByCourse. Choose course code and year and display the report.
Almut Herzog
TDDB38/TDDI60 HT 2002