As an MS Access trainer, the one question I am always asked is how to create and implement working MS Access subforms. This handy guide will get you up and running FAST!
2. http://www.access-databases.com/ebooks/subforms ...................................... 2
Beginning Access Database Subforms ............................................................ 3
What are MS Access subforms?............................................................... 3
Database subform relationships ............................................................... 5
Create a subform ...................................................................................... 7
Manually create a subform...................................................................... 14
Create multiple subforms ........................................................................ 22
http://www.access-databases.com/ebooks/subforms .................................... 23
GET THE FULL VERSION OF THIS EBOOK HERE
http://www.access-databases.com/ebooks/subforms
2
3. Beginning Access Database Subforms
What are MS Access subforms?
The best way to think of a subform is a form within another form.
We tend to create our forms to show single pieces of information.
For example, here I have a form which shows a single customer record.
What happens if I want to display related information for this customer?
Supposing the customer has related orders. Right now I have no way of
showing related information within the form.
We show related information by creating a subform and adding it to the main
form.
We can create the subform manually ourselves or use one of the MS Access
wizards to guide us through the steps.
3
4. Here I have a subform contained within the main form.
The main form details are at the top.
Customer ID
Company Name
City
Country
The subform holds details of product information sales in each quarter.
Notice there is more than one product. Subforms are ideal for showing
multiple records.
We can also have multiple subforms within a single form.
4
5. Database subform relationships
MS Access is a relational database. All this means is that we are able to
relate our data together. For example there is a relationship between
customers and orders. Also between students and courses.
Relationships are created via our tables. When creating subforms we need to
look at how we have defined our relationships.
For these examples I am using the Northwind sample database that comes
with MS Access.
Right click at the database window and select relationships
5
6. The relationship I focus on is between customers and orders.
Notice the primary key of the customers table is CustomerID.
Notice also that this same key is in the orders table.
This tells us that there is a one to many relationship between customers and
orders. One customer can have many orders and each order can belong to
one customer.
This relationship is an ideal candidate for a main subform scenario. The main
form would hold the one side of the relationship – the customer tables details.
The subform would hold the many side of the relationship – the orders table
details.
We show one customer record in the main form and many order details in the
subform.
Looking at the tables we can see that the CustomerID is in both customer and
order tables.
6
7. Create a subform
Now that I have set up the table relationships it is time to go ahead and create
the main form.
At the database window first click on the forms button.
And then click ‘New’.
The form wizard will then appear.
Select design view.
7
8. Then select the customers table.
Then click the ok button.
Drag fields from the customers table onto the form.
Hover over the edge of the form with your mouse until you see a cross type
symbol. Hold down the mouse button and then drag the form edge down
slightly in order to make room for the subform. Also drag the sides of the form
out slightly.
Click on the toolbox icon.
8
9. At the toolbox make sure the wizard button is pressed. It will glow orange
when it is pressed.
From the toolbox select the subform/subreport control.
Drag and drop it onto the form. Keep the mouse button pressed and drag it so
that it fits on the subform.
The wizard will also appear.
Make sure the first option is selected to use existing tables and queries.
9
10. Click the ‘Next’ button.
Select the orders table from the dropdown box.
I then select a few fields to show on the subform.
It may be worth including CustomerID, although not necessary if you have
defined the links between the tables when creating relationships.
10
11. Click the ‘Next’ button.
Because I defined the relationship between the customer and order tables,
MS Access makes a guess at how I want to relate the main form and subform.
It has guess correctly in that I will relate by CustomerID. Recall this is how I
related the tables together.
MS Access wants to link the forms together via the CustomerID.
Alternatively, we could also select the define my own option. In this case we
would manually have to relate the main and subform together by choosing the
linking fields.
11
12. Click the ‘Next’ button to move on.
Now give the subform a name and click the finish button.
12
13. MS Access has now added the subform to the main form.
Run the form by clicking the button named ‘View’.
Notice the one side of the relationship – Customer - is showing at the top of
the form. This is the main form details.
The customer related orders are showing in the subform.
I have successfully modelled the one customer has many orders relationship.
13
14. The main form has navigation buttons at the bottom and the subform also has
it’s own.
Use the main form buttons to move through each customer record. The
related orders for each customer will be shown as you move through each
customer record.
Use the subform buttons to move through each subform row.
The main and subform have been successfully created.
Now save the main form.
I name it ‘Customers_Main’
Manually create a subform
We’ve seen how the wizard can be used to step us through creating a
subform.
We are now going to create a subform manually.
Here is my customer record form (Customers_Main) in design view.
14
15. I have left some blank space for the subform.
We already know that customers are related to orders. I have the customer
record currently showing so now I have to create a subform to hold the related
orders.
At the database window first click on the forms button.
And then click ‘New’.
15
16. The form wizard will then appear.
Select design view.
I now select the orders table from the dropdown box.
Click the ok button to move on.
From the field list that appears drag and drop the required fields to the form.
16
17. Double click the small square at the top left of the form to bring up the form
properties.
At the property sheet, click on the ‘All’ tab.
Look for the property called ‘Default View’ and change it to ‘Datasheet’.
17
18. I now save the form.
Open up the main form in design view and move it so that it is next to the
database window.
Now drag the subform from the database window onto the space in the main
form.
18
19. Position the subform on the main form. Click on the top of the subform and
look for the small finger symbol at the top left. Hold down the mouse button on
this symbol and drag the subform into position.
Now we need to link both forms together.
Right click the top edge of the subform and select properties.
The two properties we are looking for are the ‘link child fields’ and the ‘link
master fields’.
Link master refers to the linking field in the main form and link child is for the
linking field in the subform.
19
20. My linking field will be CustomerID in both these properties.
Now run the form.
Clicking on the record selector changes the main form record and shows the
related subform records.
The subform has been created manually and successfully related to the main
form.
I am showing company name in both the main and the subform. This is fine,
but not really necessary.
20
21. To remove the customer from the subform, go back to form design view,
select the field in the subform and delete it.
Save and run the main form again.
The field will now only show in the main form.
21
22. Create multiple subforms
We can create more than one subform within a single form.
Here I have a main and a subform.
The customer details containing company name and country are shown in the
main form and I also have two subforms – the first shows order information.
When one of those order lines are clicked the related products are shown in
the second subform.
There is a relationship between the main form details and the first subform.
That relationship is linked via the CustomerID. One customer has many
related orders.
We also have a relationship between orders and products.
22
23. GET THE FULL VERSION OF THIS EBOOK HERE
http://www.access-databases.com/ebooks/subforms
For products, ebooks, training and MS Access info, check out the website
http://www.access-databases.com/
2010 Paul Barnett. All Rights Reserved.
No part of this publication may be reprinted
or reproduced without permission.
23