Section 4: Subforms and controls Part 5 Page 1 Introduction to this section What this section is about This section focuses on some of the more advanced features of forms such as creating subforms, adding calculated fields, using combo boxes and command buttons, and accessing a database through a switchboard. Outcomes, aims and objectives Sections 3 and 4 cover outcome 2 of this unit. Import data from an external source and design and use complex forms to aid the inputting of new data. The aims of this particular section are to enable you to: Create subforms. Perform calculations in a form. Add combo boxes. Add command buttons. Create switchboards. Approximate study time This section should take approximately 5 hours to complete. Other resources required For this section, you will use the following files: Patients.accdb. Summer Camp.accdb. Summer Camp2.accdb. Training Records.accdb (created in section 1). Feel Good Health Club.accdb (created in section 2). Staff Membership.xlsx. Mr Umberto Angelo.docx. Mr Jose Bothelho.docx. Part 5 Page 2 Assessment information for this section How you will be assessed When you have completed sections 3 and 4 you will be ready for assessment for outcome 2. However this unit lends itself to one holistic assessment. When and where you will be assessed Assessment will take place at the discretion of the centre. What you have to achieve Please refer to the section on assessment information. Opportunities for reassessment Please refer to the section on assessment information. Part 5 Page 3 Create subforms When working regularly with a database, forms create a user interface to present the data in an attractive and organised manner. Forms can be used for entering, editing and viewing information. Unlike a table, however, where you may have to scroll along the screen to see all the fields for one record, a form will display an entire record at a time in a much more user-friendly way. Forms can be created from fields from either a table or a query, and any data added to the form will automatically be updated in the table or the query that the form is based on, and if the table or query is updated then the form will be updated too. The following section will introduce you to subforms, which are forms within forms. This gives you the benefit of updating more than one table at a time. For example, you could have a form for the Patients Details table in the Patients database that could be used to enter or edit patients’ personal details, but you could also have a subform added to this form that would show the medical details of the patient whose personal details were shown. See example below: In order for the forms to be synchronised, that is both parts showing information about a specific person or subject, the tables or queries making up the forms must be in a one-tomany relationship. See example below: One Many It would make little sense for patient 1’s details to be shown at the top of the form and patient 5’s medical history shown below! The table making up the main part of the form must be from the ‘one’ side of the relationship, that is the table with the Primary Key field, and the table making up the subform should be Part 5 Page 4 from the ‘many’ side of the relationship. When you create a form from a single table that has a one-to-many relationship Access will automatically add a subdatasheet that is based on the related table or query to the form. This looks very like a subform but does not have the same versatility. Forms can be opened in three views: Form View, where you can view and update the actual data Layout View, where you can make design changes to the form while still viewing the data Design View, where you can customise the form by changing colours and properties and also adding objects There are several ways of creating subforms: By using the Subform Wizard to add a new subform to an existing main form. By dragging the form that you want to use as a subform from the database window into the main form when you already have two existing forms. By creating a new main form and a new subform together, using the Form Wizard. In the following activities, you will use some of these methods: first, you will create a simple form and see how a subdatasheet is automatically added, secondly you will create both forms using the Form Wizard then thirdly you will create a subform using the Subform Wizard. 4.1 Creating a form You are going to create a form in the Patients database, which will display both personal and medical details for the patients. The form will allow you to edit and update both underlying tables (Patients Details and Medical Records). 1. Open the Patients database. Before you start to create the form, you should check that a relationship has been created between the tables you will use for the forms. 2. From the Database Tools ribbon, Show/Hide group, click the Relationships command to open the relationship window. You will see that a relationship has already been created between the two tables using the PatientNo (the Primary Key) from the Patient Details table and the PatientID (the Foreign Key) from the Medical Records table. Note: that the fields do not have to have the same name. However, the data types must be the same or compatible, as do the properties. You cannot have a relationship where one Part 5 Page 5 data type is set at text and one set at number, but you can have autonumber on the ‘one’ side with the ‘many’ side set as a number. 3. Close the Relationships window. 4. Open the Patient Details table. 5. From the Create ribbon, Forms group, click the Form command. You should see the following form in Layout View. Subdatasheet Record selectors Main form Record selectors (Do not worry if your subdatasheet has a different layout from the one shown above.) You will have to adjust the column widths in the subdatasheet so that all of the data can be seen. The top part of the form displays all the details from the Patient Details table and the bottom part displays the medical records for the same patient. At the foot of the main form, you can see the Record selectors for the main form where you can move through the form records or add new records. At present, the screen above shows record 1 of 72. This means there are 72 patients in the database. This record is for patient number one who is David Donaldson. At the foot of the subdatasheet are the Record selectors for the subdatasheet where you can add or edit medical information for each patient. The screen above shows there are four records in the Medical Records table for David Donaldson. Part 5 Page 6 6. Change to Design View and note that the subdatasheet section which displays the data from the Medical Records table does not contain any details. See the screenshot below. This section is blank 7. Save this form as Patient Details Example 1 and close the form and table. Creating a main form and subform together using the Form Wizard You will create both forms using the Form Wizard from the database window. 4.2 1. From the Create ribbon, Forms group, click More Forms, and then select the Form Wizard. 2. The Form Wizard window will open to help you create the two forms. You must first select the table for the main form (the one side of the one-to-many relationship) and the fields you want to use in the form. Part 5 Page 7 3. In the Tables/Queries box, select Table: Patient Details from the drop down list. This table will be the base for the main form as this has the PatientNo as the Primary Key field. The PatientNo is the field that links the two tables in the one-to-many relationship. 4. Move all the fields from the Available Fields window to the Selected Fields window by using the button (see the example below). 5. Now change to the Medical Records table by using the Tables/Queries window drop down list. 6. Again you can use the button to place all the fields into the Selected Fields area. However, you do not need the Patient ID in the subform as this number will appear in the main part of the form. Click on this field in the right hand window and click the back button to remove it (see example below). 7. Click Next. Part 5 Page 8 8. The next part of the Form Wizard gives you options on how to view your data. Accept both default options of viewing data by Patient Details and a Form with subform (show below). 9. Click Next. 10. In the next window, accept the default layout as Datasheet. Click Next. 11. Choose a style for your form. In this example I have used Median. Click Next. 12. In the final window shown below, type Patient Details as the title for the main form and Medical Records Subform as the title for the subform. 13. Make sure the radio button is checked for ‘Open the form to view or enter information’ then click Finish. Your new form will open up in Form view. Your form should look similar to the one below. Part 5 Page 9 14. Close the form for the moment and open up the Medical Records table. Sort the table Patient ID number using the button on the toolbar. 15. Look at all the entries for patient 1. Check there are four entries. By using the form you have just created, not only can you see all these entries but you can also see the patient’s name and personal details, which are not available in the Medical Records table! 16. Go to Design View and note that unlike the datasheet that is entered automatically when you create a form this section contains details of the Medical Records table. This section contains details from the Medical Records table 17. Close the table and return to the database window. Ensure that Forms has been selected from the Navigation panel on the left. Notice that there are two forms shown, the main form and the subform. This allows you to open the subform as a form in its own right and make changes to it. Part 5 Page 10 Before you add any new information to the form, you will change the layout of the form to make it easier to use and ensure all the fields are visible. How to customise the form design is detailed below. Customising the form design After creating a form, you can go on to change its appearance to suit exactly what you need. There are many alterations that you can make. You can, for example, add text labels, lines, boxes, and command buttons. Each of these can be formatted as required, for example, change the size of text or the thickness of lines. Each item on the form is called a control. Each control has a number of properties that can be customised, as does the form itself. For example, you can set the Caption property to name the label that is to appear next to a field control on the form. Using the form's properties, you can specify whether the scroll bars, record selectors and navigation buttons are to be shown. These are just a few of the properties available. The alterations to the form are made in Design View. Some can also be made in Layout View which is a new feature of Access 2007. Under the Form Design Tools tab there are two ribbons: Part 5 Page 11 Design Arrange Sections In Design View each form has a number of sections: The Form Header section contains any controls that are to appear at the top of the form. The Details section contains all the controls that are to appear in the body of the form. The Form Footer section contains any controls that are to appear at the bottom of the form. In the screenshot above, only the footer bar for the Form Footer is visible. Before you can add any controls to this section, you will have to expand it. You do this by moving the mouse pointer on to the bottom of the section, then clicking and dragging until the section is the required size. When the mouse pointer is moved on to the bottom of the section it becomes There are also another two sections, the Page Header and Page Footer sections, which are used to display controls at the top and bottom of every printed page. When they are not Part 5 Page 12 shown, they can be displayed from the Form Design Tools tab, Arrange ribbon, Show/Hide group, Page Header/Footer command (see screenshot below). Grid Ruler Form Header/Footer Page Header/Footer Rulers and Grid The rulers appear at the top and left of the Design View window and are used to help you align controls on the form. The grid is also used to help you align the controls on the form. The rulers and the grid can be shown/hidden using the View, Ruler and View, Grid commands respectively (see screenshot above). Layouts When you create a form Access 2007 puts text boxes and other controls in guides called layouts. Layouts are either Tabular or Stacked . Stacked is normally used for a form showing single records and Tabular for lists. In the screenshot below Patient Details (the main form) has a Stacked layout and Medical Records (the subform) has a Tabular layout. In Design View and Layout View a layout is indicated by an orange grid around the controls. The screenshot below shows the Stacked layout on the form open in Design View: Part 5 Page 13 Layout selector box Control Layout Selecting controls To select a single control in a layout: 1. In Design View select the control that you want to move or remove from the layout. Note: To select more than one control, hold down the SHIFT key and then click the controls you want to move or remove. To select all the controls in the layout, click the layout selector box at the upper-left corner of the layout. 2. From the Arrange ribbon, Control Layout group, click the Remove command. 3. You can now move or remove the command. Divide one layout into two layouts 1. Open the table in Design View. 2. Hold down the SHIFT key and then click the controls that you want to move to the new control layout. 3. From the Arrange ribbon, Control Layout group, click the layout type that you want for the new layout (Tabular or Stacked ). This will create a new control layout and add the selected controls to it. Note: When selecting a field control, make sure that you click on the field rather than its label. If you do click on the label, then only the label will be selected. The label is normally to the left of the field control. To select the entire form Click the Form selector shown at the left of the horizontal ruler. When the form is selected, a black square is shown in the centre of the Form and Report selector as shown above. Part 5 Page 14 Change the Default View of a form Look at the layout of the Medical Records subform in Activity 4.1. The records are in a row, this is the Datasheet View. In Design View, select the entire form and double click the Form selector again. This will bring up the Property Sheet where you can see the Default View options (shown below). If you were to change the Default View to Continuous Forms, it would look like the example below: Customise the tab order When you change the position of controls on a form often the order you move through the form using the tab key will be out of sequence and this can make the form harder to fill in. To put the tab order into a more logical sequence, do the following: 1. Open the form in either Layout View or Design View. 2. From the Arrange ribbon, Control Layout group, click the Tab Order command Part 5 Page 15 3. In the Tab Order dialogue box, under Section, click the section you want to change. 4. Do one of the following: If you want Access to create a top-to-bottom and left-to-right tab order, click Auto Order. If you want to create your own custom tab order, click the selector for the control you want to move. Click the selector again and drag the control to the desired location in the list. 5. Click OK. 4.3 1. Still using the Patient Details database, open up the Patient Details main form in Design View. 2. If the header section is not visible, open it up by clicking and dragging the mouse just under the form header bar. Make the header section approximately 1.5 cm. Change the header of the form to Patient Personal and Medical Details. 3. If you do not have a heading in the header section you can insert one from the Design ribbon, Controls group, Label command. Select the Label and draw a text box in the header then type the text. Part 5 Page 16 4. Format the font appropriately. In my example the font is Tw Cen Mt size 20. You are now going to cut the PatientNo field from the details section of the form and paste it into the form header so that the number can be seen clearly. 5. Select the Layout Control, select both the label and text box control for PatientNo and click the Cut button on the toolbar. Click inside the header section of the form and then click the Paste button. You will now need to move the controls as they will not be in the position you want them to be in. You should move both controls to the right-hand side of the header area. 6. Format the controls as detailed below: Label control – change font to size 12 and embolden Text box control – make the background colour red using Give the control a raised appearance using Special Effects command in the Controls group. Font white, size 14, bold You will now edit the form so it looks similar to the example below. 7. Drag the width and depth of the form to an appropriate size. 8. Change the tab order to make it easier to navigate through the form. Part 5 Page 17 9. Change the default view of the Medical Records subform to Continuous Forms. 10. Use the button on the record selectors to move through the forms to see different patients. The form and subform should now look similar to the example below: 11. Keep the form open for the next Activity. Let’s now see how you can use this form. 4.4 1. Using the Patient Details main form with the Medical Records subform you will now add a new patient to the database, and input details of a visit to the medical centre. Select New Record from the Record selector at the bottom of the main Patient Details form. You should now see a blank form (this should be number 73). 2. Add the following details: Part 5 Patient No 73 Title Mrs FirstNames Rosetta Surname Valente Address1 45 Grove Cottage Page 18 Address 2 Address 3 Town Kirkcaldy Postcode K84LM Date of Birth GP Consultant Blood Group AB+ Telephone 01592743621 Next of Kin Mr Ricardo Valente Relationship Husband Tel 01592743621 3. Now add the following visit to the subform: Note that the Visit ID is set up as an auto number. Date of Visit 3/8/04 Follow up treatment Repeat prescription for HBP Payment Type Visa Amount Paid £25 Further Info 4. Add a new medical record for patient 5, Mr James Louden. Remember you can use the F5 go to key here to quickly locate Patient No 5. You do not need to add a number for the Visit ID because this has been set up as an auto number. Part 5 Page 19 Date of Visit 10/8/04 Follow up treatment Emergency bypass operation required Payment Type Cheque Amount Paid £100 Further Info The form and subform make updating data so much easier as there is no need to move between different tables. 5. Save and close the form. 6. Open the Patient Details table and go to the last patient, which should now be Rosetta Valente. Click on the + sign to show the subdatasheet for this patient. The medical centre visit you just entered into the form should now be shown there. 7. Close the Patients database. Creating a subform using the subform/subreport wizard In the following Activity we will add a new subform to an existing main form. 4.5 1. Open the database Summer Camp. This database is used in a travel agency which specialises in summer camp bookings. The database has only been set up recently and is used to track information on bookings made by each of the employees. This database has three tables, Bookings, Employees and Camps. 2. Establish the relationships: open the relationships window and you will see that there is a one-to-many relationship set up between the Employees and the Bookings. The link field names are different but both contain the same information, which is the Employee ID. Part 5 Page 20 Note: If the Relationship window is empty click on Relationships, Show Table, highlight the tables you want, click Add, and then click Close. 3. Open the Employees Form in Design View. 4. Drag the detail area of the form down to approx 13 cm and drag the width to approx 24 cm. This is to allow space for the subform. 5. Ensure that the Use Control Wizards command is switched on. 6. Click once on the Subform/Subreport command. Your mouse pointer will turn to a cross. Click and drag the shape for the subform to fill the width of the form. Part 5 Page 21 The Subform Wizard window will display: 7. Select the default option to use an existing table and click Next. 8. In the next window, select Bookings from the list. Move all the fields from the list to the selected field’s area using the using the form. button then select the Booker field and remove it button. This field contains the Employee ID and is already in the main 9. Click Next. 10. You will be asked to define the field which links the tables. Select the first option in the list, which is “Show bookings for each record in Employees using EmployeeID”. 11. Click Next, name the subform Booking Subform and click Finish. 12. Switch to Form View to see the result. 13. The default view for the subform is Datasheet View. Switch back to Design View and change the Default View property to Single Form. 14. Go back to the design of the form and resize the subform. The subform can be resized in the same way as the main form by clicking on the edge of the subform and dragging. 15. Ensure all the information can be seen in the form. Your form should now look similar to the one shown below: Part 5 Page 22 Note: the Camp field and the Pmt Method fields have drop down lists to choose from. This is because the data type used for these fields was the Lookup Wizard. 16. Change the header to Employee Bookings. 17. Save the form. Keep it open for the next Activity. Perform calculations in a form This form is also going to be used by management to check on the performance of the staff to see if they are meeting their target booking figures. You will first add a calculated control which will make it very easy to identify an employee as soon as the form is opened. The control will be used to display the full name of the employee in the header of the form for quick identification. The calculation will add both the first name and the last name field together. Because it is text strings you are adding you will use the &, not a + sign. Follow the instructions below. 4.6 1. Open the Employees Form in Design View. 2. Move to the form header area. 3. From the Form Design Tools tab, Design ribbon, Controls group select the text box and draw a text box in the right-hand side of the form header area. Part 5 Page 23 4. Click once inside the text box – remember the text box should say Unbound. This is because it is not linked to any data. The label box will now display the word Text followed by a number. 5. Delete Unbound and add the following text: =[FirstName]& “ “ & [LastName] The = sign lets Access know there is a calculation following. Field names are enclosed in square brackets [field name]. The & adds the fields together. The inverted commas with a space in between allows for a space to separate both names. Note: Field names must be typed in exactly as they have been created. For example if you typed a space in First Name, the calculation would not work. 6. You do not need the label for this control, so select the label only and delete it. 7. Change the size of the font in the new control to 18, and make it bold and red. 8. Now change back to Form View to see the result. You can now easily see the name of the employee as soon as the form is opened. Move through the forms using the Record selectors at the bottom of the page. As mentioned earlier, management wants to keep track of the bookings made by each employee to see if they are meeting targets. The subform already tells you how many bookings each employee has made from the number of records. But what if a manager wanted to know how much money each employee was bringing in from the bookings? You can perform a calculation by summing up the Cost field in the Booking subform. 4.7 Part 5 Page 24 1. Change to Design View. 2. Click inside the subform. You are going to drag out the footer of the subform to insert the Calculated control. Click and drag here for about 0.5 cm. 3. Insert a Text Box into the subform footer. 4. Type the following into the Text Box: =sum([cost]) This will sum the figures in the Cost field. 5. Click on the Properties Sheet command in the Tools group. Change the Format property to Currency. 6. In the label control add the following text: Total Cost to Date 7. Change the font size of both controls to 10 and embolden. 8. Change to Form View to see the result. As you move through each form, you will see the total amount collected by each employee. The subform will look similar to this example: Part 5 Page 25 The completed form now gives a vast amount of information on one screen. This makes it simple for management to look at the form and view information about employees and bookings without having to change screens. This type of form also simplifies the input of data. Both tables will be updated by using the form, and calculations will also be updated. 9. Add the following new employee: Part 5 datehired 31/8/09 Title Miss FirstName Beverley LastName Iwan Address 32 Great Manor Road City Perth PostCode P16 2HY HomePhone 01691 328444 email bev@aol.com Ext 561 Page 26 10. Add the following booking for Beverley: FirstName Levi LastName Jones DOB 31/12/87 Camp Coney Castle Cost £200 Pmt Method Visa Start Date 31/10/09 No of Weeks 2 When you move away from the record and move back again, the Cost field will be updated. 11. Save and close the form. 12. Close the database. Add combo boxes You can improve the effectiveness of a form by adding either a list box or a combo box. Both controls allow you to choose information from a list, which helps speed up data entry and also helps to prevent keying in errors. A list box is always open, displaying the data, so that you are restricted to choosing from the list. A combo box is a text box in which you click the down arrow to see the choices. The combo box or a list can access the data from another table, or from a value list that you key in. If the data is pulled from another table or query the box becomes a bound control. If you key in the list then the box is an unbound control. To add a list or a combo box: 1. In Form Design Tools, Design ribbon, Controls group, make sure the Use Control Wizards is selected. 2. Still in the Controls group click either the List Box Part 5 tool or the Combo Box tool. Page 27 3. On the form, click where you want to place the list box or combo box to start the Wizard. You will add a combo box to the form you created in the Patients database, using the information in the Doctor table. First, though, you will add some more data to the table. 4.8 1. Open spreadsheet Doctors and add the following entries: Name Area Mr Bryson ENT Mr Alexander Surgical Mr Dot Medical Mr Dick Surgical Mr Mercer ENT Mr Berry Surgical 2. Close the Doctors spreadsheet and open the Doctors table in the Patients database. The linked table will be updated. Close the table. 3. Open the Patient Details Form in Design View. 4. Click on the Consultant control and delete (label and text box). If necessary, rearrange the form to make room for the new combo box. 5. From the Design ribbon, Controls group, make sure the Use Control Wizards is selected. 6. Click on the Combo Box command the box to the size you want it to be. then click where you want to box to go and drag The Combo Box Wizard will now display: 7. As you are going to use the Doctors table to access the information, select the first option as shown above then click Next. 8. You will now be asked which table or query the information is coming from. Select Doctors, and then click Next. The next window asks which fields contain the data. If you choose both fields, you will see two columns of data in the Combo Box. Part 5 Page 28 9. Add both fields to the Selected Fields area, click Next. 10. Sort the records in ascending order by Name and click Next. 11. Adjust the field widths if necessary to see all the data and click Next. 12. Choose Name as the column containing the value to be stored or used in the database. 13. In the next window, select the option to Store the value in this field and from the drop down list select Consultant, as this is the field we deleted and need to replace. Click Next. Part 5 Page 29 14. In the final window name the Combo box Consultant, then click Finish. 15. Save the form and return to Form View to see the new control. The patients who were initially with this practice already have consultants allocated to them, but the new patients do not. 16. Navigate to the following patients and allocate consultants: Patient no Consultant 54 Mr Tarique 55 Dr Presley 56 Dr Swami 17. Keep this form open for the next Activity. 4.9 Still using the Patient Details form: 1. Follow the above instructions to change the Payment Type control in the subform to a Combo Box. This time you should type in the values you wish to use. These will be: Part 5 Page 30 Cheque Cash Master Card Visa 2. Remember to remove the extra label control. Save the form and change to Form View to try out the new Combo box. 3. Save and close the form. Using a combo box to find records 4.10 1. Open the Summer Camp database. 2. Open the Employees Form in Design View. Make sure the Control Wizard is switched on. 3. From the Design ribbon, Controls group, click the Combo Box command, draw a box in an appropriate space in the main form. The Combo Wizard window will open: Part 5 Page 31 4. This time you will select the third option – (Find a record on my form based on the value I selected in my combo box) – then click Next. 5. From the available fields, select FirstName and LastName. Click Next. 6. In the next window, ensure the check box is ticked to hide the key column (command). You do not need to display this. Click Next. 7. In the next window, label the combo box Name. Click Finish. 8. Move the control and label as necessary to see all the text. 9. Save the form and go to Form View. 10. In Form View, click on your Combo Box and select Carletta Lucas from the list. You will now be taken to her form. Part 5 Page 32 11. Test the Combo Box with some other names. Using this type of Combo Box makes it easy to find records by name or number. Add command buttons to a form You have looked at Combo Boxes to find records in your form, but there are also other tools you can use to perform this and other operations to simplify the use of forms. You will now add some command buttons to the header of the Patient Details form in the Patients database. Buttons are on the Design ribbon, Controls group. 4.11 1. Open the Patients database. 2. Open the Patient Details form in Design View. Make sure the Control Wizard is switched on. 3. Click once on the Command Button then drag the mouse into a small rectangle shape to the right of the Patient No in the Form Header. The Command Button Wizard will display as shown: Part 5 Page 33 Look at the different categories for the Command Buttons. Within each category, there are actions you can set the Command Button to perform. 4. The first button you will create will be used to search for Patients. This is really searching through the records, so it is in the Record Navigation category, and the action to choose will be Find Record. Click on Find Record then click Next. You will now be asked to either choose a picture or text for the button. In this case, Access is recommending binoculars. If you want to see more pictures, you can select the Show All Pictures button. Do remember, however, that although pictures look very nice, the user of the database might have no idea what the button will do, so be careful if you are using only pictures. 5. Select the Binoculars here as it is a common icon for the find function. 6. Click Next. 7. In the next window, you will name the button Find Patient. This will not show anywhere on the form, but it will hold the name in its properties for later use. Click Finish. 8. Save the form, change to Form View and try the new Command button. 9. Add two more Command Buttons to the form header. One to print the record – The Print Record action is in the Record Operation category. Do not use Print the Form, as it would print all the records. Use the printer picture for this. Name the button Print Record. One to close the form – This is in the Form Operations category, use Close Form text for this. Name the button Close Form. 10. Select all the buttons using your Shift key, then from the Arrange ribbon use the commands in the Control Alignment and Size groups to adjust the alignment and size of the buttons. Part 5 Page 34 11. Save and change to Form View and try out the new buttons. The top of the completed form should look similar to the one shown below: The form is now user friendly, easy to navigate and ideal for a user with little experience of Access. Part 5 Page 35