Access 2010: Building Access Databases: Practical Workbook
Access 2010: Building Access Databases: Practical Workbook
Building Access
databases
Practical workbook
Document information
Course files
This document and any associated practice files (if needed) are available on the web. To find
these, go to http://www.bristol.ac.uk/it-services/learning/resources and in the Keyword box, type
the document name.
Related documentation
Other related documents are available from the web at:
http://www.bristol.ac.uk/it-services/learning/resources
This document is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.0 UK:
England & Wales Licence (http://creativecommons.org/licences/by-nc-sa/2.0/uk/). Its “original author” is the
University of Bristol which should be acknowledged as such in any derivative work.
Contents
Document information
Task 1 Course objectives .................................................................................. 1
Task 2 Introduction to relational database concepts....................................... 2
Task 3 Create a new database file ..................................................................... 4
Task 4 Create a table manually ......................................................................... 5
Task 5 Create new tables automatically ........................................................... 6
Task 6 Fine–tune the Student and Subject tables............................................ 8
Task 7 Create relationships using two methods ............................................ 10
Task 8 Create a dropdown list not based on a relationship .......................... 14
Task 9 Create a form and subform using Form Wizard ................................. 15
Task 10 Customise your form – basics ............................................................ 17
Task 11 Customise your form using the Property Sheet ................................. 22
Task 12 Add a calculation to a form.................................................................. 26
Task 13 Introducing automation ....................................................................... 27
Task 14 Create a form that asks you what data you want to see .................... 29
Appendix A Setting a tabbed display ...................................................................... 31
Access 2010: Building Access databases (acc-39)
Important: prerequisites
Before attending this course you must have attended "Access 2010: An introduction",
or otherwise have experience of using Access.
Without this prior experience it is likely that you will not benefit from attending
this course and will struggle to keep up with the brusque pace at which the
course is run.
Access 2010: Building Access databases (acc-39)
Table creation
We are going to (though not quite in this order):
1. Build a table and enter data manually.
2. Import pre-existing data from Excel and automatically create new tables.
3. Modify the above tables to ensure correct data types, field sizes and/or formats.
4. Import a table along with data from another Access database.
5. Add the required relationships and create dropdown lists.
1
Access 2010: Building Access databases (acc-39)
Figure 1 – LDS for four table database with three one-to-many relationships (the
number of tables and the relationships between them will differ from database to
database)
2
Access 2010: Building Access databases (acc-39)
Figure 2 – LDS showing the primary and foreign keys for each table
The $60,000 question is, how do you know which tables and fields you need in your
database in the first place? Unfortunately, this is outside the scope of a beginners’
course.
There are methods you can use to help you come up with a workable design – see
the MS Access database design checklist at:
http://www.bristol.ac.uk/it-services/applications/databases/access/checklist/.
3
Access 2010: Building Access databases (acc-39)
3.1 To open Access 2010 and create a new Access database file:
Click on the Start button and start to type Access into the Search programs and
files box. When Access 2010 appears in the list of matches, click on it once to open
the application.
On the right-hand side of the window enter Student in the File Name field.
Click on the browse icon (the open folder logo) to the right of the File Name field
and select where you want to save the new file (if in the training room, this will be
your O Drive, so that you can access your training files from other locations) then
click OK.
Check that the file path underneath the File Name field is correct and click Create.
3.2 To look around the Access work environment:
Close the table that has automatically opened by right-clicking on the tab saying
Table1 and selecting Close, then look at the Access environment.
Note If you do not see a tab, then you will need to set tabs for open objects. See the
appendix to set tabbed display.
4
Access 2010: Building Access databases (acc-39)
The basic building blocks for building any Access table are:
When done, select MarkID and click on the Primary Key icon in the Ribbon.
Click the Save icon, name the table Mark then close the table.
Note In reality, we would now build the rest of the tables, create the relationships and add
test data to make sure that the database works in the way we expect. However, time
constraints on the course prevent us doing this … so we are going to 'cheat' – but
the 'cheats' in the following task provide useful learning points in that if you have
pre-existing data there are ways to prevent you having to type it all over again.
5
Access 2010: Building Access databases (acc-39)
Click Next and tick First Row Contains Column Headings, if not already ticked,
then click Next.
Click Next again and select Let Access add primary key.
6
Access 2010: Building Access databases (acc-39)
Click Next, call the new table Student, then click Finish, followed by Close.
Open the Student table to view the data – if you see ###### just make the column
wider to see the data – then close the Student table.
5.2 To create the Subject table by importing pre-existing Excel data:
Do exactly the same as above with the following differences:
Use the file, Subject.xlsx.
When you get to the relevant point, select Choose my own primary key
and choose Subject from the dropdown.
Name the new table as Subject.
5.3 To import a table, complete with data, from another Access database:
Select the External Data tab from the Ribbon and in the Import & Link section
select Access.
Browse to Faculty.accdb, select the file and click on Open.
Make sure that Import tables, queries, forms, reports, macros, and modules
into the current database is selected and click OK.
In the Table tab, select Faculty and click OK, followed by Close.
Open the Faculty table, look at the data and then close the table.
7
Access 2010: Building Access databases (acc-39)
6.1 To modify data types, field sizes and/or formats in the Student table:
Make sure that all other objects are closed then right-click on the Student table in
the Navigation Pane and select Design View.
Figure 8 - these are the only parts that you need be bothered with
For each Field Name, update the Data Type and Field Properties (Field Size,
Format and Caption) as shown in the table below.
Field Name Data Type Field Size/Format Caption
Faculty Text Field Size: 3
Subject Text Field Size: 10
YearNumber Number Field Size: Integer Year
PaidFees Yes/No Format: True/False Fees paid?
FirstName Text Field Size: 30 First name
LastName Text Field Size: 40 Last name
Title Text Field Size: 10
Gender Text Field Size: 15
DateOfBirth Date/Time Format: Long Date Date of birth
Address1 Text Field Size: 50 Address line 1
Address2 Text Field Size: 50 Address line 2
Address3 Text Field Size: 50 Address line 3
Town Text Field Size: 50
Country Text Field Size: 20
Postcode Text Field Size: 15
8
Access 2010: Building Access databases (acc-39)
In PaidFees, click on the Lookup tab in the Field Properties and change Text Box
to Check Box (this may already be the default, in which case don’t worry).
Rename the ID field to as StudentNumber and give it the caption, Student ID.
Select Datasheet View from the Views section of the Ribbon (save when prompted
– and say Yes to the following warning message) and look at your modified table.
Question: where do the field names shown at the top of each column come from?
6.2 To modify data types, field sizes and/or formats in the Subject table:
Close the Student table then open the Subject table in Design View.
Update the fields as per below then close and save the table.
Field Name Data Type Field Size/Format Caption
Subject Text Field Size: 10 Subject code
CourseName Text Field Size: 50 Course name
9
Access 2010: Building Access databases (acc-39)
Important rules:
The data type for primary and foreign key must always match.
If a primary key is an AutoNumber, the foreign key end must be set as Number, Long
Integer – this is because the AutoNumber data type is number, long integer.
Foreign key fields MUST only contain values already contained in the related primary
key otherwise Access will not let you create a relationship.
7.1 To compare the values held in the primary and foreign key fields in three tables:
Open the Student, Subject and Faculty tables and use the tabbed display to view
one table after the other.
Notice that the values in the Subject field in the Subject table are repeated in the
Subject field in the Student table. Similarly, notice that the values in the Faculty
code field in the Faculty table are repeated in Faculty in the Student table.
Note Look back at the database design in Task 2 and you will see that the Subject and
Faculty fields in the Subject table are the foreign keys for the Subject and Faculty
tables respectively – you can guess that the cleansing work done in Excel was very
fiddly so as to get this right.
7.2 To create the first relationship using the Lookup Wizard:
Close all the tables except the Student table and open this in Design View.
Click in the Data Type cell for the Faculty field and select Lookup Wizard from the
dropdown list.
When the Wizard starts, leave the first screen as the default and click on Next.
Select Table: Faculty and select Next.
Double-click on Faculty and FacultyName in turn to move them across to the
Selected Fields section and click Next.
Sort by FacultyName ascending (don’t select Faculty by mistake) and click on Next,
followed by Next again.
Tick the box next to Enable Data Integrity and click on Finish – say Yes to the next
two messages.
7.3 To create the second relationship using the Lookup Wizard:
Do exactly the same as above but for the Subject field and with the following
exceptions as you go through the Wizard:
Select Table: Subject as the linking table.
Take across both Subject and CourseName to the Selected Fields section
Sort ascending by CourseName.
Note: any forms created will have dropdowns (which are sorted alphabetically) created automatically
because forms inherit the properties from the table as at the point of creation.
7.4 To fine tune the new relationships so that they work properly:
Close the Student table (and any other tables you might have open), click on the
Database Tools tab and click on Relationships.
Right-click on the relationship between the Faculty and Student tables and select
Edit Relationship (this is fiddly and you might need to try several times before you
get the correct right-click menu).
11
Access 2010: Building Access databases (acc-39)
In the Edit Relationships window, tick Cascade Update Related Fields and click
on OK.
Do exactly the same for the relationship between Student and Subject.
Close the Relationships Window saving when prompted.
7.5 To check the two relationships and the dropdowns:
Open the Student table in Datasheet View and notice that now, instead of seeing
the codes in the Faculty and Subject fields, you can see the names.
Note In the background, the codes are still being entered into these fields; it’s just that
Access is now showing us something more user-friendly.
Click in these and notice that both now have a dropdown.
7.6 To add in the relationship between the Student and Mark tables:
Close the Student table and open the Relationships Window again.
Click on the Show Table icon in the Ribbon, add the Mark table and click
on Close.
Click on StudentNumber in the Student table, hold the mouse button down and
drag to StudentNumber in the Mark table, then let go of the mouse button.
In the Edit Relationships window, tick Enforce Referential Integrity and Cascade
Update Related Fields only, then click Create.
Make sure that your relationships match the above – if not, delete the offending
relationships and go back to see what you did wrong!
12
Access 2010: Building Access databases (acc-39)
If all is well, click on the Close button and save changes when asked.
7.7 To check the Student/Mark relationship and add some marks:
Open the Student table and notice the plus sign next to the Student ID field – click
on the plus sign to open the sub-table for student number 1 and add the following
sets of marks (Mark code is filled in automatically, so just start by typing in the
Submission date field).
Submission Title Type Mark Comments
Date
01/03/12 Death on the Nile Essay 75 Late
01/03/12 Dial M for Murder Essay 95 Very inventive
01/06/12 English Part 1 Exam 55 Needs resit
01/11/12 LANG10031 Module 50
Add some marks for students 2 and 3 (make it up!) then close the Student table.
13
Access 2010: Building Access databases (acc-39)
Type in Male, Female and Undisclosed as per the screenshot above and click
Next.
Tick Limit To List and click on Finish.
Look at Datasheet View (saving when prompted) and try out your new dropdown by
randomly changing a few genders.
8.2 To amend this list:
Go back to Design View, select Gender then click on the Lookup tab in Field
Properties.
In Row Source add a semi-colon after “Undisclosed” and add “Fish” (with the
speechmarks).
Look at Datasheet View (saving when prompted) and try out your new dropdown to
see the new option.
Go back to Design View and revert to the original list, unless you really do want
Fish in the list!
Whatever you decide to do, don’t forget to close the table.
14
Access 2010: Building Access databases (acc-39)
In the Create section of the Ribbon, select Form Wizard from Forms.
From the Tables/Queries dropdown list choose Table: Student.
Figure 17 – select all fields except StudentNumber from the Marks table
Now select Table: Mark from the Tables/Queries dropdown and move all fields
except for MarkID and StudentNumber across, then click Next.
15
Access 2010: Building Access databases (acc-39)
Select to view your data by Student, if not already the default, and click Next.
Choose Datasheet as the layout for your subform, then click Next.
Name the form as MyStudentForm and the subform as MarkSubform, then click
Finish.
Figure 19 – a very basic form with subform – colours may differ depending on setup
Notice that the data entry fields are different sizes with data aligned left or right
depending on field size assigned and data type.
Use the navigation buttons at the very bottom of the window (not those under the
subform) to move between records – note that any marks you added for student
numbers 2 and 3 earlier will appear in the subform when you move to those records.
In the Home tab, click on the dropdown in the Views section and select Design
View ready for the next task.
16
Access 2010: Building Access databases (acc-39)
Action Method
To move a field and label together Move the mouse pointer over the edge of an object so that a
(Figure 20) four-headed arrow appears, then click and drag the box to a
new position.
To move a field without moving the Move the mouse pointer over the larger handle at the top, left-
label, or vice versa (Figure 21) hand corner of the field control, then click and drag it to a new
position.
Figure 21 - moving one part of the field by clicking on the large ‘handle’
Action Method
To move selected controls with Click on a field control and press CTRL+ARROW keys to move
micro movement in the direction of the arrow.
To change the size of an object Move the mouse pointer across the handles until a double-
(Figure 22) headed arrow appears, then click and drag to the required
size.
To size selected controls with micro Press the SHIFT+ARROW keys.
movement
17
Access 2010: Building Access databases (acc-39)
Formatting options
10.2 To find out what the various toolbar options can do:
Click on the Format tab of the Ribbon, and look at the various options – some
should be familiar as they are the same as those found in other Office applications.
Select a Text box (data field) and try changing the font and font size.
Experiment further on both fields and labels.
You can also format objects, such as field controls and labels using the Special
Effect option by right-clicking on an object and choosing one of the options.
Have a look at the other options that you have on the right-click menu, especially at
Align and Position, which can be quite useful (these options are also available on
the Arrange tab in the Sizing & Ordering section).
18
Access 2010: Building Access databases (acc-39)
Click on the Design tab to view the options in the Controls section.
Click once on the label in the Form Header that says MyStudentForm and press
DELETE on the keyboard.
Note You could just have altered the text in the original label, but then you wouldn’t have
found out how to add a label!
In Controls, select Label by clicking once.
Place the mouse on the screen in the Form Header space (the cursor should
change to +A – as in Figure 26), hold the mouse button down and draw a box.
In the label box, type a suitable title, alter the size and colour of the text (you will
need to return to the Format tab) and resize the box if necessary (Figure 27) – you
will need to click outside of the box and then click once back on it before you can
format any text that you have added.
Note Fields and labels can be deleted by selecting them and pressing the DELETE key. 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
10.4 To use Format Painter:
In the Format tab, right-click on a data field and use the Fill/Back Color, Font/Fore
Color and Special Effect options to change the colours of the background, the text
and the text box in turn.
Click away from the field (somewhere on the form, rather than on another object)
and then click back on to the chosen field once to make sure that it is selected.
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 Format Painter again to switch it off.
19
Access 2010: Building Access databases (acc-39)
Release the left button and all the objects enclosed by the rectangle will be selected.
10.6 To select multiple objects if they are not located together:
Click once on the first object.
Hold down the SHIFT key and click on the other objects once each in turn.
20
Access 2010: Building Access databases (acc-39)
To make the fields uniform in size to each other, click on the Size/Space
icon and select To Tallest, To Shortest, To Widest or To Narrowest
as your form design requires.
Also use the Size/Space icon to experiment with the various options in the Spacing
section (at least three vertically spaced controls should be selected for the Equal
Vertical options).
Note If you get unwanted results, select Undo on the toolbar and try again selecting only
the field controls and not the labels.
Look at how the form looks in Form View (Figure 30).
21
Access 2010: Building Access databases (acc-39)
Properties
For several of the following tasks you will need to use the Property Sheet. As you
become more experienced in fine-tuning the controls on your form, you will find that
it is useful to keep the Property Sheet open whilst designing your form.
11.1 To open and get to know the Property Sheet:
In Design View go to the Design tab of the Ribbon and select Property
Sheet.
Look at various of the tabs – and don’t panic! – yes, there is a lot you can do here,
but most mortals use very few of these options.
Figure 32 – notice that a small, black box appears when you click on it
In Design View, click on the small box in the top, left-hand corner of the form. This
means that you have selected the form as a whole.
22
Access 2010: Building Access databases (acc-39)
Figure 33 – you can change which toggle options you see under the Format tab
Change Allow Layout View to No and change Allow Datasheet View to Yes.
View the form in Datasheet View, then return to Design View. Notice that you now
toggle directly between Form View and Design View – that’s much better!
Test out the locked field by looking at Form View and trying to edit the data in the
field which you have locked.
23
Access 2010: Building Access databases (acc-39)
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 box (which is now black) and drag the field to a new position in the list.
When the order is correct, click on OK.
Note The Auto Order option doesn’t always work as might be expected!
11.5 To stop tabbing into a field:
There is no point in tabbing into either StudentNumber or YearNumber –
StudentNumber cannot be edited because it is an autonumber and you have locked
YearNumber earlier in this task.
In Design View click on StudentNumber.
Select the Other tab in the Property Sheet and change Tab Stop to No.
Do the same for YearNumber.
11.6 To stop tabbing into the subform:
Figure 36 – click the subform – make sure there is no black square in the circled area
Click once only on the square in the top, left-hand corner of the subform.
Open the Properties Sheet, select the Other tab, then change Tab Stop to No.
24
Access 2010: Building Access databases (acc-39)
Adding an image
www.bristol.ac.uk/imagelib holds a library of corporate graphics which you can use.
11.7 To insert an image onto your form:
Right-click on an image on the webpage and select Copy.
Right-click in the Form Header and select Paste from the menu that appears.
Notice that the picture is placed inside a box, which can be resized.
11.8 To format the image to taste:
Resize the box and see what happens to the picture inside.
Open the Property Sheet if it is closed and select the Format tab.
Click on Size Mode and 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 37).
25
Access 2010: Building Access databases (acc-39)
Figure 38 – make sure that you type the right number of brackets!
In the text box, type =int((date( )- DateOfBirth)/365.25) then go to Form View and
change the year of someone’s date of birth.
Save the record and see the result.
12.2 To name the new field control:
Click once on the text box and in the Property Sheet select the Other tab.
Note that the Name property shows the same name as that shown on the label to
the left of the text box.
Change the Name property of the text box to Age.
Important ‘gotcha’: the label and the text box both have their own names – don’t
get caught out by thinking that if you change the text on the label then that means
that you have given the text box a new name. All you will have done is changed the
text on the label. It doesn’t even rename the label – you have to do that the same
way as you have just renamed the text box!
Figure 39 – how the new text box and label should now look
Change the text on the label to Age: – including the colon – and format the new field
to match your other fields.
12.3 To check the new field in Datasheet View:
Switch to Datasheet View and notice that, although Age appears, it cannot be
edited or even sorted.
In which case, you might as well stop the new field being tabbed into – if you can’t
directly edit the field, there’s no point tabbing into it.
26
Access 2010: Building Access databases (acc-39)
27
Access 2010: Building Access databases (acc-39)
If you have time: depending where you put your buttons (i.e. if you put them in the Detail section of
the form) you may need to change the tabbing order*.
* Now do you see why you should to give buttons annoyingly long names that explicitly state what it is
that they do?
13.3 To create an ‘autoexec’ macro that tells Access what to do when you open
the file:
In Design View, click on the Create tab and select Macro.
Click on the dropdown and select OpenForm and in Form Name select
MyStudentForm.
Click on the Save icon above the File tab, save the macro as Autoexec and close
the database, saving any changes if asked.
Open the database again – MyStudentForm should now open automatically – on
opening any database file, Access always looks for a macro named ‘autoexec’.
28
Access 2010: Building Access databases (acc-39)
Task 14 Create a form that asks you what data you want
to see
Objectives To create a query that asks you which records you want to see and to create a new
form based on this query.
Comments This task is optional and will only be covered on the course if we have time. What
this task is intended to show is that it is possible to use queries to affect the way in
which you interact with parts of your database. In this instance you are creating a
form that filters the possible data records that can be shown in that form.
29
Access 2010: Building Access databases (acc-39)
Click on the square box in the top, left-hand corner of the form to select the form as
a whole, then open the Property Sheet if it is not already open.
Select the Data tab and click on the dropdown next to Student in Record Source.
30
Access 2010: Building Access databases (acc-39)
A message will tell you that you need to close the database for the change to take
effect, so click on OK.
Click on the File tab and select Close Database.
Reopen the database, click on Enable Content and choose No if a further security
warning appears.
31