Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

COMM 335 – Winter 2019 Term 1

Tutorial 10: Microsoft Access - Main Menu, Totals Query and DCount Function

The purpose of this tutorial is to enable you to:

1) create a main menu (switchboard);


2) add a Total column in a query to calculate the total of a field; and
3) use the DCount function.

You should download the sample database, comm335_T10.accdb, from the UBC Canvas as the
foundation to build and test the forms and queries in this tutorial.

 Creating a Main Menu (Switchboard)

A main menu (switchboard) is a form which contains buttons to open various objects such as forms
and queries in a database. It allows users to open the forms and queries directly from the main menu
(switchboard) rather than searching and opening them in the database window.

The switchboard can be created using the Switchboard Manager. You supply the text of the item, as
it is to appear on the switchboard (e.g. Customer Form), together with the underlying command (e.g.
Open Form in Edit Mode) and the form name (e.g. frmCustomer). Access does the rest. It creates the
switchboard form and an associated Switchboard Items table.

When the switchboard form is opened in Form view, the text appears next to each button. However,
when the switchboard form is opened in Design view, the text for each button is not displayed. This is
the nature of a switchboard because the text for each button is taken from the Switchboard Items table.

When users open (i.e. run) the database, the Navigation Pane should be hidden and the main menu
(switchboard) form should appear automatically. For example, the following is the switchboard you
may create for the video rental system.

COMM 335 Winter 2019 Term 1 Tutorial 10 Page 1


Copyright © 2019 Y.M.Cheung
Demonstration:

 Look for Switchboard Manager in Access 2016 Ribbon if you do not have Classic Menu

You will find out that the Switchboard Manager feature disappeared from the Ribbon. In fact the
Switchboard Manager is not listed on the Ribbon. So you may need to customize the Ribbon, and
add Switchboard Manager under the Database Tools in the Ribbon.

Key Steps (Add Switchboard Manager to Command List):

a. Select File  Options to open the Access Options window.


b. Click the Customize Ribbon at the left pane.
c. In the Choose commands from dropdown box, select the Commands Not in the Ribbon
item.
d. Select the Switchboard Manager item in the command list box.
e. Expand the Database Tools in the Customize the Ribbon list.
f. Click the New Group button. Then click Rename.
g. Enter Switchboard in the Display name box. Click OK.

h. Select the new group Switchboard (Custom) that you just created.
i. Click Add button to add the Switchboard Manager to this group. (You have to make sure
that the Switchboard Manager is still selected in the command list box.)

j. Click OK to close the Access Options window.


k. Can you see the Switchboard Manager in the Ribbon under the Database Tools?

COMM 335 Winter 2019 Term 1 Tutorial 10 Page 2


Copyright © 2019 Y.M.Cheung
 Create a Main Menu (Switchboard) form linking to various forms in a database.

Key Steps (Part 1 - Create a switchboard form to open a form and exit from Access):

a. Select Database Tools  Switchboard Manager.


b. Click Yes if you see a message indicating that there is no valid switchboard.

c. On the Switchboard Manager dialog box, click Edit button to edit the Main
Switchboard.

d. On the Edit Switchboard Page dialog box, change the Switchboard Name as “Video
Rental System”.
e. Click New button to add an item to the switchboard.
f. Add the first switchboard item as follows:
 enter “Customer Form” in the Text box.
 choose “Open Form in Edit mode” from the Command drop-down box.
 choose “frmCustomer” from the Form drop-down box. Click OK.

g. Click New button to add the second switchboard item as follows:


 enter “Exit Application” in the Text box.
 choose “Exit Application” from the Command drop-down box. Click OK.

COMM 335 Winter 2019 Term 1 Tutorial 10 Page 3


Copyright © 2019 Y.M.Cheung
Key Steps (Part 1 – cont’d)

h. Click Close twice to close the dialog boxes.


Can you see a form named “Switchboard” and a table named “Switchboard Items”
created by Access?

i. Open the Switchboard form and test the buttons. Does it work?
j. Open the Switchboard form in Design View. Change the font size and color if necessary.

Practice:
 Add a new switchboard item to open the New Rental Form.
(Hint: Repeat Steps (a), (c), (e) and (f) for each switchboard item.)

Key Steps (Part 2 – Update the Startup menu to open the Switchboard form):

k. Select File  Options to open the Access Options window.
l. Click Current Database at the left pane.
m. In the Application Options section, type the database name, e.g. Video Rental System, in
the “Application Title” text box.
n. Click the “Display Form” drop-down box and select “Switchboard”.
o. In the Navigation section, clear the “Display Navigation Pane” check box.

p. Click OK to close the dialog box. (The next time the database is opened, the switchboard
will be displayed automatically.)
q. Close the database and open it again to check whether the Main Menu (Switchboard) form
is opened automatically. Does it work? (Press F11 to open the Navigation Pane.)

COMM 335 Winter 2019 Term 1 Tutorial 10 Page 4


Copyright © 2019 Y.M.Cheung
 Adding a Total column in a Query to calculate the Total of a
Field
Demonstration:
 Calculate the total “Rental Charge” by customer in a query.

Key Steps:
a. Create a query (qryTotalRental) from tblCustomer and tblRental including MemberNo,
LName and FName fields. (You may either use Query Wizard or Query Design.)
b. Open the query in Design View.
c. Add a calculated field named “Charge” by entering the following expression in a new
column:

Charge:([DateIn]-[DateOut])*[Rate]*(1-[Discount])

d. Save and open the query. Does it work? (Correct any typo errors if you see an error
message or “Enter Parameter Value” window.)
e. Switch to Design View.

f. Click the Totals command in the Ribbon to show the column totals in the query
result. The Total row appears in the design grid.

g. Go to the Total row of “Charge” column. Switch “Group By” to “Sum”.


h. Save and open the query. It shows the total rental charge by Customer. Does it work?
i. Switch to Design View and add the DateOut field to the design grid.
j. Save and open the query. It should show the total rental charge by Customer and
DateOut. Does it work?

COMM 335 Winter 2019 Term 1 Tutorial 10 Page 5


Copyright © 2019 Y.M.Cheung
 Using DCount Function

Use the DCount function to determine the number of records that are in a specified set of records
(a domain.) This function can be used when writing expressions in a form or macro.

Syntax: DCount ( "[field name]", "table name", "criteria" )

where: (1) "[field name]" is the name of a field in a table (or a query) that identifies the field
for which you want to count records.

(2) "table name" is the name of a table (or a query) identifying the set of records that
constitutes the domain.

(3) "criteria" is an optional string expression used to restrict the range of data on which
the DCount function is performed. If criteria are omitted, the DCount function
counts the records against the entire domain.

Demonstration:

 Build a macro and use the DCount function to check whether there are any rental records belonging
to a customer before deleting the customer in the Customer Form.
– If there are rental records associated with the customer selected in the Customer Form, then
display a meaningful warning message.
– If there are no rental records associated with the customer, delete the customer record from the
tblCustomer table and display a confirmation message.
– Make sure that the following system error message will not be displayed.

 Create a command button to run the above macro.

Key Steps (Part 1 – Create a new Macro):

a. Select Create  Macro


b. Enter the conditional expression and actions as shown in the figure on page 7 below.
c. Save the macro as “mcrDeleteCustomer”.

Key Steps (Part 2 – Create a Command Button to run the Macro):

d. Open the Customer Form (frmCustomer) in form design view.


e. Add a command button to run this macro. Choose “Miscellaneous” under Categories and
“Run Macro” under Actions. Click Next.
f. Select the macro “mcrDeleteCustomer” and click Next.
g. Choose Text and type “Delete Customer” and click Next. Click Finish.
h. Save and test the form. Does it work?

COMM 335 Winter 2019 Term 1 Tutorial 10 Page 6


Copyright © 2019 Y.M.Cheung
Conditional expression using DCount function

Display a warning message if


there are rental records belonging
to the customer selected in the
“Customer Form” and then stop
the macro.

Question:

1. When should we use the DCount function?

- end of document -

COMM 335 Winter 2019 Term 1 Tutorial 10 Page 7


Copyright © 2019 Y.M.Cheung

You might also like