Anda di halaman 1dari 16

1(16)

Lecture 9Introduction to Access 2000

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

Lecture 9Microsoft Access

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

Lecture 9Microsoft Access

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

Admission Date Birthdate ID Year


n

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

3 Creating the Tables


You have the choice to create a new table using design view, a wizard or by entering data. The wizard is nice to test some times, it will suggest sample tables, but most often, especially when you have previous knowledge of database design, design view is more efficient. Table creation by entering data results in a table without column names and primary keys. Use design view. In design view, you enter column names, types, and other properties as textual data in a sheet as shown in Figure 4.

Almut Herzog

Lecture 9Microsoft Access

TDDB38/TDDI60 HT 2002

4(16)

Figure 4: Design View for creating a table

3.1 Creating the institution table in design view, step by step


Choose InstID as field name with data type AutoNumber. Right-click on InstID and choose Primary key from the background menu. Choose InstAbbr as next field name with data type Text. In the General-tab, adjust Field Size to a suitable value, set Format to > for converting all input characters to capital characters (pressing F1 when the cursor is positioned in the Format-field will give you the full syntax for Format), set Caption to a string that you want to display as heading for this column, make the field a required field by setting Required to Yes Choose InstName as next field name with data type Text, adjust settings in the General-tab if desired. Save the table (e.g. with Ctrl-S) and give it the name Institution.

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

Lecture 9Microsoft Access

TDDB38/TDDI60 HT 2002

5(16)

Figure 6: Institution-table in datasheet view with two records

3.2 Creating the Course-table in design view, step by step.


Choose CourseID as a field name with data type AutoNumber. Make it the primary key (right-click and set). For the other fields, follow the table and figures below. Data Type Text Field Name CourseLevel Data Type Number

Field Name CourseAbbr

. CourseName Text CourseInstID Lookup Wizard

General properties of your preference

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.

3.3 Creating the Student-table in design view, step by step.


Choose StudentID as a field name with data type AutoNumber. Make it the primary key (right-click and set). For the other fields, follow the entries of Figure 7. The birthdate (cf. Figure 7) should have a format and input mask. Experiment with validation rules. In this example, birth dates that are in the future are not allowed.

Almut Herzog

Lecture 9Microsoft Access

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).

Figure 8: General-tab of the column StudentAdmissionDate

3.4 Creating the participate-table in design view, step by step


Choose PartStudentID as a field name with data type Lookup Wizard and point out the column StudentID of table Student as the reference. Choose PartCourseID as a field name with data type Lookup Wizard and point out the column CourseID of table Course as the reference. Make both PartStudentID and PartCourseID the primary key (mark both, right-click and set). Add the column PartYear, which denotes the year in which the student first registered for the course, make it a Number field and set a reasonable default (cf. Figure 9).

Figure 9: Participate-Table with view on the properties on the column PartYear

Almut Herzog

Lecture 9Microsoft Access

TDDB38/TDDI60 HT 2002

7(16)

3.5 Creating referential integrity constraints


Before we start using our tables for real, we must make sure that referential integrity between primary and foreign keys is ensured. Choose from the menu, Tools RelationshipsThat will display a window as shown in Figure 10:

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.

4.1 Course Form


We will now create a simple form for the course table using the form wizard. In the main window (cf. Figure 2), click on Forms, then double-click on Create form by using wizard. Follow the sequence of actions below.

Almut Herzog

Lecture 9Microsoft Access

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

Lecture 9Microsoft Access

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.

4.1.1 How to get rid of displayed keys in the form


Open the Course form in design view and double click on the field (not the label) CourseInstID. This opens the Property window for the combo box CourseInstID. And now comes the cryptic part which would take ages to find in the documentation! Instead of basing the contents of this combo box on a query that returns only the institution id, we base it on a query that also returns the institution abbreviation. Switch to tab Data. Put the cursor in the field Row Source (where some SQL is displayed) and click on the -Button that appears to the right of the field. That opens a query editor as shown in Figure 16.

Almut Herzog

Lecture 9Microsoft Access

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

Lecture 9Microsoft Access

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)

4.2 Student Form with Course Information


We will now create a form that contains data from more than one table. We want to have a form that allows us to see and edit information about each student together with information about the courses s/he is taking. The form will also allow us to enter new students and assign them to courses. We use the design wizard to create the form. In the first wizard window, we choose fields from the following tables Student: All fields Participate: PartCourseID, PartYear Course: CourseName, CourseLevel Institution: InstAbbr, InstName In the second wizard window, choose to see data by student and form with subforms. In the third wizard window, choose Tabular layout. In the fourth wizard window, choose Standard style. In the fifth window, accept the suggested names Student and Participate Subform and finish. The following first look of our form is shown.

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

Lecture 9Microsoft Access

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

4.2.1 Adding a button for editing course information


From the form that we created above, we also want to add new courses. We add a button to the Student-form that opens our previously created form Course from this Student-form window. Open the Student-form in design view. If not already open, open the floating window Toolbox from the menu View Toolbox. Choose a Command Button from the toolbox (the tool tips over the icons will tell you which is the command button). Draw the button on the form. Automatically, the command button wizard will open and help you configure the button. In Categories, choose Form Operations. In Actions, choose Open Form. Click Next and identify the form (i.e. Course) that you want to open. Choose Open the form and display all records. Click Next. Choose an appropriate name or symbol for the button on the form. Click Next. Choose a name for the button widget. Click Finish. Save the form, open it in form view and check that the button works by opening the course form and adding a new course. If you now try to assign students to the new course, you will note that the new course is not available in the Participate-subform! You must refresh the Student form before this is possible. You can e.g. do this by adding another command button to the Student form that has the category Form Operations and action Refresh Form Data. In a real application, this is probably a little cumbersome but for us this is okay. For programmers: Check out the code that is generated for the buttons and make something nice with it. More tips also on the labs web page of the course.

5 Reports and Queries


In this section, we will first create a simple report that displays data from multiple tables. In the second section, we will create a report based on user-input.

5.1 Simple Report


In this section, we will create a report that displays courses and their students, i.e. the user wants to see all existing courses and all students that have ever taken the course grouped by the year they took the course.

Almut Herzog

Lecture 9Microsoft Access

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

Lecture 9Microsoft Access

TDDB38/TDDI60 HT 2002

14(16)

5.2 Report based on a Query created from a Form


In a real system, a report like the one above would create an enormous document: All students that ever participated in any course! What we want to do in this section is to create a report based on supplied input data. We will now create a report that shows all students that have registered for a certain course in a certain year.

5.2.1 Create a query with simple parameters to test


So far, we have not made explicit use of Access Queries although you already have edited queries when you changed form fields. We will now create a query by switching to the Access main window and clicking on Query. You can either use the query wizard and choose the following tables and columns: From table Course: CourseAbbr, CourseName From table Participate: PartYear, From table Student: StudentLastName, StudentFirstName Choose to display all record information, not summaries. Or you use drag-and-drop in design view to make these columns part of your query. When you open the query in design view (e.g. after creating it with the wizard), the query should look like Figure 23:

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

Lecture 9Microsoft Access

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.

Figure 25: Entering parameter values prior to opening a report

5.2.2 Create a report based on the query


Create a new report using the report wizard. Instead of choosing columns from a table, you base your report on the query you created above (and not on tables!). Apart from that change, walk through the wizard and make choices that correspond to the ones you made when we created the course report. When you now open this new report in print preview, it will ask you for the CABBR and PYEAR parameter as above, and then it will display the appropriate report.

5.2.3 Create a report from a form that supplies the parameters


Entering data in the parameter value window is a quick-and-dirty way of testing your queries and reports. In a real database application, you would want your users to choose from pre-set values. That is what we will do now. We create a form that makes the user choose from a list of existing courses and years and then will display a report based on the choices in the form. This time, we create the new form in design view. Choose a combo box from the toolbox and draw it on the form. When the wizard for the combo box comes up, press Cancel. This is easier without the wizard. The first combo box is for the course abbreviation. Set a label text, e.g. Course Code. Open the Properties-window of the combo box, go to tab All and edit the following fields and values Field Name Value Name ComboCourse Format > Row Source SELECT Course.CourseAbbr FROM Course; ColumnWidths 3cm Limit to List Yes Close the properties window. Now choose a second combo box from the toolbox window and draw the combo box on the form. This combo box is for selection of the course year. Set a label text, e.g. Year.

Almut Herzog

Lecture 9Microsoft Access

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

Lecture 9Microsoft Access

TDDB38/TDDI60 HT 2002

Anda mungkin juga menyukai