A subform is a form that is embedded in another form, the main form. A subform can also be inserted into a report.
It is common to show a subform in Datasheet view because it usually takes less vertical space. But it is awkward if there are more columns than will fit. A subform can display in Form View or in Continuous Form View, if that works better in a particular situation.
Subform in Datasheet View; Subform in Continuous Forms View
Changes
that break things:
It is easy to forget what queries or subforms go with what forms and
reports. Before making changes to existing objects, check the Object
Dependencies. Make sure that your planned changes do not break
something elsewhere!
Names for subforms/subreports: Use names that clearly identify the object as a subform or subreport. Don't plan to use the same copy both alone and as a subform/subreport. If you edit the original for use alone, it may make a mess when it shows as a subform/subreport.
You can create a subform by dragging an existing table, query, or form and dropping it onto the main form. Or you can use the Subform/Subreport tool from the Controls palette. The Subform Wizard will open in either case and take you through a few choices to create a new form. The wizard automatically adds it to the list of Forms in the Navigation Pane. You can open the new form (your subform) and make changes, just like any other form.
The subform control contains the subform. Sizing or formatting one does not change the other! You must select the right part.
In Form Design View, when the subform control is not selected and neither is anything inside it:
A subform is actually an object inside a subform control. It can be tricky to know what is selected and therefore to which object your keystrokes and mouse clicks will apply.
Subform control is selected:
Form inside the subform control is selected:
Subform's source is a table or query:
Something selected in subform: It gets a bit trickier to know when the selected part is part of the subform. The Property Sheet has the same properties in either case. You will have to look carefully for the gold border for a selected control or for the dark bar for a selected section.
In Design View, a subform shows its own design view inside the subform control. That is often a bit small to work with. Access allows you to open a subform in a new window,where you have more working space. The subform control must be selected for this option to be available.
Close
separate subform/subreport window to view main form:
While a subform or subreport is open in Design View in a separate window, the main form or report cannot use it. All you will see is a blank area with the name of the subform or subreport.
You may get a warning message, depending on the order in which you opened the main and child forms or reports.
![]() |
Step-by-Step: Subforms |
![]() |
What you will learn: | to add a subform using drag-and-drop to change the source of a subform to edit a subform in a new tab to format a subform control to edit subform datasheet from parent form view to create new query as source of subform to add a subform with tool on Controls palette to rename a subform to modify parent form source to format subform |
Start with: , resource files, worldtravel-Lastname-Firstname.accdb from folder databases project4 as updated in the previous lesson
You will use some forms that you have already created but you will modify them to include subforms:
Projects with a subform showing staff assigned to that project
Agents with a subform that shows that agent's clients.
You will modify an existing form for projects by adding a subform of people assigned to the projects, based on an existing table.
In the Navigation Pane, drag the linked table Project Staff and drop it
below the Budget text box.
A new unbound control appears on the form. The form enlarges automatically to hold it.
The Subform Wizard opens.
Accept the default choice for fields to link the subform to the form.
Click on Next >.
Enter as the name Subform-Project Staff.
It is
handy to group subforms together in the Forms list by starting their
names with "Subform". This makes it easy to tell what's a subform and
what's not.
Click on Finish.
The wizard closes, putting you back in Form Design View.
Access automatically resizes the Detail section and creates a
default-sized subform control.
In Design View, only part of the form shows. That's because of the ruler and section bars. You cannot really tell whether this size is good or not until you look at the Form View.
Switch to Form View.
Is the subform working?
It clearly is showing something! But is it useful?
The label for the subform control is above the subform, overlapping the Budget control.
In the subform itself, it looks like too many numbers and not enough user-friendly data. Where are the names of the people assigned to the project?
Strangely, the subform displayed as a form in Form Design View but it actually shows as a datasheet when in Form View. The property Default View is set to Datasheet, by default for a subform. Datasheet View gets more records in sight at once. That is usually very helpful. So it makes sense, once you think about it. You just have to manage which fields are really needed.
Click the horizontal scroll button at the bottom right of
the subform to see what other columns there are.
Inspect the datasheet for each record in the main form, using the navigation buttons at the bottom of the window.
The subform does change with the records. Good! But there are issues...
Issues:
The column EmployeeID is blank for all records. That's not right!
There are no names to go with those Job titles.
The label for the subform is overlapping the Budget text box.
Looks like some formatting is in order plus a change to the source of the
subform so we can see some names.
Save the
form as Projects with Project Staff.
Since there is something wrong with the datasheet (EmployeeID is blank), that should be the first item to fix. We know that you put values in the underlying table, so the problem must be that those values are not linking properly to the main form.
Thinking:
What field do you need to link the subform to the main form? ProjectID.
What data do you really want to see in that subform? The full names of staff members assigned to the project and their jobs. What can you use as the source that includes both?
Add the query QFullName-Staff to the Query
Builder.
You created this query earlier to concatenate the name parts into a
single name.
Problem: QFullName-Staff fields are named Expr1, Expr2, etc.
Access lost track of your tables.
Solution: Close the Query Builder and open the query in Design View. Delete the Expr1: and similar to return the fields to their normal names. Repeat the steps above to invoke the Query Builder again.
Run the query to verify that you are going to get the data that you
want.
Looks good. It shows the field that links the subform to the main form and the two data fields that you want to show in the subform.
Only three projects have staff assigned, Projects 1, 2, and 8.
Close the query by clicking the Close button at the far right of the query tab.
(While in datasheet view, there is no close button on the ribbon.)
A message appears asking if you want to save your changes to the SQL statement.
Inspect the subform.
Whoops. Access did not update the form with the new fields. The subform shows the same fields as before.
The ProjectStaffID field shows an error now because that field is not in the new source.
The SFullName field is not showing anywhere.
More editing to do!
Save the
form and close it.
[Projects with Project Staff]
You need to make changes to the controls on the form that is in the subform control. That form is named Subform-Project Staff. It is easier to edit the form in its own window.
Switch to Datasheet View.
Now you can get that label out where it belongs.
Like any other datasheet, you can change the arrangement and width of the columns and sort directly in the datasheet.
Repeat for the SFullName column.
This width will be remembered!
Would you rather see the names first? Also easy to do.
Save the
form.
[Projects with Project Staff]
You could use the datasheet column menu also while in Form View to sort or filter.
Note: This form cannot be used to update project staff records. The datasheet will not let you type in the SFullName column, which is a calculated column. To be able to add new staff assignments you need a subform that includes the fields from the underlying table(s). You cannot edit a calculated control like SFullName. This type of subform is actually well suited to a report.
Next you will create a form for agents that shows their clients. You will create a new query to pull together the fields that you need. This information meets in the Trips table. For the purposes of this form, you only need to show the client's name, not all of the other info. You already created a query that glued the name parts together, but to sort those names alphabetically you must refer to the parts again. <sigh>
Add the table Trips and Clients and the query QFullname-Client to the design.
Drag the fields AgentID, CFullName, LastName, FirstName,
MiddleName to grid at the bottom, with the resulting columns in that order.
Sort Ascending the name parts, LastName, FirstName, MiddleName and uncheck their Show boxes.
Run the query.
There are 11 records in neat alphabetical order.
This list shows a client for each trip in the Trips table. Some clients took more than one trip! Some clients in the Clients table have not booked trips yet. They do not show in the results.
Save the
query as QAgents and Clients-fullname.
Close the query.
Note: The query QFullName-Client also has the name fields, but the Show box was unchecked. You could edit the query to show those fields. You would not need the Clients table in the grid for the new query. But does changing the QFullName-Client query cause a problem somewhere else? You would have to check carefully!
Now you will add a subform to a new page on the tab control. You will use the Subform/Subreport tool on the Controls palette.
Select
A new page appears with a default name, Page + a number, like Page159.
Recall that the number depends on how many controls you have inserted
already, including the ones that you have deleted.
In
the Property Sheet for the page, change the Name property to Clients.
The tab text changes to match.
Click on the Subform/Subreport tool in the Controls palette on the ribbon.
Drag on the Clients tab to make a box about as
large as the tab page.
The Subform Wizard opens.
Accept the first choice, Use existing Tables and Queries.
Click on Next >.
Select the query that you just created, QAgents and Clients-fullname.
Click on the >> button to move both fields
to the right.
Click on Next >.
Click on Next >.
Change the suggested name for the subform to
Subform-Clients-Staff-tabcontrol.
This kind of name tells you what will be in the subform (Clients) and which form it goes with.
Click on Finish.
Access creates a subform using its default formatting.
The tab control enlarges automatically and now covers controls on the main form. Whoops.
In Form Design View, this looks horrible. Even though you dragged a
control, Access made a wider but shorter one. Humph!
Click on the Clients tab and navigate to each agent, using the Navigation buttons at the bottom of the window.
Only some of the staff members have clients in the Clients table.
Use the Navigation button at the bottom to change to record #2, Hector Chavez.
Scroll the subform to see all 6 records. One client appears several times, once for each trip.
This looks better than design view but it is still strange. The photo control is totally covered up and the full name is overlapping the tab control. That's why there is a gap in the border. The full name control has a white background.
Problem:
Subform for clients is on top of all the tab pages
You did not get the subform onto a tab page to start with. It is
floating on top of the tab control.
Solution: In Form Design View, click the edge of the subform
control to select it. Copy. Delete. Click the Clients tab. Paste.
If you change the subform's name, Access is cooperative and changes the references to it for you! Let's prove that! The main form and subform must be closed.
From the context menu select
. If you did not close the form, a message appears telling you that you
cannot rename an open form.
You must close the form and try again.
Most of the staff members entered so far in the Staff table do not have any clients. That's because they are not agents! You need to change the source for the main form so that only Agents show. Currently the source is the table Staff.
Run the query.
Make sure that all of the records show Agent in the Title column and
there is only 1 column listing the titles. There should be 8 records.
Notice that both "Agent" and "agent"
were accepted.
Close the query by clicking the Close button .
A message appears, asking if you want to save your changes and
update the property.
Click on the Company Info tab.
That is the tab where the Title control is.
Problem:
Title field shows #Name?
Access was confused because there were 2 fields named Title in the
query results. You forgot to uncheck the Show box for the field Title
that you dragged separately to the query grid. The * column includes
Title also.
Solution: Go back to Design View, open Query Builder, and fix the source query.
Navigate the records and verify that all have the
title Agent and that the subform shows on the Clients tab.
If you checked the query datasheet, there should not be a problem.
The filter accepts both "Agent" and "agent".
There are several things that need fixing.
That little subform needs help! You will make some changes to clean things up.
It can sometimes be a little tricky to select what you want. You can select the subform control or the form inside the control, plus you can select a section or a control in the subform. Watch for the handles!
Drag the bottom edge of the subform down toward the bottom of the tab
page but stop above the Date Updated control. Do not make the tab control taller.
You are enlarging the subform area so you can see better to edit it in place.
The subform is showing Form View instead of the Datasheet View that we
saw in the regular Form View. This is still confusing!
(Your subform may have a different look.)
Click on each tab and make sure that all of the controls show at the new width for the tab control.
How is it looking now? There is a an extra column in the datasheet and extra space below on the tab. The tab control could be somewhat shorter, but this will do.
Remaining issue: Duplicate names show in the Client list when a client has arranged multiple trips. The duplication of names cannot be resolved easily for a form, but it can be fixed in a report.