Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different...

36
Access 2007 Forms

Transcript of Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different...

Page 1: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Access 2007 Forms

Page 2: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Creating Forms 2

Contents Creating Forms .............................................................................................................................. 3

Creating a new form 3 Design view and Form view 5 Creating a user-defined form 5

Changing the look of your form .................................................................................................... 6 Layout View 6 Design View 6 Moving and resizing 6 Adding and removing controls 8 Conditional Formatting 10 Changing the tab order 10 Formatting a form 11

Properties ...................................................................................................................................... 13 Using the Property Sheet 13 Form properties 13 Control properties 14

More complex forms .................................................................................................................... 17 Subforms 17 Forms based on queries 20 Forms based on multiple tables 20

Adding complex controls ............................................................................................................. 23 Combo Boxes 23 Changing Control types 26 Tabs 27 Calculated Text Box 28

User Instructions .......................................................................................................................... 29 Status Bar Text 29 Control Tips 29

Access Forms: Tasks .................................................................................................................... 30 Creating Forms 30 Exercise 1: Creating a form using the form wizard 30 Changing the look of your form 30 Exercise 2: Moving and resizing 30 Exercise 3: Working with labels 30 Exercise 4: Adding and removing controls 31 Exercise 5: Tab order 32 Exercise 6: Formatting a form 32 Properties 33 Exercise 7: Using the property sheet 33 Exercise 8: Form properties 33 Exercise 9: Control properties 33 Exercise 10: Picture control properties 34

More complex forms .................................................................................................................... 35 Exercise 11: Subform using the Control Wizard 35 Exercise 12: Subform properties 35 Exercise13: Subform using the Form Wizard 35 Adding complex controls 36

Page 3: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Creating Forms 3

Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog boxes and switchboards. In this section we will be focusing on data entry forms.

A data entry form can create a more user-friendly interface for viewing, editing and entering data than the default Datasheet view of a table. In a datasheet you see all the records together organised into rows and columns but you cannot necessarily see all the data in all the fields. A form allows you to see one record on the screen at a time and enables you to see all the data in all the fields relating to that record. A form allows you to view objects such as pictures or graphs and to create larger boxes to view data in fields such as memo fields. Forms also allow you to format your data in different ways and to organise data in a record into sections to make it easier to read.

Creating a new form

Forms can be created ‘from scratch’ in Design view. This is known as a user-defined form. A form can also be created using the form wizard. Once a form has been created using the wizard it may be customised to suit your needs.

Creating a form using the form wizard

1. From the Create tab, select More Forms, Form Wizard.

2. Select the table on which you want to base the form on in the Tables/Queries box.

3. Select the fields that you want in your form from the Available Fields list on the left:

4. Click the button to move the field to the Selected Fields box on the right.

5. Click the button to move all Available Fields to the Selected Fields box.

6. Click either the or buttons to remove fields.

Page 4: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Creating Forms 4

7. Click Next.

8. Select a layout for your form. Click on Columnar, Tabular, Datasheet or Justified. Experiment with different layouts to see what they do. Columnar is the most typical layout.

9. Click Next.

10. In the final box, type in a name for your form and then click Finish.

11. Your new form will be displayed on your screen in either Form View with the details of your first record displayed or in Design View.

Helpful Hint

Forms are usually given names beginning with the prefix frm to distinguish them from other objects such as tables, reports, queries etc.

Page 5: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Creating Forms 5

Design view and Form view

Form view is the view in which you use a form to display and edit data. You can view and customise the design of the form in Design view.

To switch to Design view in a form: Click on the Design view button on the toolbar.

To return to Form view and see the form with data displayed: Click on the Form view icon on the toolbar.

You can also use the icons on the status bar (bottom right corner of the Access window) to switch between views.

Sections

The form consists of three main sections which are marked out with a grid:

Detail This is the main part of the form which contains the form controls (see overleaf)

Form header Anything placed in this area of the form will appear at the top of each form.

Form footer Anything placed in this area of the form will appear at the bottom of each form.

Controls

Each of the fields you selected using the Form Wizard is represented by two controls in the detail section of the form, one of which is a label (which gives the name of the field) and the other of which will depend on the data properties of the field. These include:

Text boxes For Text and Memo fields

Combo boxes For Lookup fields

Check boxes For Yes/No fields

Labels are known as unbound controls because they are not linked to a data source. The field boxes are known as bound controls because they are each linked to a specific field and will display data from that field. When you create a form using the wizard, labels are transparent by default, and therefore have the same background colour and/or pattern as the form, and bound controls are white by default.

Creating a user-defined form

The easiest way to create a form is by using the wizard and then customising the form as illustrated above. However, you can also create your own, user-defined form from scratch:

1. On the Create tab select Form Design. Access will create a blank form with only the detail area visible.

2. You can now:

Resize the form as appropriate (see Changing the size of a form on page 6).

Choose a table or query as the Record Source for your form (see Form properties on page 13).

Add fields to your form from the field list (see Adding additional fields on page 9).

Move, resize and format field and label controls (see Moving and resizing controls and Formatting controls on pages 7 and 11).

Add Unbound controls (see Adding and removing controls on page 8).

Add Headers and Footers to your form (see Headers and footers on page 6).

Page 6: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Changing the look of your form 6

Changing the look of your form Once you have created a form using the wizard, you can make simple changes to it by resizing it and moving and resizing the controls on it. You can change the appearance of the form and its controls by changing the background, fonts and borders. You can also add additional elements to the form such as labels (to create headings for example), headers and footers, pictures, lines and rectangles.

Layout View

Click on the View button and select Layout View

Layout View will allow you to modify the title and rearrange the fields to suit your purpose. Click in the title box to change the form title. Click on the edge of the box to select the label and change font/colour as required.

The table fields can be arranged with larger spaces in between: Select the whole block by clicking on

the table select icon . Choose how close you want the text boxes to be, from the Control Padding button on the Arrange tab.

Design View

This view will give you more control on how your form looks: You can add additional fieds.

Moving and resizing

The form can be made larger and the controls moved around to create more space and make the form more readable. Controls can also be organised into logical groups and resized to make the data or labels more visible.

Changing the size of a form

Changing the height of the Detail section (extending it vertically)

Move the mouse over the border between the Detail and Footer sections of the form to where a double-headed arrow appears. Click and drag the border upwards or downwards to reduce/increase the height.

Changing the width of the form

Move the mouse over the right-hand edge of the form to where a double-headed arrow appears. Click and drag the border left or right to reduce/increase the width.

Headers and footers

Form headers and footers appear at the top and bottom of a form when you are in Form view and at the beginning of a printed form. Any controls placed in a header stay on the screen when you scroll down a form. Use a form header and footer to display a title for the form, instructions for using the form, or command buttons that open related forms or carry out other tasks.

Page 7: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Changing the look of your form 7

To display/remove a form header and footer:

1. Right-click on any part of the form and select Form Header/Footer.

2. New sections appear at the top and bottom of the form. Anything placed in these areas will be displayed at the top and bottom of the form.

To change the size of the header or footer sections:

Move the mouse over the lower edge of the header or footer area until a double-headed arrow appears. Click it and drag upwards or downwards to reduce/increase the size of the header or footer.

Helpful hint:

If you have created a form using the wizard, a form header and footer will already be displayed but the heights of these sections of the form will be zero, (i.e. no grid area will be created for them and only the header and footer bars will be visible). Click and drag the lower border of the header or footer bars to increase their size.

You can only add both a header and footer. If you do not want both, you can size the one you do not want so that it has no height (i.e. a height of zero).

Page headers and footers

Page headers and footers appear at the top and bottom of each printed page. They do not appear in the Form view. Page headers and footers are viewed and resized in a similar way to form ones.

Moving and resizing controls

To move, re-size, delete or format controls, you must first select the control or controls you wish to change and then apply the appropriate changes.

Selecting controls

To select a control, simply click on it. The borders of the control will be highlighted in orange to indicate that it is selected.

Header bar

Footer bar

Click and drag to create/resize the footer

Click and drag to resize the header

Click and drag here to resize the detail section

Click and drag here to change the width

Top-left handle (to move this control without its label

Side handle (to resize the control)

Page 8: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Changing the look of your form 8

Selecting a group of controls

There are two ways to select a group of controls.

Select a group of controls using the Shift key:

1. Select one control using any of the methods above.

2. Hold down the Shift or Control key and click on any other controls you wish to select. Release the Shift key.

Select a group of controls using the Marquee method:

Click on a blank area of your form and drag across to another blank area. Ensure that at least some part of each of the controls you wish to select is within this area.

Moving controls

Move the mouse over the borders of a selected control or controls until a cross with four arrows appears at the end of the cursor. Click and drag to move the control or controls to a new position.

Moving a control and its label

Labels and their field controls are automatically linked, so if you select one or other and then move it as described above, the other one will automatically move by the same distance and in the same direction. To move just one (either the label or its associated control), click on the small grey rectangle (‘handle’) in the top-left corner of the control to drag it to a new position.

Moving controls to different sections

To move a control to a different section of your form you may need to cut and paste it, rather than drag it to the new location.

Resizing labels and fields

1. Click on the control to select it.

2. Move the mouse over a handle on the side of the control so that a double-headed arrow appears. Click here and drag to resize.

Helpful hint:

You can select more than one control using the Shift or Control key as described in Selecting controls above and then resize one of the controls using the method above. This will resize all the controls by the same amount.

Aligning and sizing a group of controls

To improve the appearance of your form, you can line groups of controls or resize several controls to the same size as each other. To do this:

1. Select the controls using one of the methods above.

2. Use the Align and Size/Space options on the Sizing and Ordering group of the Arrange tab. For example, select Align and Left to line up the controls by their left margin or select Size and To Tallest to make all the controls the same height as the tallest control.

Adding and removing controls

New controls can be added to your form. These can be unbound (i.e. not containing data from your database) such as text labels or features such as lines, shapes or pictures. They can also be field controls which are bound and contain data from specific fields in the database. These are added to your form using the Controls group of the Design tab:

Page 9: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Changing the look of your form 9

Creating a label

You can create new labels to add headings to a form or to create subheadings for different groups of fields. You can also use them to add instructions for users of a form.

1. Click on the Design tab and click on Label in the Controls group.

2. Click and drag on your form to draw a label box.

3. A flashing cursor will appear in the box. Type the text you want and then click outside the box.

Editing the text in a label

You can change the text which appears in a new label you have created or a label created by the form wizard.

1. Click on the label to select it.

2. Click within the border of the label. A flashing text cursor will appear.

3. Type additional text or edit the text using the keyboard as usual.

Adding other unbound controls to a form

You can add other controls such as lines, rectangles and pictures to your form to create visual impact, divide up areas of the form etc. To do this:

1. Click on the control you want to add to your form in the Controls group of the Design tab:

Add a line

Add a rectangle

Add a picture

2. Click and drag an area on your form to draw the item.

3. For pictures, you will be asked to browse for a file location. For more details about adding pictures, see the Format properties (all sections and controls) section on page 15.

Helpful Hint:

To resize a picture while maintaining its original proportions, always use the corner handles to click and drage the picture to the new size.

Adding additional fields

If additional fields are added to the table or query on which the form is based, you may wish to add these to the form itself. The Field List is displayed in a pane on the right-hand side of the form. If it isn’t visible:

1. Click on Add Existing Fields in the Tools group of the Design tab.

2. Click and drag the appropriate field onto the form.

Helpful Hint:

To update the properties of a field control to match those of the field it is bound to, delete the existing field control and add it again from the field List. Note that this will move the field to the end of the tab order (see

Conditional Formatting

Page 10: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Changing the look of your form 10

This will allow you to set the format for a text box or combo box to change when the data in the control changes. For example, if you want to highlight any figures over 1,000 in a red font automatically.

In Design or Layout View:

Select the text box (or combo box)

Select the Conditional button on the Format tab

Specify the Condition to meet:

You can have up to 3 Conditions

Changing the tab order on page 10).

Deleting a control

1. Click on the control to select it.

2. Press the Delete key on the keyboard.

Conditional Formatting

This will allow you to set the format for a text box or combo box to change when the data in the control changes. For example, if you want to highlight any figures over 1,000 in a red font automatically.

In Design or Layout View:

Select the text box (or combo box)

Select the Conditional button on the Format tab

Specify the Condition to meet:

You can have up to 3 Conditions

Changing the tab order

When you use the Tab key to move around a form, it may miss out new fields and other controls that you have added. If you have reorganised your controls, this new order will not be reflected in the tab order.

Page 11: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Changing the look of your form 11

To change the order:

1. With the form displayed on your screen, switch to Design view.

2. From the View menu, select Tab Order. The Tab Order dialog box will be displayed (see overleaf).

3. Click on the grey Field Selector box to the left of the field name to select it.

4. Click and drag the field to the new position you require using the Field Selector box.

Helpful hint:

You can select more than one field by selecting one and then holding down the Shift key while selecting additional fields.

Formatting a form

Formatting controls

Controls can be formatted to change the appearance of the text or the control itself by using the Format tab:

1. Select the control or controls you wish to change. (To select more than one control, click on one control, hold down the Shift key and then click on any other controls you wish to select.)

2. Click on the appropriate formatting feature on the toolbar (see above) to make the changes you require.

Helpful hint:

You can only apply font formatting and alignment settings to the whole of the text in a control.

Changing the background colour

1. Click on the section of the form you wish to format.

Field Selector box

Background colour

Page 12: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Changing the look of your form 12

2. Use the Background Colour button in the Font group of the Format tab to change the colour.

Page 13: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Properties 13

Properties Each section and control on a form, as well as the form itself, has a set of properties which can be displayed and edited to change the way that part of the form will look and work. The Property Sheet, which is used to control these properties, is displayed to the right hand side of the form.

Click on the Property Sheet button on the Design tab to see the properties of whichever section or control is currently selected.

Selecting a different section or control will change the Properties window to display its properties.

Using the Property Sheet

Selection Type: indicates the type of control that is currently selected. For example, the properties window below is for a Text Box.

All properties are listed on the All tab but you can click on the other tabs to see only the relevant properties in that category.

The Property sheet can be widen or narrowed by clicking and dragging its left border.

To change properties, either click in the appropriate white box and type in the details or in some cases you can choose from a drop-down list or use the Build button .

Form properties

To select a form so that its properties are displayed on the Property Sheet:

1. Click on the Select form button in the top left corner of a form so that a black box appear in the centre of it:

Some of the more useful properties you may wish to change are given below. Most of them are on the Formatting tab but the Record Source and Data Entry ones are on the Data tab:

Property What it does Default Other options Changing the property

Record source

The table or query the form is based on.

Table or query chosen when creating the form.

Any table or query in your database.

Choose a table or query from the drop-down list.

Caption This is the text displayed in the Title bar of the form

Name of the Any text Type in a new

Select form button (form is selected)

Page 14: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Properties 14

in Form view. form one.

Default view

Determines how many records are viewed at once

Single (one record at a time)

Continuous (view all records in a continuous form)

Choose from the drop-down list.

Record Selectors

Determines if Record selectors are visible in Form View

Yes No Choose from the drop-down list.

Navigation buttons

Determines if Navigation buttons are visible in Form View

Yes No Choose from the drop-down list.

Scrollbars

Determines if scrollbars are visible in Form View

Both Horizontal Only, Vertical Only or Neither

Choose from the drop-down list.

Data Entry

Determines if form is set for data entry only (i.e. if existing records are shown when the form is opened.

No (existing records are shown)

Yes (existing records are not shown, only new records can be added)

Choose from the drop-down list.

Helpful hint:

If you change the record source of your form, the field controls will no longer work if the same fields do not exist in the table or query, which is the new record source for your form.

Control properties

Control names (all controls)

Bound controls

If you have used the Form wizard to create a form, the name of any bound controls is usually the same as the field name. However, if you add a field from the field list, it may have a different name based on the type of control it is (e.g. Combo1) so you may wish to change it to make it more meaningful. The name is useful if you wish to refer to the value in a control in formulae etc.

Labels

If you have used the Form wizard to create a form, the name of labels is usually the same as the field name with the suffix ‘_Label’ e.g. FirstName_Label

Other controls

These are usually named based on the type of control followed by a number e.g. Line2

Caption (labels only)

This is the text which appears in a Label text box. This can be changed either in the Property window or by simply editing the Label itself.

Data properties (bound controls only)

When you create a form using the wizard, or add a new field to an existing form, many of the properties of bound (field) controls will be determined by the field properties of the field they are bound to. The field properties are determined by the design of the table the field belongs to.

If you change the properties of a field in table Design View, the properties of the field control you have already created on your form will not necessarily be updated to reflect these changes. However, any restrictions placed on the data allowed in a particular field will always be applied because when you

Page 15: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Properties 15

enter data in a form it is passed to the relevant field in the table. If the data you enter isn’t allowed by restrictions in the table, an error message will appear.

Similarly, you can change some of the properties of a field control on a form and this will not affect your table design. However, you cannot make any changes to the properties of a control which would be inconsistent with the underlying data source to which the control is bound. In other words, you can change data property settings in a form to further restrict data entered but not to allow data which is not allowed by the Table design.

Details of some specific data properties are given below:

Property What it does Relationship with Field Properties in Table

Changing the property

Control source The field in the Record source to which the bound control is linked

Not applicable. Choose a field from the drop-down list. (Only fields in the Record Source are available.

Input Mask Can be used to control data entry.

Any input masks in the Table are added to the Input Mask property of the Form at the point of creation.

Type in a combination of characters (press F1 for details)

Validation Rule/Validation Text

A rule can be set to restrict data entry.

Not visibly but data entered will be restricted if these are set in the Table.

Type in a validation rule and/or text. Note that this may further restrict entry but not actually conflict with restrictions set in the Table.

Default Value The value that appears by default in any new record.

Not carried through from Table field properties.

Type in the default value. Note that because this doesn’t restrict data entry it doesn’t affect any Table field properties already set.

Locked Determines whether or not data entry is possible.

Note that if you lock a control for which the Field property Required is set to yes this will prevent the addition of new records.

Set to Yes to prevent data entry on a particular Form.

Lookup properties (combo box and list box only)

These are the same properties as are set for Lookup fields in Table design view and are carried through from the Table when creating a Form using the wizard or adding a Lookup field from the Field List. These can be edited as long as they do not conflict with the Lookup properties already set in the Table.

Format properties (all sections and controls)

Controls

Most of the format properties (under the Format tab of the Property window) determine the look of the control itself (size, colour etc.). However, you can use the Format property to determine the way dates, times and numbers are displayed for controls based on Date/Time and Number Fields and the Decimal Places property of controls based on Number fields.

Page 16: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Properties 16

Picture size mode (Pictures only)

This property determines what happens to a picture when the picture control is resized. The options and their effects are listed below:

Clip: This is the default. When the picture control is made larger than the original size of the picture, the picture remains the same size. If the picture control is reduced to less than the size of the picture, only a part of the picture will be visible.

Stretch: The picture will be stretched to fit the size of the picture control. If the proportions of the control are different to those of the picture it becomes distorted.

Zoom: The picture is enlarged or shrunk to fit the size of the control but only to the extent that it remains in its original proportions.

The Picture Alignment property determines where in the control the Picture will sit if it doesn’t fit the control exactly (e.g. Centre or Top Left). This is not relevant to Stretch size mode.

Back Colour property

If you have set a background colour for one or more sections of your form (see Changing the background colour on page 11) this can be seen on the Back Colour property for this section. The colour is represented by a serial number. Click on the build button to the right of the Back colour property box to see a list of available colours and/or select an alternative. Delete the number in the property box to remove the back colour.

Page 17: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 17

More complex forms

Subforms

When tables have related data, it is possible to create a form that will show the information on one form. A Subform is a form within a form. The primary form is called the Mainform and the form within the form is called the Subform.

The Mainform is usually created from a table that forms the one side of a one-to-many relationship.

The Subform is usually created from a table that forms the many side of a one-to-many relationship.

Example of a Mainform with a Subform using the datasheet style (the default style)

The Mainform is based on tbl_doctor and the Subform on tbl_patient. Only related patient records for the doctor currently displayed (Keynes) are visible in the Subform.

Mainform with subform using the control wizard

Subforms can be created without using the form wizard. The following method uses the control wizard to create and add a Subform to an existing form.

1. Open the form you want to use as the Mainform in Design view.

2. Ensure the Detail area of the form is big enough to enable you to place a Subform on it. Make it bigger if necessary.

3. Ensure the Use control wizards option is on (this can be found in the Controls group of the Design tab).

4. From the Controls group of the Design tab, click the Subform/Subreport button.

5. Position the mouse over the form and draw a rectangle where you want the Subform to be. The SubForm wizard will appear.

6. Select Use Existing Tables and Queries to create a new subform or Use an existing form if you already have an appropriate form. (The following steps are based on using the first option to create a new subform.)

Page 18: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 18

7. Select the appropriate table or query from the drop-down list and add appropriate fields:

8. Check that an appropriate relationship is being created between the two forms, i.e. that the correct fields are joined from each table. In this example, StaffNumber is linked in both tables.

9. In the final dialog box, type in a name for your Subform and click Finish.

Mainform and subform using the form wizard

This option creates two new forms with one appearing as a subform within the other.

With the relevant database open on your screen:

1. Create a form using the form wizard and add the appropriate fields from the table on which you wish to base your Mainform on

2. In the table/queries box, change the table in the drop-down list from the Mainform table to the one on which you want to base your Subform.

3. Select the fields you want to include on your Subform so that they are added to the Mainform fields already selected.

You should now have added the fields you want to include in both your Mainform and your Subform.

4. Click Next.

Page 19: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 19

5. Ensure the Mainform is selected in the box on the left, and the Form with subform(s) option is checked:

6. Click Next.

7. In the next dialog box, choose a layout for your Subform. Select either Tabular or Datasheet, and click Next.

8. In the final dialog box, type in names for your Mainform and Subform, and click Finish.

Viewing records in a subform

There are two Navigation bars, one for the Mainform and one for the Subform. As you scroll through your main records, any related records will appear on the subform. If you add any new records to the subform they are automatically linked to the record on the mainform. In the above example you can add patients to the subform and the StaffNumber of the relevant doctor is automatically added.

Once you have created your Subform, you can resize the Subform area to ensure that all the fields are visible. Note that in Design View the Subform will look as if it is a Columnar layout whereas in fact by default it will be in a Datasheet layout. It is possible to change the layout but the Datasheet layout tends to be the most practical for displaying multiple records.

You may also want to remove the linked field (e.g. StaffNumber from the subform as this isn’t really needed.

Subform properties

Once you have created a Subform you can view and edit its properties if necessary. These are the properties of the Subform as a control on your Mainform. Click on the border of the control to select it.

Helpful hint:

Note that you can view the form properties (as opposed to the control properties) by clicking on the form Selector box within the subform control.

The Form which the Subform displays.

The Child field: the field in the Subform which links it to the Mainform

The Master field: the field in the Mainform which links it to the Subform

Page 20: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 20

Forms based on queries

In many cases you may want to create a form based on only part of your data, for example only available doctors (where the Available Yes/No field is set to Yes) or only doctors in a certain specialism. To do this, you can base your Form on a query rather than a table when running the Form Wizard. When the Form is opened in Form View, the query is run in order to populate the form with correct dataset based on the criteria in the query.

Helpful hint

If a form has a parameter query as its Record Source, when the Form is opened in Form View, the parameter dialog box is automatically opened. This enables you to use the same form to access different datasets depending on what the user requires.

Re-using forms

Once you have created a form and formatted exactly as you wish, you may wish to re-use the form for a similar purpose but with a slightly different set of data. For example you may have a form which shows all doctors (based on the table) but you want to re-use the form to show only available doctors. To do this you can copy the form and change the record source:

1. Select the form in the database window and Copy it.

2. Click on a blank area of the database window and Paste. A dialog box will appear asking you to name the form.

3. Type in an appropriate name and click OK.

4. Open the form in Design View and change the Record Source property of the Form to the appropriate table or query.

5. Check that all the fields are displaying data correctly in Form View. You may need to change some of the Control Source properties of any bound controls if not. You may also need to add or remove Fields from the Field list as appropriate.

Forms based on multiple tables

The way to incorporate more than one table into a form or set of forms will depend on the relationships between the tables and how you wish to display them.

One to Many relationships

In a one to many relationship e.g. doctors and patients, the solution will depend on the order in which you want to view the data:

View by primary table

To view the data by the parent or primary table, the best solution is to use a main form with a subform. In the example of doctors and patients, each doctor has a separate record with their related patients viewed in a subform below. See Subforms on page 17 for more details.

View by secondary table

To view the data by the secondary table the best solution is to combine the data in a single form. This can be done by using the Form Wizard and specifying that you want to view the data by the secondary table (e.g. by tbl_patient as shown below).

You can either add fields from both tables in the Form Wizard or join the tables first using a query and basing the Form on the query.

Page 21: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 21

The resulting form shows a single record for each patient with the related doctor data displayed within the form:

One to One relationships

Fields from two tables in a one to one relationship can be added to a single form in a similar way to the above, either by adding the fields from two tables into the Form Wizard or by creating a query combining the tables and then using the query as the basis for the Form.

Many to Many relationships

This is effectively a combination of a form based on two tables and a subform. In the following example, for every one ward there are many admissions and for every patient there are also many admissions so there is effectively a many-to-many relationship between patients and wards.

Page 22: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 22

The way this is represented in a form depends on which of the primary tables you want to view the data by (patient or ward):

By ward

Create a mainform based on wards Create a subform based on patients and admissions

By patient

Create a mainform based on patients. Create a subform based on wards and admissions

This example shows the data organised by patient. The main form has a record for each patient while the subform combines admissions and ward data to display the name of the ward to which the patient was admitted (and any other pertinent ward information) rather than just the ward number. The subform can either be created based on two tables or on a query that combines them.

Data entry in forms based on multiple tables

Where two tables are combined in a single form it is important to be clear which data belongs to which entity and what can be edited. For example, in the subform above, it is possible for a user to change the WardName from Cedar to, say, Beech, mistakenly believing that this will change the admission to a different Ward whereas in fact this will rename the ward with WardNumber w1 to Beech!

To avoid this problem, it may be helpful to change the Locked property of some fields to Yes which prevents the user from being able to edit them. It may also be helpful to create a drop-down list on the field which links the two tables, in this case the WardNumber, to show related data (such as WardName) to make it easier to change the ward to the correct one if necessary.

When adding the field which links the two tables, always use the one from the secondary table. For example, when combining doctor and patient information into one form, add the StaffNumber field in tbl_patient rather than the one in tbl_doctor. That way, when the user changes the StaffNumber, they are changing which doctor the patient belongs to, rather than changing the StaffNumber of the doctor. Similarly, when combining ward and admission information in one form, the WardNumber from tbl_admissions should be added.

Page 23: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Adding complex controls 23

Adding complex controls

Combo Boxes

A Combo Box is a Control which will allow new values to be entered or a value to be chosen from a drop-down list that is displayed once the Control is selected. In many cases, it's quicker and easier to select a value from a list than to remember a value to type and it also reduces data entry errors.

The drop-down list in a Combo Box consists of rows of data. Rows can have one or more columns, which can appear with or without headings. When a particular row is selected by the user, the value in the first column is entered into the box itself. Combo Boxes can be created by either looking up the values from a Table, or by entering the values which are to be viewed in the Combo Box.

Helpful Hint

If a Control on a Form is bound to a Lookup Field, the Lookup Properties from the Table will be carried forward to the Form Control and these Properties include creating a Combo Box.

Bound Combo Boxes

If a Combo Box is bound (linked) to a specific Field, the data entered in the Combo Box is entered in the specified Field (the Control Source). The values in the drop-down list can either be looked up from a Table or Query or typed in as a Value List.

Creating a Combo Box which Looks up Values in a Table or Query

1. Ensure the Use control wizards option is on (this can be found in the Controls group of the Design tab.

2. Click on the Combo Box button on the Controls group of the Design tab.

3. Click and drag an area on the Form to draw the Combo Box.

4. The Combo Box Wizard is displayed on your screen.

5. Select I want the combo box to look up values in a table or query and click Next:

Page 24: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Adding complex controls 24

6. Click on the Tables, Queries, or Both button to display a list of Tables and/or Queries:

7. Select the Table or Query that is to provide the values for your Combo Box.

8. Select the Field or Fields that contain the values you want included in your Combo Box.

9. Specify the sort order you want for the drop-down list.

10. Set the width of the columns in the Combo Box as explained in the next dialogue box.

11. Select Store that value in this field and select an appropriate field. This should be the field name that is linked to the primary key in the table in which you are looking the information up. In this example it will be the School Number field:

12. In the final box, type in a name for your Combo Box and click Finish.

Creating a Combo Box based on a Value List

Repeat the first four steps as in the previous section and then:

1. Select I will type in the values that I want.

Page 25: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Adding complex controls 25

2. Type in the number of columns you want for your list, then type in the values you want displayed in the boxes below:

3. Adjust the width of the columns if necessary.

4. Select Store that value in this Field and choose the Field in which you wish to store the data from the drop-down list.

5. In the final box, type in a name for your Combo Box and click Finish.

Editing an existing Combo Box

Once a Combo Box has been created using the Wizard, it can be edited using the Property window.

The Property window below shows a Combo Box on a Form with the Record Source of tbl_Nurses. It looks up values from the WardNumber field in tbl_Ward and enters them in the Ward field of the tbl_Nurses.

Modifying the Row Source

Preventing Duplicate Values

To stop duplicate values being shown in the Combo Box:

1. View the Properties for the Combo Box.

2. Under the All tab, click on the Row Source Property. The Build button will appear.

3. Click on the Build button to view the Row Source in the Query Builder window.

4. In the Query Builder window, double-click on the grey background to view the Properties window (see the diagram below).

5. In the Query Properties window, set the

The Combo Box is based on a Table or Query. (Alternatively, this could be set to Value List.)

This defines which fields are used in the drop-down list. If the Row Source is a Value List, the list will be displayed here. See

Modifying the Row Source below on how to edit the Row Source.

Field where the value is stored.

Page 26: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Adding complex controls 26

Unique Values property to Yes.

6. Close the Query Builder window and click Yes when you are asked if you want to save the changes.

Helpful Hint

You can also use the Query Builder to change the sort order of your drop-down list or add additional fields. If you do the latter you will need to adjust some of the other properties such a Column Count and Column Widths.

Adding Values to a Value List

1. Click on the Build Button of the Row Source Property as shown above.

2. Type in the values that you want to add or remove.

3. Click OK.

Changing Control types

Controls can be changed from one type into another. For example, Text box, Combo boxes and List Boxes can each be changed into the other two and Check boxes, Toggle buttons and Option button are all interchangeable in a similar way.

Lookup Fields

Just as Yes/No Fields are check boxes by default, Lookup Fields are Combo Boxes by default. These can be changed to List Boxes which show all the available options at all times on the Form (as shown right). This would be advisable only if you have enough room on the Form and usually works best where there are only a few options on the Value List.

Yes/No Fields

When creating a Form using the Form Wizard or adding a Field from the Field List, a Check Box is the default Control type for a Yes/No Field. Option Buttons are an alternative but Toggle Buttons are less clear and not advisable.

To change a control type:

1. Right-click the control and select Change To

2. Select the appropriate control type from the second menu which appears. Unavailable options will be greyed out.

Page 27: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Adding complex controls 27

Tabs

You can use a tab controls to give your form more than one ‘page’ that can be tabbed through. This is particularly useful where some controls are not used very often, you wish to organise them into groups for different purposes or where having all the controls on one ‘page’ would be cluttered. One particular example might be to put the subform on one tab and the mainform on another.

Adding tabs to an existing form

You can reorganise the controls into tabs on an existing form by first creating the tab control and moving the controls onto it.

1. Click on the Tab Control button on the Controls group of the Design tab and click and drag on the form where you wish to place the tab control. Note that this should be on a blank area of the form. A tab control will be created with two tab pages.

2. Select a control or group of controls and Cut and Paste them onto a Tab page:

Select the control and click on Cut on the toolbar

Select the appropriate Tab page

Click on Paste on the toolbar

3. Once you have moved all your controls onto the pages of your tab control you can move the whole tab control to the appropriate part of your form and resize the form accordingly.

Helpful Hint

Note that click and drag will not work and that you must select a specific tab page by clicking on the tab page label. The image below shows a tab control with a specific tab page selected (named Main Details). Note the dotted line around the tab page name and the border around the content of the page.

Editing a tab control

To change the text on the tabs, change the Caption Property of the Tab page

To change the order of the pages, right click on the Tab Control and select Page Order.

To add additional pages, right-click on the Tab Control and select Insert Page.

To delete a page, right click on the Tab Control and select Delete Page. Warning: this will delete all the controls on the page as well!

Helpful Hint

The Tab Order on each page will be the same as the Tab order of the controls before they were moved onto the tabs except that, when you reach the last control on a page, pressing the Tab key will take you to the caption of the tab and then pressing Tab again will take you to the same tab page on the next record.*

Page 28: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Adding complex controls 28

To move to the next tab page within the same record you need to click on the caption of the relevant tab page or, when the caption of the current page is selected, press the CTRL and Tab keys simultaneously to select the next tab.

*Assuming you have the Cycle property for the Form set to All Records which is the default. For more details see Access Help.

Calculated Text Box

When you want to display a value a form that isn’t available in the record source, you can enter an expression in a text box to display the value:

To add a calculated field to a form:

Switch to Design View of the form

Select the Text Box button from the Control category on the Design tab

Click the position on the form where you want to add the text box.

Type in the expression you want to evaluate: precede the expression with an = sign: Examples:

=date() Will display today’s date on your form

=[Cost Per Day]*0.2 Will display the VAT amount payable for the field Cost per Day, on your form

Page 29: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

User Instructions 29

User Instructions There are a number of ways to guide users in the way that they view, edit and enter data in a Form. For example:

Highlight important data by changing the Font of the Field Controls.

Highlight Fields that should be filled in by changing the Border of the Field Controls and re-ordering these Controls to the top of the Form.

Create additional Label Controls on the Form for instructions or additional information.

Apply Conditional Formatting to a Field

Add Calculated Fields.

It is also possible to add information about specific Fields so that it appears in the Status Bar or to add a Control Tip to a Field so that it appears when the user hovers the mouse over that particular Control. Details of how to do this are given below.

Status Bar Text

If you add Descriptions to the Fields of a Table in Design View, these are automatically added to the Field Control when you create a Form using the Form Wizard or add Fields from the Field List. This appears in the Status Bar Text Property of the Control in Design View. In Form View, this text appears in the Access status bar when the user navigates to the relevant Field Control:

Status Bar Text can be added or amended by clicking in the Status Bar Text Property in the Property Sheet and typing in the required text.

Control Tips

These can be added to a Control by typing the text you wish to appear in the Tip in the ControlTip Text Property of the Control. The text will then appear in a pop-up Control Tip when the user of the Form hovers their mouse over the Control in Form View (as show for the Specialism Field Control below):

Helpful Hint

This type of user instruction is not very useful for users who navigate a Form using the keyboard!

Status bar text for Home Phone No. field

Page 30: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Access Forms: Tasks 30

Access Forms: Tasks

Creating Forms

Exercise 1: Creating a form using the form wizard

1. Open tbl_Employees in Datasheet view and observe the types of Data stored. Note any combo boxes (drop-down lists) and check boxes. Close the Table.

2. Create a Form based on tbl_Employees using the Form Wizard:

include all Fields except Telephone and Date_Left

choose a Columnar layout

name the Form frm_Employees

3. Navigate through the records in Datasheet view.

4. Switch to Design view to view the design of the form.

Changing the look of your form

Use frm_employees for these exercises.

Exercise 2: Moving and resizing

1. Increase the width of the Form to 24 cm.

2. Re-arrange the Controls on the Form so that they appear in three columns as below. Keep the Labels with their associated bound Controls:

Staff_ID Nationality Job_Title

First_Name Ethnicity Dept_ID

Last_Name Disability Notes

Date of Birth (DOB)

Tip: try using the Shift key to select the controls in groups and move them together.

3. Select both the Notes and Job_Title text boxes and resize them to the width of the Job_Title text box using the Size To Narrowest option.

4. Check the results of your changes in Form view.

5. Return to Design View and increase the size of the Form Header so the area is visible.

Exercise 3: Working with labels

1. Add a Label at the left-hand side of the Header with a suitable heading for the Form.

2. Change the text in any Labels where there is an underscore [ _ ] to replace the underscore with a space.

Page 31: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Access Forms: Tasks 31

3. Use the Marquee method (see page 5 of the manual) to select all the Controls in the Detail section of the Form and move them down by one row of the grid. (Hint: you may need to click below the controls and drag upwards.)

4. Add a Label above the first two columns with the text ‘Personal Information’ and one above the third column with the text ‘Job-related Information’.

5. Close the Form and save your changes.

Exercise 4: Adding and removing controls

1. Insert the picture logo.jpg at the right-hand side of the Form header. Resize the picture using the corner handles so that it is fully visible. See Exercise 10: Picture control properties in the

Page 32: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Access Forms: Tasks 32

2. Properties section for more details about formatting pictures.

3. Add the Telephone Field to the Form:

Use the Field List to add the Telephone Field to the Form.

Place it under the Disability Field.

Adjust the position and size of the Controls to align them with the Controls above and to the left of them.

4. Remove the Notes control.

Exercise 5: Tab order

1. Switch to Form view and use the Tab key to move around the Form.

2. Return to Design view and change the Tab Order to move between the controls of each column in turn (top to bottom), starting with the leftmost column.

Exercise 6: Formatting a form

1. Add a background colour to the Detail section.

2. Make the text in the First_Name and Last_Name text boxes bold and change the font colour to a colour of your choice.

3. Left-align the text in the Dept_ID text box and view the results in Form view.

Additional tasks

Add other unbound controls. For example, use lines and rectangles to divide up your form.

Experiment with re-organising the controls on your form using the techniques you have learnt.

The following Controls are important ones you would like users to fill in wherever possible. Change the colour of their borders to Red: Staff_ID, First_Name, Last_Name, Dept_ID

Make the text in the Date_Left Control bold so that employees who have left stand out. Test this by adding a date to the Date_Left Control for any employee.

Experiment with other formatting on your Form using the Formatting toolbar.

Page 33: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Access Forms: Tasks 33

Properties

Use frm_employees for these exercises.

Exercise 7: Using the property sheet

1. View the Property sheet and answer the questions below about the properties of the corresponding controls and sections:

What is the Control Source of the Last_Name text box?

What is the Record Source of the form?

What is the Height of the Form Header?

What details are on the Data tab of the First_Name label? Why?

Exercise 8: Form properties

Change the Caption property of the form to Employees.

View the change in Form View.

Change the Default view from Single Form to Continuous Forms

Switch to Form View and scroll down the Form to view the result.

Change the Default view back to Single Form.

Exercise 9: Control properties

1. Open the Table tbl_Employees in Design view. Make a note of the specified Field Properties in the table below:

Field Property Setting

Staff_ID Format

DOB Caption

DOB Format

Nationality Default Value

Last_Name Required

Ethnicity All on the Lookup tab (no need to write all these down!)

2. Close the Table.

3. Open the Form frm_Employees in Design view.

4. View the Property sheet.

Keep the Property sheet open for the rest of this task.

5. View the same Properties as in 1. above for the corresponding Form Controls and compare the Form Control Properties with the Table Field Properties. Which Properties have been carried across to the Form design?

6. Switch to Form view and go to a new record. Notice the Default Value in the Nationality Field.

Page 34: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

Access Forms: Tasks 34

7. Change the Default Value Property of the Nationality Field to “U.K.” (include the double quote marks [“]). Note the results of the change you have made in Form View.

8. Change the Format Property of the DOB text box to Long Date and view the changes in Form view. Resize the text box to fit the data.

9. Enter a new record in Form View, leaving the Last_Name field blank. What message appears? Why?

Exercise 10: Picture control properties

Note that you can only do this Exercise if you have added a picture to your form. See 0 Exercise 4: Adding and removing controls

1. Check that the size mode property of the picture is Clip (this is the default).

2. Click and drag on the edges of the border of the picture control (not the corners) to make it larger and smaller. What happens to the image?

3. Change the size mode to zoom and repeat step 2 above. Do the same for stretch size mode. Notice what happens to the image.

Additional Tasks

Experiment with changing the following properties and test the results as described:

Property Setting Check the results in Form View

Form Srollbars Record Selectors Navigation buttons Data Entry

Neither No No Yes

View the difference in the look of the form for each change

First_Name Locked Yes Change the First Name in the first record.

Last_Name Input mask >L<??????????????? Edit the Last Name of any employee

Don’t save the changes you have made.

For any other Properties you are interested in, click in the Property and press F1 on the keyboard for more details.

Page 35: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 35

More complex forms

Exercise 11: Subform using the Control Wizard

1. Add a Subform to frm_Employees based on tbl_Salaries:

Ensure the Control Wizard is switched on

Add the Subform below the existing Controls on frm_Employees

Create a new Subform

Include the all the Fields from tbl_Salaries.

Name the Form frm_Salaries_subform

2. View the Form in Form View and view the Salary records in the Subform. Switch between the two views, adjusting the size of the Subform until all Salary records are fully displayed.

Exercise 12: Subform properties

1. View the Properties of the Subform control on frm_Employees. Which Fields are linked?

Subform Field:

Mainform Field:

2. Change the text of the Subform Label to read ‘Salary Details’.

3. Add a new salary record to the employee with Staff_ID 00001 as below:

Salary_Date: 01/04/03

Salary: 25000

4. Open tbl_Salary in Datasheet view to check that the correct Employee_ID has been added.

Exercise13: Subform using the Form Wizard

1. Create a Mainform with Subform using the Form Wizard:

base the Mainform on tbl_Departments and add all Fields

base the Subform on tbl_Employees and add Staff_ID, First_Name, Last_Name, Job_Title and Dept_ID Fields.

choose Datasheet style for your Subform.

name the Mainform frm_Departments

name the Subform frm_Employees_subform

2. View the Form in Form View and view the Employee records in the Subform. Switch between the two views, adjusting the size of the Subform until all Employee records are fully displayed.

3. View the Properties of the Subform. Which Fields are linked?

Subform Field:

Mainform Field:

Page 36: Access 2007 - University of Exeter · 3 Creating Forms Creating Forms There are many different types of forms that can be created in Access. These include data entry forms, dialog

More complex forms 36

4. Why is it not necessary to include the Field Dept_ID in the Subform? Switch to the Database window and open the Subform frm_Employees_subform in Design View.

5. Remove the Dept_ID Field from the Subform. Close the form.

6. Open frm_Department in Datasheet view to view the results of your changes. Switch to Design view to resize the Subform if necessary.

7. Change the text of the Subform Label to read ‘Employees in this department’

8. Add a new Employee with Staff_ID 50 and Last_Name Smith to the Department with Dept_ID of 500. Open tbl_Employees in Datasheet view to check that the correct Dept_ID has been added.

Adding complex controls

Use frm_employees for these exercises.

Exercise 14: Adding a bound Combo Box

Notice that the Dept_ID Control is a Text Box. You will replace the Text Box with a Combo Box which displays the Dept_Name but will still store the Dept ID in the Dept_ID Field.

Delete the Dept_ID Text Box and its associated Label.

Add a new Combo Box to display Dept_ID:

Ensure that the Control Wizard on the Toolbox is on

Add a Combo Box from the Toolbox

Use the Combo Box Wizard to create a Combo Box which will display the Dept_Name Field but is linked to the Dept_ID Field

Ensure that data entered in the Combo Box will be stored in the Dept_ID Field

Sort in order of Dept_Name Give the Combo Box the Label ‘Department’