Step by step Exercise. Creating subforms
To learn the creation of forms with subforms included.
We are going to create a form that will allow us to see on each screen, one course and the list of students that are registered on this course. In this way, when we go to the next course the students in this course will appear automatically.
In this case we will need a form to extract the list of students, we will define this with tabular distribution so that the various rows will appear with the data of each student in each row. We also need a form to visualize the courses, and in this case as we want one course per screen we will define the distribution in columns. After this we will add to this form a subform based on the tabular students form that we created earlier. The final form will look something like this:
1 Position yourself in the Database window of the Classes.mdb database, and in the Forms tab if you are not already there.
We will start by creating the form that we will put inside the other form (the subform).
1 Double click on the Create form by using wizard option in the database window.
2 Select the Students table from the Tables/Queries box in the first wizards first screen.
3 Click on the button to add Student code.
4 Click on the button to add Student name.
5 Click on the button to add Student surname.
6 Click on Course field in the list on the left to select it.
7 Click on the button to add it to the list on the right. We will not add any more fields to the form.
8 Click on the Next button to go to the wizards next step.
9 Select Tabular option as distribution.
10 Click on the Next button.
11 Click on the Industrial option as style.
12 Click on the Next button.
13 Type the title as Students of a course.
14 Click on the Finish button.
15 Close the form.
We will now use the wizard to create the first part of the courses form, that referring to the course.
1 Position yourself in the Database window in the Forms tab if you are not already there
2 Double click on the Create form by using wizard option in the database window.
3 Select the Courses table from the Tables/Queries box in the first screen.
4 Click on the button to add all the fields to the form.
5 Click on the Next button to go to the wizards next screen.
6 Select the Columnar distribution.
7 Click on the Next button.
8 Leave the Industrial option as the forms style.
9 Click on the Next button.
10 Type the title as Courses with students.
11 Check Modify the form's design otion.
12 Click on the Finish button.
We will now move the Start date and Finish date fields to leave them to the right of the other fields and we will extend the Detail section to then add the subform.
1 First enlarge the form, position the mouse on the bottom right corner and when cursor change its form, drag it.
2 Select both controls (Start date and Finish date) maintaining the Shift key whilst you click on them.
3 Left click on the mouse when a hand appears and without letting go drag the controls to where you want them.
If you have not enough place to include the subform, you can make the form highter this way:
4 Move the cursor to the lower border of the detail section until the cursor takes the form to extend the Detail section and drag the mouse down to extend the section until the subform fits.
5 And now to save the changes, click on the button.
We will now add the subform.
1 Click on the button if the Toolbox is not open.
2 Click on the button on the Toolbar if it is not activated.
3 Click on the button on the Toolbar.
4 Position the cursor where you want the top left corner of the subform to be, then left click on the mouse and without letting go drag the mouse until the control is the correct size, then let go of the button. The subform wizard will start up.
5 Click on the students of a course form in the wizard's first screen (the form we created before). Notice how the Use an existing form option is automatically activated. We could have not defined the students of a course form before and selected the Use existing tables and Queries option, but this option would allow us less freedom at the time of defining the subform.
6 Click on the Next button.
7 Select Show Students for each record in Courses using Course Code.
8 Click on the Next button.
9 Type the name of the form as Students subform. This is the name of the subform that we have within the form.
10 Click on the Finish button.
11 Click on the button to see the result.
As both tables are related by the course code, Access has automatically linked the form with the subform, and so only those students that have the course code assigned of the active course in the principal table will appear.
You have two scroll bars for the records, the bar furthest out corresponds the form and so it allows you to move yourself along the form's records (the courses), and the other is the subform which permits is to move on the students but only of the same course.
12 Click on the button in the outer bar and you will see the course change and at the same time as the students in this course appear.
13 Click on the button on the inner bar and you will see that the course does not change and that if it passes to the next student within the same course though there are more students, we will not see this.
Legal warning: Authorised on-line use only. It is not allowed the use of these courses in companies or private teaching centres.
© aulaClic. All rights reserved. Reproduction in any form whatsoever is prohibited. April-2006.