Vous êtes sur la page 1sur 34

University of Bristol Information Services document acc-14

Access 2003: Creating


forms
Practical workbook













Aims and Learning Objectives
This course aims to provide participants with the basic skills necessary to create data entry
forms for Access 2003 databases (the document can be used with earlier versions of Access)
Please note: this course is not suitable for those using Access 2007.
The core learning objectives for this course are to:
navigate around forms, including datasheet view;
create forms using wizards;
create subforms to view and enter related data;
redesign forms to suit the way you work, including adding images and colours;
add dropdown lists to make data entry easier and to prevent typos;
create calculated fields (expressions);
filter data in forms.


Access 2003: Creating forms (acc-14)

Access 2003: Creating forms (April 2010)
2010 University of Bristol. All rights reserved.

Document information
Course files
This document and any associated practice files (if needed) are available on the web. To find
these, go to www.bristol.ac.uk/is/learning/resources and in the Keyword box, type the document
code given in brackets at the top of this page.

Related documentation
Other related documents are available from the web at:
http://www.bristol.ac.uk/is/learning/resources





Access 2003: Creating forms (acc-14)


Contents
Document information
Task 1 Opening the student database with correct security settings ............ 1
Opening Access and the students database ................................. 1
Security enhancements .................................................................. 1
Task 2 Using Forms ........................................................................................... 2
What do I need to know about forms before I start? ..................... 2
Opening a pre-existing form ........................................................... 2
Features on frmStudent .................................................................. 3
Finding data ..................................................................................... 4
Switching between Form and Datasheet View .............................. 4
Task 3 Design forms using Form wizards ........................................................ 5
Task 4 Create a subform within a form ............................................................. 7
What do I need to know about subforms before I start? .............. 7
Form, subform and form/subform join properties ........................ 8
Create a new form with subform automatically:............................ 9
Task 5 Customising a form - basics ................................................................ 10
What do I need to know before I start customising a form? ....... 10
Moving and resizing ...................................................................... 10
The Formatting Toolbar ................................................................ 11
Introducing the Toolbox and adding a label ................................ 11
Format Painter ............................................................................... 12
Selecting and formatting multiple fields ...................................... 13
Aligning and sizing objects .......................................................... 13
Task 6 Customising a form more advanced methods ................................ 15
Properties....................................................................................... 15
Adding and deleting fields ............................................................ 16
Locking fields and removing them from the tabbing list ............ 16
Changing the order of data entry (tabbing order) ....................... 17
Adding an image............................................................................ 17
Customising the Marks subform .................................................. 18
Task 7 Add a dropdown box ............................................................................ 19
Limit to List .................................................................................... 21
Disallowing null values ................................................................. 21
Task 8 Add a calculation to a form.................................................................. 22
A simple calculation ...................................................................... 22
Using the previous calculation in a subsequent calculation ...... 22
A less simple calculation! ............................................................. 23
Conditional calculations ............................................................... 23
Task 9 Using filters .......................................................................................... 24
What do I need to know about filters before I start? ................... 24
Filter By Selection on a whole field .............................................. 24
Access 2003: Creating forms (acc-14)


Filter By Selection on only part of a field ..................................... 25
Filter By Selection using more than one filter ............................. 25
Filter By Form ................................................................................ 25
Filter records by using Advanced Filter/Sort .............................. 27
Load an existing filter.................................................................... 27
Task 10 More advanced forms (optional task) ................................................. 28
Adding a tab control to a form ...................................................... 28
Creating a calendar on a form ...................................................... 28
Appendix A Backup and filters ................................................................................ 29
Backing up ..................................................................................... 29
Filters - disadvantages of Filter By Selection on multiple criteria:29
Filters - disadvantages of Filter By Form ..................................... 29
Applying a filter automatically ...................................................... 29

Access 2003: Creating forms (acc-14)







.
Prerequisites
Before attending this course you must have attended Access 2003: An
introduction, or otherwise already have a basic familiarity with Access.
This course is not suitable for those without prior experience of Access.

Introduction
Access is the Universitys recommended database product. This document covers
the basic operations required to create a data entry form with a subform.
Data used in example databases on this course is fictitious. We would remind you
that if you store personal information of individuals on a database, you should be
aware of the legal requirements under the Data Protection Act. Further information
about the Data Protection Act can be obtained from the Registrars Office.

Access 2003: Creating forms (acc-14)

1
Task 1 Opening the student database with correct
security settings
Objectives To open the student database (StudentFrm.mdb) and select the correct security
settings.

Opening Access and the students database
1.1 To open StudentFrm.mdb:
From the Start button choose All Programs / Microsoft Office 2003 / Microsoft
Office Access 2003.
Select File / Open and navigate to the C:\User\Access\Access modules\Forms
directory.
Double-click on StudentFrm.mdb to open the Database window.
Security enhancements
Access 2003 protects you against potentially unsafe Visual Basic for Applications
(VBA) code by setting the macro security level to medium for you. This means that
when you open a database, you will see two security warning messages.

Figure 1 select No on the first Security Warning message
1.1 To open a database when the security settings are set to medium:
On the first message, select No (Figure 1).
Note You may receive another Security Warning next, which states that the Microsoft Jet
4.0 Service Pack must be installed. If you receive this message, select Yes.
On the second message, select Open (Figure 2).

Figure 2 - select Open on the second Security Warning message
Note You may receive these warnings when you start Access wizards. If so, repeat as
shown here.
Access 2003: Creating forms (acc-14)

2
Task 2 Using Forms
Objectives To explore a pre-existing form in the example database.
Comments Forms are more convenient than tables for accessing and entering data. Forms let
you look at data one record at a time and can provide default formats so that data is
entered in a consistent way.

What do I need to know about forms before I start?
Question Answer
What are forms
used for?
Forms make your data easy to look at and use. They provide
an efficient way to find, edit, and add data, such as through
dropdown lists.
Are forms easy
to create?
Wizards let you make simple forms easily. More
sophisticated forms require more effort but can result in
powerful and useful facilities.
How many
forms can I have
in a database?
Any number of forms can be developed within a database to
look at or enter the data in different ways. For example, in a
student database one form could relate to personal details
(e.g. first name, last name, home address, term time
address, date of birth), whilst another form could relate to
academic progress (e.g. first name, last name, number of
projects submitted, project mark, subject, tutor) and so on.
Opening a pre-existing form
2.1 To open the pre-existing form:
In the Database window select Forms.
Double click on frmStudent.
Note This form displays fields from both tblStudent and tblFaculty.

Figure 3 Student Form
Access 2003: Creating forms (acc-14)

3
Features on frmStudent
Feature Description
Dropdown lists Subject and Title have dropdown lists. Title has values that
were specified when the dropdown was set up (these values
can easily be amended or added to) and Subject has a
dropdown list based on tblSubject. Data is selected from the list
to reduce data entry time and to ensure data consistency.
Calculations Age is a calculated field which is not included in tblStudent.
Conditional
formatting
The Sex field changes colour based on the sex of the student.
Tabs Tabs let you group related details together to save space on
the main form. You can include subforms and images on tabs
as well as crosstab forms and so on.
Links Hyperlinks link to other parts of the database.
Record view In form view only one students data is viewed at a time.
Images Images could be added for each student if you wanted and the
header includes a University crest that is visible on all records.
Buttons Buttons can be set up to do lots of things, such as open reports
and forms, or go to specific recordsin fact there is a great
deal that can be done to automate otherwise complicated tasks
by using buttons, especially when based on macros. There is a
course available to explore what buttons can do, called
Access buttons and macros.

2.2 To familiarise yourself with the frmStudent:
Read through the above table and locate the elements on the Student Form.
Click on the down arrow alongside the Subject field to display a list of subject codes
and titles.
Select any subject from the dropdown list and notice the contents of the Subject
window change to display the subject code highlighted in the list.
Try adding an unknown code, such as XYZ9 to see what happens when you try to
save the record. (Referential integrity is set, which means that you can only add
something that is already on the list.)
Change the Subject back to Q300 before continuing (you can type this manually
into the field).
Note In this instance only the Subject code is recorded in the field and the subject titles in
the list assist in choosing the right code. You will find out later how to show the
subject title on the form instead.
Move between records using the navigation buttons in the bottom left hand corner of
the window.

Figure 4 form navigation buttons
Access 2003: Creating forms (acc-14)

4
Finding data
Note Finding a record using the Find button in form view is the same process as finding a
record in table view.
2.3 To practice finding specific records in form view:

Figure 5 make sure that Look In and Match correspond to the above settings
Click on the Find button and find Chelwood Village - make sure that Look In points
to Student List so that all fields are searched (Figure 5).
Switching between Form and Datasheet View
It is possible to switch between Form View (which shows one record at a time) and
Datasheet View (which shows records in a tabular format, similar to that in table
view).
2.4 To view records in Datasheet View:
Close the Find and Replace dialogue box.
Click on the downward arrow next to the Design View icon in the toolbar.
Select Datasheet View (Figure 6) and close the window when you are finished.

Figure 6 - switching views from a form
Access 2003: Creating forms (acc-14)

5
Task 3 Design forms using Form wizards
Objectives To create two new forms, one based on tblStudent and one based on tblMark.
Comments The Autoform wizard creates a basic form, based on a table or query, without asking
which fields or layout options you want, whilst the Form wizard gives you more
flexibility to produce a more customised form.

3.1 To create a form using an Autoform wizard:

Figure 7 select AutoForm: Tabular based on the tblMark
In the Database window select Forms and click on New.
Note Selecting New gives access to more wizards than using Create Form using a
Wizard). For further information on wizards, refer to the Appendix.
Select Autoform: Tabular from the list (Figure 7) do not click OK yet.
From the dropdown list choose tblMark and click OK to create the form (Figure 8) (if
feature not installed, select Yes: if security messages appear refer back to Task 1
for correct options).

Figure 8 the Marks form
Close the form and save it with the name frmMark when prompted.
3.2 To create a form using the Form wizard:
In the Database window double-click Create form by using Wizard.
Access 2003: Creating forms (acc-14)

6
From the dropdown list that shows available tables and queries choose Table:
tblStudent (you may need to scroll up to find this on the list).

Figure 9 select the fields from the Students table as shown
In the Available Fields box, double-click on StudentNumber, FirstName,
LastName, Year, Subject and DOB in turn to move them to the Selected Fields
box, then click Next (Figure 9).
Note You can also move fields across by clicking on the field once to highlight it and then
click on the > button. If you have moved a field across that you do not want,
highlight that field and then click on the < button to move it back to the Available
Fields list.
When asked which layout you want, select Columnar and click Next.
Note If you click the various available options for both layout (above) and style (below),
you are given a preview of what the form will look like with each choice.
When asked which style you want, select Standard and click Next.
Name the form frmMyForm and select Finish to see the form you have just created
(Figure 10).

Figure 10 a very basic frmMyForm, ready to be customised
Access 2003: Creating forms (acc-14)

7
Task 4 Create a subform within a form
Objectives To create a subform within frmMyForm that shows the students marks.
Comments It is worth knowing that subforms have two sets of overall properties one for the
subform as a form in its own right and the other as a form within the main form,
which shows the link between the two forms.

What do I need to know about subforms before I start?
Question Answer
How do I create a
subform within a
form?
There are three ways to create a subform within a form:
You can use the Subform/Subreport Wizard on the
Toolbox.
You can drag an existing form, query or table from the
Database window onto an existing form.
You can create the main form and subform together by
adding all the fields for both forms using the Form Wizard.
This way, Access recognises a one-to-many link and uses
the correct fields for the main form and the subform. This is
the most straightforward way if you know beforehand that
you want a subform.

4.1 To add a subform to frmMyForm:
Close any open objects so that only the Database window is open.
Open frmMyForm in Design View and make sure that there is at least 5cm of blank
space at the bottom of the form (see the note below).
Note Add some space at the bottom by moving your mouse pointer over the top edge (not
the bottom edge) of the Form Footer until the pointer turns into a double-headed
arrow and then click and drag down.

Figure 11 you will need to be able to see both the database window and frmMyForm
in Design View
Access 2003: Creating forms (acc-14)

8
Click the Restore window button of frmMyForm window (in-between the
Minimise window and Close window buttons) so that you can see the
Database window behind it (Figure 11).
In the Database window, select Forms if not already selected.
Click on frmMark and, holding the mouse button down, drag it onto the space at the
bottom of frmMyForm and release the mouse (Figure 12) to create the subform.

Figure 12 the Marks subform
Form, subform and form/subform join properties
4.2 To view the main form properties:
Click on the square, grey box in the top, left-hand corner of the main form so that a
small, square, black box appears: the Properties box says Form (Figure 13).

Figure 13 where to click to view properties for frmMyForm
4.3 To view the subform join properties:
Now click only once on the grey square box on the subform (if you only click once,
the small, black box should not appear if it does appear, click outside the subform
and then try again) and note what the Properties box now says.

Figure 14 where to click (once) to view properties for the linkage between the main
form and the subform
Access 2003: Creating forms (acc-14)

9
In Properties, click on the Data tab box to view the contents of Link Child Fields
and Link Master Fields to see on which fields Access has linked between (Figure
15): this is based on the relationship already set up between tables.

Figure 15 the fields that the form and subform are linked on
4.4 To view the subform properties and change the source of a subform:
Click once more on the square, grey box in the top, left-hand corner of the subform
so that the small, square, black box appears and note that the Properties box
shows the word Form in its title bar (Figure 16).

Figure 16 - properties for the Marks subform
Now click away from the subform to deselect the grey box.















If you have time
Create a new form with subform automatically:
1.1 To create a new form with subform:
Start Form Wizard and use tblStudent.
Select a few fields making sure you include the students number
and name.
Change the source at the top (using the dropdown box) to Table:
tblMark, add all of the fields from tblMark and click Next.
Group by tblStudent and check that Form with subform(s) is
selected.
Click Next and choose Tabular for your layout option.
Finish off the wizard as before choosing your design options and
name the new form as frmTestSubform.
Click Finish to view your form.
Access 2003: Creating forms (acc-14)

10
Task 5 Customising a form - basics
Objectives To make the form look and work the way you want it to.
Comments Customise a form by changing the appearance of text, data and other objects.

What do I need to know before I start customising a form?
Question Answer
Can I alter the design in Form
View?
To alter the design of a form in Access 2003
you need to be in Design View.
What is the best way to learn
how to alter form design?
The best way to learn how to customise the
form is to experiment, preferably on a non-
mission-critical copy of your database.
What do I do if it all goes
horribly wrong?
If it all goes horribly wrong, remember that you
can click on the Undo icon to undo your last
action.
What if it all goes horribly
wrong but I dont notice in time
to use the Undo command?
If it all goes very horribly wrong, or if you do not
like what youve done, you can just close the
form - but remember to select No when asked if
you want to save changes. However, it is a
good idea to save the form regularly after
making changes that you do like.
Moving and resizing
There are several ways of moving fields, labels and objects (see table below).
5.1 To change the appearance of a field, label or other object:
Click on the object once so that handles (small, black squares) appear.
Try each of the actions in the two tables below by using the method suggested (you
must click once on an object to select it first).

Figure 17 Moving the field and label together (whole hand)
Action Method
To move a field and label
together (Figure 17)
Move the mouse pointer until it changes to an
open hand, then drag the box to a new position.
To move a field without
moving the label, or vice
versa (Figure 18)
Move the mouse pointer over the larger handle at
the top, left-hand corner of the field so it changes
to a pointing finger, then drag it to a new position.

Figure 18 - moving one part of the field (one finger)

Access 2003: Creating forms (acc-14)

11
Action Method
To move selected controls with
micro movement
Press the Ctrl + arrow keys to move in the
direction of the arrow.
To change the size of an
object (Figure 19)
Move the mouse pointer across the handles
until a double-headed arrow appears, then click
and drag to the required size.
To size selected controls with
micro movement
Press the Shift + arrow keys.


Figure 19 resizing a field (double-headed arrow)
The Formatting Toolbar
Note There are several ways in which you can customise your form, for example, there
are a number of useful toolbar buttons available in Design View.
5.2 To find out what the various toolbar options can do:
Hover the mouse pointer over each button to see a short description of the option.
Select a Text box (data field) and try changing the font and font size.
Experiment further on both fields and labels.

Figure 20 - formatting toolbar
Introducing the Toolbox and adding a label
5.3 To open the Toolbox (if it is not visible) and add a title to your form:

Figure 21 the Toobox
Make sure that the toolbox is visible by clicking the Toolbox icon.
On the form, move the mouse pointer over the horizontal line between the Form
Header and Detail bars at the top of the form until the pointer changes to a double-
pointed arrow.
Access 2003: Creating forms (acc-14)

12
Whilst the pointer is the double-headed arrow, click and drag downwards to create a
space between the Form Header and Detail bars (Figure 22).

Figure 22 create a Form Header above the Detail section
On the Toolbox, select the Label icon by clicking once.
Place the mouse on the screen in the Form Header space that you have just
created (the cursor should change to +A as in Figure 23), hold the mouse button
down and draw a box.

Figure 23 the cursor changes to +A
In the label box, type a suitable title, alter the size and colour of the text and resize
the box if necessary (Figure 24).
Note You will need to click outside the label box after you have entered the text and then
click back on the box before you will be able to format the text.

Figure 24 the new form header label
Note Fields and labels can be deleted by selecting them and pressing the Delete button.
If you select the field and press Delete, this will delete both the field and its label. If
you select just the label and press Delete, this will delete only the label.
Format Painter
Note To format an object so that it looks the way you want it to can take several or more
operations. To do the same set of operations to each object in turn would be
laborious. Format Painter lets you copy the formatting from one object to another
similar object.
5.4 To use Format Painter:
Click once on a data field and use the Fill/Back Color, Font/Fore Color and
Line/Border Color options to change the colours of the background, the text and
the text box in turn (Figure 25).

Figure 25 - colour and border icons
Click away from the field (somewhere on the form background, rather than on
another object) and then click back on to the chosen field once to make sure that it
is selected.
Access 2003: Creating forms (acc-14)

13
Click the Format Painter icon, and then select the next field to which you
want to apply the formatting.
To copy the selected formatting to several locations, double-click Format Painter
and then click once on each of the objects that you want to format. Click the Format
Painter once more to switch it off.
Selecting and formatting multiple fields
Note You can select a number of fields at the same time so that formatting, sizing and
alignment can be carried out in a single operation. To deselect them, click away
from the controls.
5.5 To select multiple objects if they are located together:
Move to a point above and to the right of the first object, hold down the left mouse
button and move down to a point just below the final object to be selected.
Release the left button and all the objects touched will be selected.

Figure 26 selected objects display handles as in the top three fields above
5.6 To select multiple objects if they are not located together:
Click once on the first object.
Hold down the SHIFT key (not the CTRL key as is used for similar operations in other
Microsoft applications) and click on the other objects once in turn.
Aligning and sizing objects
5.7 To align and size multiple objects:
Note All objects when moved automatically snap to grid to make positioning of objects
more uniform.
Once you have selected several fields, select Format / Align.
Align the selected fields to each other by selecting Top, Bottom, Left or Right as
your form design requires (Figure 27).

Figure 27 the Align options
Select Format / Size and make the fields uniform in size to each other by selecting
To Tallest, To Shortest, To Widest or To Narrowest as your form design requires.
Access 2003: Creating forms (acc-14)

14
Next, select Format / Vertical Spacing (at least three vertically space controls
should be selected for this option) and choose Make Equal to equalize the gaps
between the top, bottom and middle items (Figure 28).
Note If you get unwanted results, such as the fields and labels moved on top of each
other, select Undo on the toolbar and try again selecting only the fields and not the
labels.

Figure 28 the Vertical Spacing options
Try increasing the vertical space between each control.
Look at how the form looks in Form View (Figure 29).

Figure 29 how your form might possibly look though hopefully you have been a bit
more ambitious!

Access 2003: Creating forms (acc-14)

15
Task 6 Customising a form more advanced methods
Objectives To make the form look and work how you want it to.
Comments This task introduces the Properties box, which gives you fine control over how
various objects look and act.

Properties
For several of the following tasks you will need to use the Properties box. As you
become more experienced in fine-tuning the controls on your form, you will find that
it is useful to keep the Properties box displayed whilst designing your form.
6.1 To open and get to know the Properties box:
Return to Design View.
Click the Properties icon on the toolbar.
Click on one of the fields that you have formatted and select the Format tab to look
at the options (Figure 30).

Figure 30 - properties box
Click on a field that you have not applied any formatting to and see how the values
of the various options are different from the object you first selected.
Note If you do not have any unformatted fields on your form, you can insert a field in
default format see Adding and deleting fields below.
6.2 To move the Properties box to a new location:
Click on the Properties title bar (the part at the top where it says Text Box:
FirstName in Figure 30) and drag the box to a new position.


Access 2003: Creating forms (acc-14)

16
Adding and deleting fields
Note If you have forgotten to add a field when creating the form using Form Wizard, or if
you have accidentally deleted a field, you can add these fields to your form.
6.3 To add a field to your form:
6.4 Click on the Field List icon (right) to open the Field List (Figure 31).

Figure 31 the Field List box
Click on MiddleName in the Field List and drag it to the required position on the
form so that the field and label appear on your form.
Format the new field to match the other fields.
Locking fields and removing them from the tabbing list
Note For some databases, it is important that users should not be allowed to change
certain fields; a student reference number would be an example. This can be
achieved by locking the field.
6.5 To lock a field:
Make sure that the Properties box is open and click on the Studentnumber field.
In the Properties box, select the Data tab and set Locked to Yes (Figure 32).

Figure 32 set Locked to Yes
Test out the locked field by looking at Form View and trying to edit the data in the
field which you have locked.
Note You can also use the Enabled command under the Data tab to stop searching or
entering the field, but this means that you cannot then use the field for sorting.
Access 2003: Creating forms (acc-14)

17
Changing the order of data entry (tabbing order)
Note Pressing the TAB key moves from one data field to the next. However, if you have
moved fields around in design view, then the tabbing order is likely to become
randomised.
6.6 To change the order of data entry tabbing on the form:
Make sure that you are In Design View and select View / Tab Order (you will
probably have to click on the two downward chevrons to show all available options).

Figure 33 - changing the tab order
In the Tab Order dialogue box change the order of the fields by clicking once on the
grey box to the left of the field name.
Click on the grey box again and drag the field to a new position in the list.
When the order is correct, click on OK.
Note Clicking on the Auto Order button puts controls in the order in which they appear on
the form. This is often satisfactory but sometimes needs adjusting, particularly if you
have controls side by side on your form.
Adding an image
Note www.bristol.ac.uk/imagelib holds a library of images such as University symbols and
maps, which may be useful in this task.
Forms can include pictures, sounds and other objects. These can be stand-alone,
unbound objects (i.e. the same object is shown regardless of which record is being
viewed), or bound and stored in the underlying table (e.g. a photo of each student).
6.7 To insert an image onto your form:
Open a web browser and either navigate to the UoB image library at the URL above,
or search the web for specific images (remember that there are copyright issues
with many images that you will find on the web).
Right-click on the image you want to use and select Copy (Internet Explorer) or
Copy Image (Firefox).
Click in the section of the form where you want the image to appear (i.e. Form
Header, Detail or Form Footer) and press CTRL + V to paste the image.
Move the picture to the desired location.
6.8 To resize the image:
Size the image to suit and notice that the image either does not all show or that
there is a white space around the image, depending on the size you have chosen.
Access 2003: Creating forms (acc-14)

18
Select the image and open the Properties box if it is closed and click on the Format
tab.

Figure 34 Size Mode properties
Click on Size Mode experiment with the options available from the dropdown list:
Stretch shows the whole picture stretched to outline; Zoom shows the whole picture
in proportion (Figure 34).
Customising the Marks subform
6.9 To customise the subform:
If the subform is incorrectly positioned move it to a more suitable position and make
it taller so that it can show several rows of marks. Move it and size it as you would
any other control.

Figure 35 do not delete the StudentNumber field control on the subform
Warning! Do not delete the StudentNumber field control in the Marks subform. If you do, the
subform will no longer work. If you do not want to view the StudentNumber field
control, you can make it invisible from within the Properties under the Format tab.
Look at the form in Form View and make any necessary minor adjustments when
you return to Design View.
You can now redesign your subform using colours, font sizes, etc. as you did earlier
with the main form.
If you want, you can delete the labels in the Form Header and then close Detail and
Form Header together, otherwise you can format the labels its up to you.
Note You might find it easier by opening the frmMark in Design View from the Database
window, rather than trying to redesign it from within frmMyForm .

Access 2003: Creating forms (acc-14)

19
Task 7 Add a dropdown box
Objectives To add a dropdown (Combo) box to a form.
Comments A dropdown box is a very powerful facility when adding data. When you click on a
dropdown box a list of data values appears. Clicking on the value you want enters it
into the data field. Not only does a dropdown box reduce the need for typing, it also
helps to ensure accuracy and consistency of data. Combo boxes can use either
values from an existing table (generally most useful) or values you type in yourself.


7.1 To create a dropdown box containing subject codes and course names:
Make sure that you have frmMyForm open in Design View.
Open the Toolbox and the Properties box and move to convenient positions on the
screen.
On the Toolbox, make sure that the Control Wizard (wand) icon is selected
and then click once on the Combo Box icon (right).
Draw a box on your form roughly where you want the Combo Box to appear.
For this example you will get the values from another table, so leave the wizard
defaults as they are and click Next.
Note Of the other two options, the first, I will type in the values that I want, is the most
useful and lets you create a bespoke list of values that you want to appear in the
dropdown list. You can practice creating a combo box using this option in the If you
have time section at the end of this task. Find a record on my form based on the
value I selected in my combo box is less useful.
From the list of available tables, select tblSubject and click on Next.
Double-click on Subject and Coursename (in that order) so that they are added to
the Selected Fields box and click on Next (Figure 36).

Figure 36 select Subject and Coursename as above
Note Subject must be chosen first because the first field chosen is used as the value to
be stored later.
Sort by Coursename (leave the default as Ascending) and click Next.
If necessary, adjust the width of the visible column. Subject is not visible, which is
what we want, so leave the key column hidden and click on Next (Figure 37).
Access 2003: Creating forms (acc-14)

20

Figure 37 leave the key column (Subject) hidden and adjust Coursename width
Select Subject as the value you want to store on the form and from the dropdown
list select Subject as the place in which you want this value stored and click on
Next (Figure 38).

Figure 38 store the value from Subject: Subject in Student: Subject
Note The Subject field we have chosen to include in the dropdown box is the primary key
from tblSubject. The Subject field on the form in which we want to store the value is
a foreign key in the tblStudent.
The wizard now suggests a name for your new combo box. Accept the default
suggestion and click on Finish.
7.2 To test your new combo box and to tidy up some loose ends:
Click on Form View to try out your new combo box.
Check that the code in the Subject field changes when you select a new subject
from the list. If it doesnt change, then delete the combo box and run through the
wizard again, as you will most likely have missed out on one of the above stages.
Delete the existing Subject field from the form if you dont want to see both the code
and the name of the subject.
Note The combo box still updates the Subject field when you enter data using the combo
box because the control source is bound to Subject.
Change the name on the combo boxs label to Subject name.
Adjust the size, formatting and position of the combo box if necessary.
Once you are happy with the dropdown box, save the form.

Figure 39 the contents of the dropdown box are arranged in alphabetical order
Access 2003: Creating forms (acc-14)

21
Limit to List
7.3 To prevent entry of items that are not included in a combo boxs list:
In the Properties box, select the Data tab and look at the default for Limit To List.
Note The default for this combo box is set to Yes already. This is because Access
recognises that you are entering data into a field that is a foreign key, where the
relationship demands referential integrity. If you defined the contents of the list
yourself, this would default to No.
Disallowing null values
Note Combo boxes always accept null values when Limit to List is set to Yes whether or
not the list contains null values.
7.4 To check the current default and disallow null values:
Open frmMyForm in Form View and try giving a blank subject to a student.
Close frmMyForm completely and open tblStudent in Design View.
Set the Required property of Subject to Yes (Figure 40).

Figure 40 set the Required property of Subject to Yes in table Design View
Note You can only set this property from the table.
Reopen the frmMyForm and try to give a blank subject to a student.



If you have time
1.3 To practice creating combo boxes:
Using the I will type in the values that I want option, add
another combo box that enters values into the Title field on the
frmMyForm (e.g. Mr, Mrs, Miss).
Note You will have to add the Title field to the form from the Field List.
Limit the combo box to only accept items from the list.
Access 2003: Creating forms (acc-14)

22
Task 8 Add a calculation to a form
Objectives To add calculated fields onto a form.
Comments You can use expressions and functions to add calculated fields onto a form,
including many of those listed in Access query language elements (acc-4). You
must use a Text Box to create calculated fields. Do not try to use Labels to do this.
Make sure you know which Toolbox button is which.

A simple calculation
8.1 To create a simple calculated field on the form:
Open frmMyForm in Design View and make sure the Toolbox is open.
On the Toolbar, click the Text Box button once and let go.
Move the mouse pointer to where you want to add the text box, then click and
draw a small rectangle with the mouse button depressed.
Note The new text box will say Unbound as Access doesnt know what value to show.
Click in the unbound box and enter =[dob]+21 (Figure 41).
Note Calculations must always be preceded by =.

Figure 41 creating a simple calculation in a new text box
Note This uses an existing field (DOB) and a static number (21) and the calculation adds
21 days to the date of birth. (Field names are not case sensitive.)
In the label box to the left type a suitable label (changing the label text does not alter
the name of the text box it just changes what the label says).
View the form and change the date of birth to see what happens to the new text box.
In Design View change the calculation in the text box to =[dob]+[studentnumber].
Note This now adds two fields together with the calculation adding the relevant student
number to the date of birth in days.
Using the previous calculation in a subsequent calculation
8.2 To use the previous calculation in a new calculation:
In Design View click on the text box that you have just created.
Open the Properties box and select the Other tab, then look at the content of the
Name field it will be called something along the lines of Text21 (Figure 42).
Rename this field as YearPlus.

Figure 42 rename the field as YearPlus
Access 2003: Creating forms (acc-14)

23
Note It is useful to give calculated fields sensible names so that you have a better idea of
what the control is when trying to sort out tabbing orders otherwise you will see
unhelpful Access-generated control names in the tabbing order list.
Create a new text box using the Toolbox but this time enter =[YearPlus]+21 and
check the result in Form View.
A less simple calculation!
8.3 To add a calculation to a form:
Open frmMyForm in Design View and add a text box using the Toolbox.
In the text box, click where it says Unbound and enter =int((date( )- DOB)/365.25)
to calculate age.
View the changes, close and save changes to the form.

If you have time
Conditional calculations
Note You can use the function =iif(expression, true, false) where
expression is the criteria, true is what to do if the criteria is true
and false is what to do if the criteria is false.
1.2 To create a text box with a conditional calculation:
Create another text box using the Toolbox and enter
=iif([paid_fees]=no,Fees still owing,Paid) then check the
result.
Note iif above is not a misprint it stands for if and only if.
Access 2003: Creating forms (acc-14)

24
Task 9 Using filters
Objectives To create filters that find selected records.
Comments Filters and queries are very similar in that they are ways of searching the database
quickly. The main difference is that queries are useful when you know what data you
are looking for, whilst filters are useful for browsing the database when you dont
know what data it holds. For further information on filters, refer to the Appendix.

What do I need to know about filters before I start?
Question Answer
What is the difference between
a filter and a query?
One difference between filters and queries is
the result. With a query you only see those
fields that you specified. With a filter you see
all of the fields. You cannot specify the fields or
the order of the fields with a filter, however.
What different methods of
filtering records are there?
There are four methods to filter records: Filter
by Selection; Filter by Form; Filter by
Exclusion; Advanced Filter/Sort.
Can I only create filters on
forms?
You can create filters on tables, queries, forms
and subforms. Tables and queries need to be
open in Datasheet View. Forms can be open
in either Form View or Datasheet View.
Does removing a filter delete
it?
Removing a filter does not delete it - it just
does not apply it. When you save a table or
form, Access saves the last filter. You can
reapply the filter by clicking on Apply Filter.
How do I find out if I have a
filter saved?
You can check if a filter is attached to an object
by looking at the objects properties.
Can I break anything by using
filters?
Yes! Treat filters with care because it is very
easy to modify the data by mistake.
Filter By Selection on a whole field
9.1 To use Filter By Selection on a whole field:
Open frmMyForm, noting how many records there are specified by the navigation
buttons.
Navigate through the records until you find one, for example, where the student is in
year 2.
Warning! Do not type 2 in the Year field to find a record because all this will do is change
whatever is in the field to a 2.
Click on the Year field that contains the value 2, and then click the Filter By
Selection icon on the toolbar.
Note that fewer records are displayed and that (Filtered) now appears after
the number.
Click the Remove filter icon (as shown opposite) to display all the records
again, then click the Apply Filter button to reapply the filter.
Access 2003: Creating forms (acc-14)

25
Filter By Selection on only part of a field
Note How you select the value determines what values the filter is looking for. You can
highlight the whole field or a word in that field.
9.2 To use Filter By Selection on only part of a field:
Return to Form View.
In Date of Birth, highlight just the month, for example, July, and click Filter By
Selection (Figure 43).

Figure 43 select just the word July
Do not remove the filter as the results of this filter will be used in the next task.
Filter By Selection using more than one filter
9.3 To use Filter By Selection using more than one filter:
Use the filter from the previous task, showing all those with a birthday in July (or
whichever month you selected).
Click into the Year field and click Filter By Selection to display just those students
who are in the year that was displayed in the Year field and who were born in July.
Notice that less records are found.
Remove the filter.
Filter By Form
Note If you want to choose the values to search on from a list use Filter By Form. This is
very useful if you do not know what values are in that field.
9.4 To use Filter By Form on a table:
Open tblStudent (so that you can see a filter working from a table).
Click the Filter By Form icon on the toolbar to switch to the Filter By Form
window. This shows a blank view of the datasheet.
Note If there is already a filter attached, then it will be shown. Click the Clear
Grid icon on the toolbar to clear an existing filter.
Click each field in turn and notice the dropdown arrow that appears when you click
in each field (Figure 44) each individual value for that field is shown.
Note You can specify the filter criteria by selecting the value you are searching for from
the list for that field, or by typing the value or an expression into the field.
Access 2003: Creating forms (acc-14)

26

Figure 44 you can choose values from the dropdown list
Click on Year and select 2 from the dropdown list and apply the filter.
Note The Close icon, which is displayed when you click Filter By Form, has the same
effect as Apply Filter.
Remove the filter by clicking on the Remove Filter icon.
Select the LastName field and click Filter by Form again.
The last filter is still displayed, so clear the grid using the Clear Grid icon.
9.5 To display surnames starting from S using Filter By Form:
Click in the LastName field and press the S key.
Note The dropdown list now starts at S so that you can now select the name you want
without having to scroll all the way down from the start of the list. It is not possible to
display only those students with surnames beginning with S by this method.
Click Filter by Form and clear the grid.
To list only surnames that start with S, enter S* in the LastName field and apply the
filter (Access automatically alters what you have entered to Like "s*") (Figure 45).

Figure 45 this will find just those surnames that begin with the letter s
9.6 To use an expression when using Filter By Form:
Enter >=1/1/72 in DOB and apply the filter to find those students born during and
after 1972 (Access automatically adds hashes to show that it recognises that it is
looking for a date) (Figure 46).

Figure 46 this will find all those students born on or after 1 January 1972
9.7 To find null values by using Filter By Form:
Click on Filter by Form, select Add3, enter is null and apply the filter to find
records by those students who do not have a value in Add3 (Figure 47).

Figure 47 this will find all those records where there is no data in the Add3 field
Access 2003: Creating forms (acc-14)

27
Note If you specify values in more than one field, the filter returns records only if they
satisfy those values in all fields. To specify alternative values that records can have,
click the Or tab at the bottom of the window, and enter more criteria.
Filter records by using Advanced Filter/Sort
Note For complex filters use the Advanced Filter/Sort option. This lets you add all the
filter criteria and the sort order in one step.
9.8 To filter records using Advanced Filter/Sort:
Select Records / Filter / Advanced Filter/Sort from the menubar to open the query
grid (Figure 48).

Figure 48 - opening the advanced filter
Clear any existing filter from the grid using Edit / Clear Grid.
Add Year and LastName to the grid.
Add criteria to find students in Year 2, and sort in LastName order (Figure 49).

Figure 49 creating an advanced filter
Apply the filter by clicking the Apply Filter icon on the toolbar.
Return to the grid, select File / Save As Query and name it qryTestFilter.
Note You can only find the File / Save As Query option when the Advanced Filter/Sort
grid is open
Close the filter you have just saved.
Load an existing filter
9.9 To load an existing filter:
Return to the grid, clear any existing filter, then select File / Load from Query.
Select Test Filter and apply the filter by clicking the Apply Filter icon.
Note All queries based on the table or query that the form is based on will be shown in the
list of available queries/filters.
Access 2003: Creating forms (acc-14)

28
Task 10 More advanced forms (optional task)
Objectives To add a tab control and create a calendar on a form.

Adding a tab control to a form
Note A tab control avoids the need for long forms and is very useful when the data can be
sub-categorised. For example, frmStudent has the basic details of the student on
the main form and then tabbed pages for address, faculty details, and so on.
10.1 To add a tab control to a form:
Open frmMyForm in Design View.
Click once on the Tab Control icon in the Toolbox and then click once on the form
where you want to place the control.
Note Access adds a tab control with two pages as default.
Highlight several fields then right-click on one of them and select Cut.
Click on the tab of the page where you want the fields to appear then right-click
anywhere on that page and select Paste.
Note Controls cannot be dragged and dropped since they will just stay on the main form
and will not be visible because the tab control will obscure them.
Double-click on the individual page tab to look at page properties of Page 1 tab and
change the Name of the control and the Caption to something more sensible.
Add some more fields to the second page.
Add a new page by right-clicking on the page tab bar and selecting Insert Page.
10.2 To add a picture to a tabbed page:
Click on the Format tab in the Properties box and click the three dots next to the
Picture option, to get a list of pictures.
Select a picture from the list and look at where it is inserted.
Creating a calendar on a form
10.3 To create a calendar on a form:
Create a new form based on tblFaculty and add FacultyName, Faculty and
RegDate.
On the menubar, select Insert / Active X Control then choose Calendar Control
(if there is more than one version, choose the one with the latest version number)
and click OK.
Open the Properties box, click on the Data tab and set the Control Source to
RegDate to display this date on the calendar.
Open the form and change the date on the calendar.
Click on the Record Selector bar to save the record and notice, when you navigate
between records, that the RegDate field changes.
Note You would not normally have both the field and the calendar - this is to illustrate that
the calendar really matches the data in the field.

Access 2003: Creating forms (acc-14)

29
Appendix A Backup and filters
Backing up
Databases may represent a considerable investment of time (in both design and in
data entry) and the information they contain may be critical to a research project or
the running of a department.
It is essential that databases are backed up (that is, a duplicate copy is made
periodically and stored at a location away from the original e.g. out of the
building) and also that if containing sensitive or restricted UoB data that the
backups are securely stored.
If you require help with backing up or safeguarding data, please contact your
departmental IT support staff.
Filters - disadvantages of Filter By Selection on multiple
criteria:
1. This allows search for records that meet multiple criteria, but each criteria
must be specified one at a time;
2. It does not allow search for records that meet one criteria or another;
3. It not obvious which fields have filters;
4. It difficult to delete the filter;
5. It does not allow expressions to be entered as criteria;
6. It does not allow sort order to be specified in same step.
Filters - disadvantages of Filter By Form
1. This does not allow sort order to be specified in the same step.
Applying a filter automatically
You can create a form or report based on a table that already contains a filter or on
a query. If a sort order was specified in the underlying table or query, the new form
or report will use it, but each time you apply a filter or change the sort order to a
form or datasheet, you are asked if you want to save the changes, even when you
think you have not made any. This is because Access wants to save the last filter or
sort order.
In a report, you cannot create a filter using the filtering techniques mentioned
previously. However, a report (as well as a form) can inherit a filter. When you
create a form or report based on a table or query that has a filter or a sort order
specified in it, Microsoft Access includes the filter and sort order with the form or
report. The sort order is automatically applied.
Whether or not the filter is automatically applied depends on how you create the
form or report.
If you create the form or report directly from filtered data while the data is
displayed in an open table or query, the filter is applied automatically each time
you view the report, but it is applied only the first time you view the form after
creating it. The next time you open the form, you can apply the filter yourself.
If you create the form or report based on a closed table or query, the filter won't
be applied in the new form or report. You apply it when you need it.

Vous aimerez peut-être aussi