You are on page 1of 3

4.

7 Bound main form and subform

In many cases we want to bind a main form (a window) to the database. One example is the stay window. It should
show data about a single stay and guest (Figure 4.7). On the stay window, we also have a list of rooms, and it should
show the rooms for this stay.
As you see, we need two queries: one to combine stay and guest to supply data for the main Stay form; an-other to
extract room data for the list of rooms. We will now outline how to do it. We will shorten the explana-tion a bit and
leave it to you to open and close things properly.
Finish the main form
1. Make a query, qryStay (not shown on the figure). It must join tblGuest and tblStay. It should include all of the
fields, since they will all be needed somewhere on the Stay form. The easiest way is to drag the two stars from
the tables to the grid. Don't worry that we get two guestID fields - one from each table. We actually need them
in the later pro-grams.
2. Bind to qryStay. You should have made a simple version of frmStay already (section 3.4). Now set its Record
Source to qryStay in order to connect it to the query.
3. Connect the existing main-form controls to the query. For instance the Stay No. field should have its
ControlSource set to stayID, in order to show this query field in the text box.
4. Add fields. Add a few more fields to the main Stay window, for instance the name, address and state. A
convenient way to do it is to use the small Field List window that may have popped up when you connected the
form to the query. Otherwise open the Field List window through the View menu or with the Field List icon
next to the hammer icon. Drag a field to the form to create a text box and associated label. Access will
automatically bind the text box to the query field.
5. For combo boxes, such as pay method and state, make the first column width zero to let the user see the
mnemonic text rather than the number code.

Have a look at the stay window in user mode. It should show the first stay. You can use PageUp and Page-Down to
browse through all the stays. You can edit guest and stay data through the window since the dy-naset behind the
form allows it.
In the final system, the user should be allowed to edit data this way, but it makes no sense to allow him browsing
through thousands of stays this way. We will later see how to control this and the creation of new stays (section
5.5.2).
Make the subform
We will now make the subform that shows the rooms for the stay.
1. Make a query, qryStayRooms. It is a join of three tables: tblRoomState, tblRoom, and tblRoomType (Figure
4.7).

The result of the raw query would be a list of all room states with data added about the room type and the prices. For
a long stay, this would give an awfully long list of room states with one line per day. We want to shorten it to one
line per room. In each line we need to show the first date the room is used and the number of nights it is used in
total.
The query should give a result like the datasheet to the right in Figure 4.7. Here is a description of the fields in the
query result. Try to define the query on your own.
stayID: Not shown in the continuous form, but needed to bind the list properly to the main form.
roomID: Not shown directly in the continuous form, but needed by the program to find out what the user has
selected.
From: The first date this room was used by the stay.
Nights: The number of nights this room was used by the stay.
Room: A computed text string consisting of roomID, a comma, and the description from tblRoomType.
Persons: The number of persons actually staying in the room.
Price: The price - taking into account a possible dis-count for one person staying in a double room. More
precisely, when the number of actual persons is one and the bedCount is more than one, price2 should be used.
Otherwise price1.
Total: The total price for the room for all these nights.
Binding the subform to the main form
1. The query is to be shown in the subform on the Rooms tab. First make a continuous form based on
qryStayRooms. Use the Form Wizard and omit stayID and roomID from the continuous form. Since the field
names above are user-oriented, the continuous form should automatically look correct in datasheet view.
2. Connect the continuous form to the subform con-trol on the Rooms tab. When you now look at the Stay window
in user mode, you will see that the subform shows all rooms in the database.
3. To show only the rooms related to the stay, set these properties on the Data tab for the subform control: Link
Child Fields = stayID Link Master Fields = stayID.

66 4. Queries - computed tables


In this way you specify that you only want to see those child records (in qryStayRooms) where stayID matches
stayID in the master form.
Now the stay window should look right. Try browsing through the stays and notice how the room lists vary from one
stay to another.
4.7.1 Editing a GROUP BY query

What kind of actions can the user make on a complex query table such as qryStayRooms? Actually very little. It
would make sense to edit the number of persons in the room, but since the room list is made by a Group By, this is
impossible. There are two ways out:
. Dialog box. Provide an edit-button that the user can click when he has selected a rooms line. The user will
then see a dialog box where he can enter the number of persons and possible other editable data. When he closes the
button, the VBA program updates the corresponding room state records.
. Store query result in a temporary table. The alternative is to store the query result as a new, temporary
table. (SQL can do this by means of an INSERT INTO query. See section 7.1) Then you show this table instead of
qryRoomState. Since this is a table and not a query, the user can edit it. Next, the VBA program will have to transfer
the changes to tblRoomState.

Programming issues such as these are the reason many software applications don't allow the user to edit di-rectly in
what he sees, but offer him a dialog box inFig 4.7 Bound main formWanted resultQuery forthe
subformQuery subformRecordSource = qryStayRecordSource = qryStayRoomsLink to main form stayID

You might also like