Access VBA Macro Programming

(Joao Candeias) #1

onto your form. Change its Control Source to Quantity. Set the corresponding label control
caption to read Quantity. Add three text box controls onto the form by dragging the text box
control from the Controls group of the ribbon. Set the Control Source properties for the three
text boxes to Quantity, Unit Price, and Discount.
Take a look at the form in Form View mode. As before in the Orders form, you will see
the Quantity, Unit Price, and Discount for each order. However, the problem is that you have
the order details for every single order with no distinction as to which order is which.
Return to your original Orders form. Go into Design View and add a Subform control.
This control icon is the fifth control from the left on the third row of icons in the Controls
group of the ribbon. It looks like a box with four bars and two rectangles inside it. If you
hover the cursor over it, it will display Subform/Subreport. You may need to enlarge your
form by dragging the borders before you add this control.
A Subform wizard will appear. You can use this, but it is often better to set the properties
manually, so click Cancel on the initial wizard screen.
On the Source Object property is a drop-down with a list of available forms. Select
subfrmOrderDetails (or whatever you called your subform when you prepared it). You
must have already made sure that both the main form and the subform have been bound to
a control source by setting the control source property on each form.
Click the Link Master Fields property and click the button with three dots on it. This will
display a form allowing you to choose how your subform will be linked to the main form.
You can have up to two links.
Access will attempt to create a logical link between the two forms, but it is well worth
checking this first.
The link is based on the relational fields within the record sources in the two forms, not
the actual fields displayed. In this particular example, the Order ID field in the Orders form
(which is the Master Field) is linked to the Order ID in the subform for Order Details (which
is the Child Field).
On the corresponding label box for the subform, you can provide a meaningful title such
asOrder Details.
Look at the Orders form in Form View mode. It should resemble Figure 9-3.
Notice that your Order Details subform sits in a window of its own within the main form
and has its own record selectors and record count.
Use the main record selectors at the bottom of the Orders form to browse through the
records. The main form shows each order in turn, but the subform window will show all the
products and quantities for that particular order. You can then use the record selector buttons
on the subform to browse through the various products for that order, and even add or delete
rows in the Order Details table.
You may wish to show your subform as a datasheet view (see next section) so that the user
can see several detail rows at once. To do this, close the main Orders form, since you cannot
make changes to the subform while it is open in the parent form.


Chapter 9: Forms and Reports 103

Free download pdf