chapter 8: user interface labfaculty.strose.edu/avitabij/cis321fall06/lab/chapter8.doc · web...

79
Chapter 8: User Interface Lab Learning Objectives This chapter covers Access 2003 features that allow you to define a user interface for your database. At the completion of this chapter, you should have acquired the knowledge and skills to Create a switchboard form using the Switchboard Manager. Create a hyperlink to browse a Data Access Page. Create command buttons on forms using the Command Button Wizard. Create macros with actions and conditions. Understand the concept of a module and gain experience writing procedures with the Visual Basic for Applications language Overview In other lab chapters you learned how to create the objects of a database application system. You created tables, queries, forms, reports, pivot tables, and Data Access Pages. This

Upload: others

Post on 04-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

Chapter 8: User Interface Lab

Learning ObjectivesThis chapter covers Access 2003 features that allow you to define a user interface for

your database. At the completion of this chapter, you should have acquired the knowledge and

skills to

Create a switchboard form using the Switchboard Manager.

Create a hyperlink to browse a Data Access Page.

Create command buttons on forms using the Command Button Wizard.

Create macros with actions and conditions.

Understand the concept of a module and gain experience writing procedures with the

Visual Basic for Applications language

OverviewIn other lab chapters you learned how to create the objects of a database application

system. You created tables, queries, forms, reports, pivot tables, and Data Access Pages. This

chapter shows you how to build a user interface to combine and customize these objects.

This chapter demonstrates a number of tools to support easy access to database objects

and customization of database objects. You will learn how to create a switchboard form to

access the database objects that you created in previous chapters. You also will gain experience

using command buttons that link forms together. Macros allow you to customize the way a

database object responds to user actions. In addition, macros reduce user time by consolidating

the steps for routine actions. You will create a simple macro as well as more complex macros

that use conditions. For more detailed customization, modules provide more flexibility than

Page 2: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

macros although module coding is more difficult than specifying the steps of a macro. You will

learn about Visual Basic for Applications (VBA), the language for modules, as well as write

several procedures to provide VBA practice.

8.1 Navigation among Database ObjectsWhen a database contains many forms and reports, a menu or switchboard form listing

these choices for the user is helpful. When Access starts, the switchboard form initially opens to

allow convenient navigation among database objects. From this form, a user clicks a command

button that opens a specified form or report. To return to the switchboard form, the user closes

the form or report. Otherwise, the user clicks another command button to open another form or

report. This section demonstrates these navigation features of Access.

8.1.1 Using the Switchboard ManagerA menu or switchboard form is simply a blank form that contains command buttons with

identifying labels. A user clicks a button and the appropriate form or report opens. A

switchboard form may be created in design view with a title added to the form header and

command buttons added on the form using the toolbox. However, a faster way to build a

switchboard form is to use the Switchboard Manager. Similar to a wizard, the Switchboard

Manager prompts you with a sequence of dialog windows.

When you create a form with the Switchboard Manager, Access creates the Switchboard

Items table and the Switchboard form. The Switchboard Items table contains details about the

command buttons on the form such as the action to take when a button is clicked. The

Switchboard form contains the actual command buttons but not the action details. Access uses

the Switchboard Items table to operate the Switchboard form.

2

Page 3: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

To change a switchboard form, you can use form design to change the appearance of the

form and the buttons. To change the action details, you should use the Switchboard Manager to

edit an existing switchboard form. Do not use form design to change action details. Keep in

mind that the Switchboard Manager has certain limitations. For example, a switchboard form

can contain at most eight command buttons. To add more, you need to make another

switchboard form. You would probably want to rearrange the command buttons between the

original switchboard form and a new switchboard form. In addition, you would want to have a

button on the original switchboard form that opens the new switchboard form. The instructions

below will help you make a switchboard menu for the auto repair database.

You should have the Database window on the screen with the Forms section open.

Follow these steps to build a switchboard form:

1. Open the Switchboard Manager Window : On the Tools menu, click Database Utilities

Switchboard Manager (Figure 1). A dialog box appears stating that there is no valid

switchboard in this database and asks if you would like to create one. Click Yes.

2. Open the Edit Switchboard Window : In the Switchboard Manager window (Figure 2), click

Edit… to open the Edit Switchboard window (Figure 3).

3. Enter Menu Name : In the Edit Switchboard window, type “Auto Repair Shop Database

Menu” in the Switchboard Name area (Figure 4). Click the New button to open the Edit

Switchboard Item window (Figure 5).

3

Page 4: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 1: Selecting the Switchboard Manager from the Database Utilities Menu

4

Page 5: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 2: Switchboard Manager Window

Figure 3: Edit Switchboard Window

Figure 4: Switchboard Name

5

Page 6: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 5: Default Edit Switchboard Item Window

4. Add Switchboard Item : Using the Edit Switchboard Item window, you add command

buttons that open the forms and reports.

In the text area, type the caption for the first command button, “Enter Customer”.

In the command area, click the arrow and select “Open Form in Edit Mode”. Note that

the last area in the window just changed from “Switchboard” to “Form”.

In the form area, click the arrow and select the “Customer” form (Figure 6).

Click OK to return to the Edit Switchboard window. This first item is now shown in the

switchboard items list (Figure 7).

Figure 6: Modified Edit Switchboard Item Window

Figure 7: Item Added to the Switchboard

6

Page 7: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

5. Add Remaining Command Buttons : In the Edit Switchboard window, click New to open the

Edit Switchboard Item window again. Repeat step 4 until all of the items from Table 1 are

added to the switchboard.

6. Edit or Delete : If you need to edit or delete an item in the Edit Switchboard window, click

the item in the list and then click Edit or Delete. If you want to rearrange items, click the

item and then click Move Up or Move Down. If you have already returned to the Database

window, then select Tools Database Utilities Switchboard Manager. When the

Switchboard window opens, click Edit… to open the Edit Switchboard window and follow

the above instructions.

7. Close Switchboard : When you are finished, close the Edit Switchboard window and the

Switchboard Manager window.

8. Modify Form in Design View : From the Database window, return to the Forms section.

Select the Switchboard form and click Design. Select the “AutoRepair” label and open its

Properties window. Change the Caption property to “Auto Repair Shop”. If you do not care

for the default color (green) background, simply select both parts (there are two parts) and

delete them.

9. View the Menu : Toggle to form view. Your menu should appear as in Figure 8. Close the

menu and save the changes.

7

Page 8: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Table 1: Command Buttons in the Switchboard Form

Text Command Form or Report

Repair Orders Open Form in Edit Mode RepairOrder2 (Form)

Edit Parts List Open Form in Edit Mode Part (Form)

Enter Vehicle Open Form in Edit Mode Vehicle (Form)

Complete Part Report Open Report Part Report3 (Report)

Monthly Part Report Open Report Part Report4 (Report)

Parts Expense Report Open Report Parts Expense Report (Report)

Figure 8: Auto Repair Shop Menu

8.1.2 Creating a Hyperlink in a Command ButtonNow that you have created a Data Access Page (DAP) in the previous chapter, you will

create a button linking your Switchboard form to a DAP. This button acts as a hyperlink to

connect to the Part Report DAP. To perform this linkage, you will create a command button and

set its hyperlink address property.

8

Page 9: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

1. Insert Link on the Switchboard Form : Open the Auto Repair Database window in the Forms

section area. Select the Switchboard menu form and open it in design view. From the

toolbox, select the Command Button tool and place a new command button on the bottom

of the Switchboard form (Figure 9). When the Command Button Wizard opens, cancel it.

2. Set Command Button Properties : Select the new command button, right mouse click and

choose Properties. Open the All tab and set properties as follows:

– Name : “Data Access”.

– Caption : “Part Report DAP”.

– Hyperlink Address : Click inside this property area and click the ellipsis (…) button to

open the Edit Hyperlink window (Figure 10). Click the Object in This Database button

on the left side to select an object in this database to which to link the command button.

On the right side, click the “Pages” object to expand the menu downward (Figure 11),

and choose “Part Report”. Click OK when finished.

Figure 9: Switchboard Form in Design View with New Command Button

9

Page 10: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 10: Edit Hyperlink Window

Figure 11: Edit Hyperlink Window in Object in This Database Section

10

Page 11: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

3. Try the Button: Toggle to form view and click the button. The Part Report Data Access Page

should open. (Note: It may take some time to load.) When you are finished viewing the page,

close it to return to the Switchboard menu form.

Technical Note: In form design, you can create a general hyperlink by clicking the Insert menu and

selecting Hyperlink, or by clicking the Hyperlink button on the toolbar to open the Edit Hyperlink

window. This action will insert a label into a form and bind the label to a web page.

8.1.3 Displaying the Switchboard Form at StartupAfter designing a switchboard form, you want to make it appear when your database

opens. In addition, you want to hide the Database window to prevent users from viewing it.

After hiding the Database window, you can make it reappear by clicking Windows Unhide.

Follow the steps below to display the Switchboard form when the database opens and hide the

Database window by default:

1. Open the Startup Window : From the Database window, select Tools Startup.

2. Modify the Startup Window : In the Display Form area (Figure 12), click the arrow and

select “Switchboard”. Directly under that area, click the Display Database Window box to

clear it (Figure 13). You do not want the Database window to appear. Click OK to close the

startup window. Note that this change becomes effective the next time you open the

database.

11

Page 12: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Any time you need to open the database to work on it (after the Switchboard form is in

startup mode), remember to use the Window Unhide to bring the Database window

back to the screen.

Figure 12: Startup Window

Figure 13: Modified Area in Startup Window

The Startup window supports other customization choices for database applications. You

can control the full menus, shortcut menus, toolbars, and shortcut keys available when a

database opens. For more details about the options available in the Startup window, use the

search term “startup” in the Answer Wizard.

8.1.4 Adding Command Buttons to FormsA switchboard form provides navigation when a database opens. To provide navigation

when using a particular form or report, you can add command buttons. Access provides the

Command Button Wizard to help you create buttons that initiate actions when clicked. The

12

Page 13: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

wizard can generate code to perform a variety of actions such as opening database objects

(forms and reports) and navigating among records. You will use the Command Button Wizard to

generate code to open forms and reports. The following instructions guide you to adding

command buttons to several forms that you previously created.

1. Open the Customer Form : Return to the Forms section in the Database window and open the

Customer form in design view.

2. Add the First Command Button : In the toolbox, click the Control Wizards tool to

activate a Control Wizard that will open when the appropriate control tool is selected. Next,

click on the Command Button tool in the toolbox. A small box icon appears with the

crossbar cursor. Place a command button directly under the subform as shown in Figure 14.

After releasing the mouse button, the Command Button Wizard appears automatically.

3. Select Command Button Action : The first wizard window (Figure 15) asks you to choose a

category and an action. In the Categories column select “Form Operations” and in the

Actions column select “Open Form” and click Next.

4. Select Form to Be Opened : The next window asks you to identify the form to open (Figure

16). Select “Vehicle” and click Next.

5. Show Form Records : The third wizard window asks if a specific record is to be shown when

the form is open or if all records will be available. Select the second choice (Figure 17),

“Open the form and show all the records”, and click Next.

6. Command Button Format : You are asked if you want text or a picture to appear on the

button. Select “Text” and type “Vehicle Form” (Figure 18). Click the Next button to

continue.

13

Page 14: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

7. Name the Command Button : This final wizard window (Figure 19) asks you to name the

command button. Type “OpenVehicle” and click Finish. A new command button appears on

the Customer form (Figure 20).

8. Try the Command Button : Toggle to form view and click the button to see if the Vehicle

form opens. When you close the Vehicle form, you are returned to the Customer form.

Toggle to design view.

Figure 14: Initial Layout of the Command Button

14

Page 15: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 15: First Window of the Command Button Wizard

Figure 16: Second Window of the Command Button Wizard

15

Page 16: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 17: Third Window of the Command Button Wizard

Figure 18: Command Button Text Entry

16

Page 17: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 19: Final Window of the Command Button Wizard

Figure 20: Customer Form with a New Command Button

9. Add the Second Command Button : Follow steps 2–8 above and place a new button next to

the Vehicle Form button (Figure 20). Make the button open the RepairOrder2 form. The

caption on the button should be “Repair Orders” and the name should be “OpenRepairOrd”.

If the button is not the same size as the Vehicle Form button, adjust it after you finish with

the wizard.

17

Page 18: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

10. Add the Return to Menu Command Button : Place a third button next to the Repair Orders

button. Make the button open the Switchboard form (choose “Form Operations” and “Open

Form” in the first wizard window). The caption on the button should be “Return to Menu”

and the name should be “OpenMenu”. When you are finished, toggle to form view and try

the buttons. When finished, close the form and save the changes.

11. Add Command Buttons to Other Forms : Refer to Table 2 to place command buttons on the

forms by following steps 2–8 above. Note, for a command button to open a report, choose

“Report Operations” and “Preview Report” in the first wizard window. When you are

finished with each form, toggle to form view and try the buttons (Figures 21 to 24). When

finished, close each form and save the changes.

Table 2: Command Buttons to Add to Forms

Form Object to Open Button Caption Button Name

Vehicle Customer Customer Form OpenCustomer

Vehicle RepairOrder2 Repair Orders OpenRepairOrd

Vehicle Switchboard Return to Menu OpenMenu

RepairOrder2 Customer Customer Form OpenCustomer

RepairOrder2 Vehicle Vehicle Form OpenVehicle

RepairOrder2 Part Report3 Complete Part Report OpenPartRept

RepairOrder2 Part Report4 Monthly Part Report OpenMoRept

RepairOrder2 Switchboard Return to Menu OpenMenu

Part Part Report3 Complete Part Report OpenPartRept

Part Part Report4 Monthly Part Report OpenMoRept

Part Switchboard Return to Menu OpenMenu

18

Page 19: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 21: Revised Customer Form

19

Page 20: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 22: Revised Vehicle Form

20

Page 21: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 23: Revised RepairOrder2 Form

21

Page 22: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 24: Revised Part Form

8.2 Working with MacrosAccess supports two tools, macros and modules, to automate repetitive tasks and

customize the way that forms and reports react to user actions. Macros are the older and simpler

tool for automation. Access provides a convenient environment for specification of the actions

and the parameters in a macro. Since many applications developed with early versions of Access

rely on macros, you should understand the basics of macro. The next section describes modules,

a more flexible tool that may require significant coding.

This section guides you to formulate several macros of increasing complexity. Section

8.2.1 presents background on macros along with simple macros with parameters. Section 8.2.2

presents macros with conditions to refine when macros execute.

22

Page 23: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

8.2.1 Creating Simple MacrosA macro is a name given to a sequence of one or more actions that perform a common

task. You formulate a macro by specifying its name, its actions, and the parameters or arguments

for each action. To more easily manage macros, you can store them in a group. Since you will

develop only a few macros in this chapter, the macro group feature is not used.

An event triggers or causes a macro to execute. You indirectly have used the Click event

for common buttons through the Command Button Wizard. The wizard generated VBA code

that executes when a user clicks on a button. Determining the proper event for a control is not

always so easy. Events apply to every kind of control including the form itself. Sometimes the

same event applies to multiple controls. To determine the proper event for a macro, you may

need to carefully study the meaning of events for available kinds of controls. The help

documentation provides a detailed explanation of each event including when the event occurs

and what kinds of actions are typically used with the event.

Overview of Macro Tools

As an introduction to the macro development environment, let’s begin with an empty

macro window. To start a new macro, click the New button in the Macros tab of the Database

window. The starting point for a macro usually is to select the action in the drop-down list in the

Action column (Figure 25). The macro toolbar supports manipulation of the Macro window as

listed in Table 3. When you are finished viewing the empty Macro window and toolbar icons,

close the window.

23

Page 24: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 25: New Macro Builder Window

Table 3: Description of Macro Toolbar Icons

Icon Meaning

Adds the macro names column to the macro sheet. Also use ViewMacro Names.

Adds the condition column to the macro sheet. Also use ViewConditions.

Inserts one or more new rows above the selected row. The number of rows selected determines the number of inserted rows. Also use InsertRows.

Deletes the selected rows. Also use EditDelete Rows.

Executes the macro. Also use RunRun.

Single step execution of the macro. Also use RunSingle Step.

Formulating Your First Macro

The first macro refreshes the Serial No. combo box in the RepairOrder2 form.

Remember that this combo box uses a query that references a field on the main form. In Chapter

5, you had to manually requery the combo box by using Records Refresh. This macro

relieves a user from manually refreshing for each record in the main form.

24

Page 25: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

In this macro, you will use the On Current event. The On Current event occurs when a

new record obtains “focus” (becomes current) or when a form is refreshed. Making a record

current means that the record has the ability to receive user input. The On Current event allows

the Serial No. combo box to be automatically requeried when the customer number value

changes on the main form. Access executes the macro just before the new record is displayed.

Recall that the query for the Serial No. combo box uses the customer number value on the main

form to restrict the vehicles displayed. Because the customer number field is read-only on the

form, the only way to change the customer number value is to advance to a new record.

The following steps instruct you how to formulate a macro to requery the Serial No.

combo box on the RepairOrder2 form:

1. Open the RepairOrder2 Form and Its Properties Window : In the Forms section of the

Database window, select the RepairOrder2 form and open it in design view. Open the Form

Properties window and click the Event tab.

2. Open the Macro Builder : Click in the On Current event property and click again on the

ellipsis (…) button to open the Choose Builder window. Select Macro Builder and click

OK. When prompted to name the macro, type “ReQuerySerialNoCombo” and click OK.

3. Create the Macro : In the Action column of the window, click the arrow and scroll down to

select “Requery”. In the Control Name parameter area below, type “SerialNo” as shown in

Figure 26. Close the window and save the changes when prompted.

4. Try the Macro : Toggle to form view to see the effects of the macro. Click the Serial No.

combo box to see the list of vehicles for the customer on the main form. To see the effect of

the macro, advance to a record with a different customer number. Click the Serial No. combo

box to see that the list reflects the vehicles owned by the current customer.

25

Page 26: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 26: Macro Builder Window for SerialNo Macro (resized smaller)

You also can also use a macro with the RunCommand action and the “Refresh” argument

instead of the Requery action and “SerialNo” argument. For refreshing combo box queries in

subform fields, the Requery action causes a run-time error. The RunCommand action works well

for subform fields, however.

Formulating a Macro with a More Complex Action

Many macros use more complex actions than the Requery action. More complex actions

such as the OpenForm action involve a number of parameters. The OpenForm action uses

parameters that specify the form to open, the view of the form when opened, a filter name, a

condition to restrict the form’s records, the data mode of the form, and the window mode. To

facilitate macro development, Access provides default values for many parameters. You are

encouraged to understand the meaning of each parameter to ensure that default values are

appropriate.

The next macro involves a command button on the RepairOrder2 form. You will replace

the current Click event procedure in the Customer Form command button with a macro. The

event procedure for this button opens the Customer form when clicked by the user. To customize

the action of this button, you will create a macro to retrieve a specific customer’s record when

the Customer form opens, rather than retrieving all customer records. However, if there is no

26

Page 27: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

record for a customer (a null value), such as for a new customer, then all records will still be

displayed. Follow the instructions below to formulate this macro:

1. Open the RepairOrder2 Form : Select the RepairOrder2 form and open it in design view.

Double-click the button with the caption “Customer Form” to open its Properties window.

Click the Event tab to see the On Click property.

2. Open the Macro Builder : Because you had previously defined an event procedure using the

command button wizard, you need to delete the event procedure before you can define a

macro for the On Click event. To delete the event procedure, select “[Event Procedure]” in

the On Click event and use the Delete key to remove it. Then, click on the ellipsis (…)

button to open the Choose Builder window. Select Macro Builder and click OK. When

prompted to name the macro, type “OpenCustomer” and click OK.

3. Create Macro : In the Action column of the window, click the arrow and scroll down and

select “OpenForm”. In the parameter area below, set parameter values as follows:

Form Name : Click inside the Form Name area, click the arrow, scroll down, and select

“Customer” (Figure 27).

View : Click inside this area, click the arrow, scroll down, and select “Form”.

Filter : Blank.

Where Condition : Click inside this area and then click the ellipsis (…) button to open the

Expression Builder window. Type the following condition in the Expression Builder

window, then click OK:

[Forms]![RepairOrder2]![CustNo]=[CustNo] Or [Forms]![RepairOrder2]![CustNo] Is Null

Data Mode : Select “Edit” from the list.

Window Mode: Select “Normal” from the list.

27

Page 28: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Close Window : Save the changes when prompted.

4. Try the Macro : Toggle to form view. When you click the Customer Form command button,

the record with the current customer number should appear when the Customer form opens.

Return to the RepairOrder2 form to test the other condition with a null customer number.

Place the RepairOrder2 form in data entry mode using the Records menu. Click the

command button again to open the Customer form with all records available.

·

Figure 27: Macro Builder Window for Customer Form Button Macro

8.2.2 Adding Conditions to MacrosSometimes you want to restrict a macro’s execution beyond the occurrence of an event.

To refine when a macro executes, you can add conditions. If you add a condition to a macro, the

macro executes when the event occurs and the condition is true. One macro presented in this

section uses a condition to restrict its execution. This section also demonstrates a second macro

that works with the first macro to update a related field.

The new macros involve the subform Parts: Subform2 of the RepairOrder2 form. Recall

that this subform displays the parts used on a repair order (Figure 28). This subform can change

records in the PartsUsed table. However, changing the QtyUsed field of a record in the

PartsUsed table should cause a change in the UnitsInStock field of the Part table. Currently, the

28

Page 29: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

subform does not update the In Stock form field. You need to write macros to update the In

Stock field if there is sufficient stock to meet the requested demand.

Figure 28: Parts: Subform2 Embedded in RepairOrder2

The macros involve the Before Update and After Update events of the Quantity Used

field in the subform. Because the Before Update event occurs just before updating a control with

changed data, it supports complex validation rules. This event is not triggered if the data in a

control are not changed. The After Update event occurs just after the update has been processed.

This event supports changes to related fields. These macros work together so that if the

validation rule in the Before Update event is violated, the update action in the After Update

event does not execute.

The condition in the Before Update macro is true if there is not sufficient stock to meet

the requested demand. If the number of additional units required is greater than the quantity in

stock, then display a message box to alert the user and cancel the update. The number of

additional units is computed as the new Quantity Used textbox value minus the old Quantity

Used value. You will use the OldValue property to reference the old value. Simply using the

field name references the new value. The CancelEvent action removes the effect of a change.

The After Update event does not occur if the CancelEvent action has been executed in

the Before Update macro. Thus, the After Update event requires no condition. The first action of

29

Page 30: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

the After Update event updates the In Stock textbox with the change in quantity used. The

change is computed as the current In Stock value minus the number of additional parts required.

The number of additional parts required is the same as described in the previous paragraph.

The After Update macro contains one additional action to save the subform record using

the RunCommand action and the “SaveRecord” parameter value. Saving the record is necessary

to update the OldValue property. If the OldValue property is not updated to reflect the update to

the Quantity Used textbox, subsequent changes before saving the record will not update the In

Stock textbox correctly.

After you have understood the motivation and the description of these macros, you are

ready to formulate them. The instructions to formulate the macros are given in three parts. First,

you will create the Before Update macro. Next, you will create the After Update macro. Finally,

you will modify the subform to work properly with the macros.

Before Update Macro

Open the Properties Window: In the Forms section of the Database window, select the Parts:

Subform2 and open it in design view. Select the QtyUsed textbox and open its Properties

window to the Event tab.

Open the Macro Builder: Click in the Before Update event property and click again on the

ellipsis (…) button to open the Choose Builder window. Select Macro Builder and click

OK. When prompted to name the macro, type “ValidateQtyUsed” and click OK.

Add Condition Column : Since you will insert a condition, you must add a Condition column.

Click the View Conditions. (Note, you also can point on the blue Macro Builder title bar,

right-click, and select Conditions.)

30

Page 31: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Enter Condition: In the Condition column (in the same row), right-click and select the

expression builder (Magic Wand icon and Build…) from the drop-down menu. Type the

following text in the Expression Builder window and then click OK:

[Quantity Used]-NZ([Quantity Used].[OldValue])>[In Stock]

The NZ function converts a null value into 0. Null values make the result of the condition

unpredictable. The OldValue property is null for new records. In Figure 25, the Condition

column has been widened to display the entire condition.

Set Message Box Action: Select “MsgBox” as the action in the first row of the Action column.

In the area below, set parameter values as follows:

Message : Type “Quantity Used change exceeds units in stock”.

Beep : “Yes”.

Type : Click inside this area, click the arrow, scroll down, and select “Warning!”.

Title : Type “Quantity Used Change”.

Document the Condition: Enter text in the Comment column to document the condition.

Set CancelEvent Action and Condition : In the second row of the Action column, click the arrow

and select “CancelEvent”. In the Condition column of the same row, type three dots (…) and

then click OK (Figure 29). The three dots mean that the condition in the previous row

applies to this row of the macro. It provides a convenient way for multiple actions to apply

to the same condition. Save the macro when finished and return to the QtyUsed Properties

window.

31

Page 32: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 29: Macro Builder with Condition, Message Box Action, and Comment

After Update Macro

1. Open the Macro Builder : Click in the After Update event property and click again on the

ellipsis (…) button to open the Choose Builder window. Select Macro Builder and click

OK. When prompted to name the macro, type “UpdateStock” and click OK.

2. Set the SetValue Action : In the first row of the Action column, click the arrow and select

“SetValue”.

3. Set Argument Values : In the area below, set argument values as indicated in the list below.

For your reference, see Figure 30.

Item : In the Expression Builder window, type the following text and then click OK:

[In Stock]

Expression : Inside this area, click the ellipsis (…) button to open the Expression Builder

window, type the following expression, and then click OK:

[In Stock]-([Quantity Used]-NZ([Quantity Used].[OldValue]))

32

Page 33: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

4. Set RunCommand Action and Argument Values : In the second line of the Action column,

click the arrow and select “RunCommand”. In the argument area below, choose

“SaveRecord” from the list. Figure 31 shows the resulting macro.

5. Enter Comment : Enter a comment to document the action. When finished, save the macro

and return to the QtyUsed Properties window.

Figure 30: Macro Builder with SetValue Action, Arguments, and Comment

Figure 31: Macro Builder with RunCommand Action, Arguments, and Comment

33

Page 34: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Make Subform Changes and Execute Macros

1. Change UnitsInStock Properties : Select the UnitsInStock textbox and open its Properties

window. Change the Enabled property to “No” and the Locked property to “Yes”. These

properties prevent a user from changing the value. Only the After Update macro can change

the value.

2. Execute the Macros : Open the main form of the RepairOrder2 form in form view. In the

subform, enter a new value for a part in the Quantity Used column. A corresponding change

should occur in the In Stock column. If you enter a value in which the additional parts

required exceed the In Stock value, a message box should appear as shown in Figure 32.

Close the form and save the changes when prompted.

Figure 32: Message Box when Quantity Used Exceeds Units In Stock

8.3 Working with ModulesIn contrast to macros, modules require coding in Visual Basic for Applications (VBA), a

full procedural language available with all Microsoft Office applications. Macros, although

easier to develop, lack the flexibility of modules and execute more slowly than modules. In

addition, Microsoft has not committed to supporting macros in future versions of Access. Thus,

understanding modules is important for developing database applications in Access.

For advanced development, you can use Visual Basic .Net instead of VBA. Visual

Basic .Net is a separate development environment for Web applications. The tools available in

34

Page 35: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

VBA to develop Web applications are not necessarily intuitive or easy to use. Microsoft Visual

Basic .NET, when used with the Microsoft .NET Framework, provides tools and related

technologies that facilitate Web application development for Access databases. The usage of

Visual Basic .Net is beyond the scope of this lab book. For more information, the technical

articles in the Access section of the MSDN Office Developer Center.

This section guides you to formulate several modules of increasing complexity. To

contrast with macros, Section 8.3.1 explains background about modules, while Section 8.3.2

provides background about the VBA language and environment. Section 8.3.3 presents

procedures (components of modules) similar to the macros presented in the previous section.

Because modules require programming expertise that some users of this book may lack, the

material in this section only provides an introduction. You are encouraged to study books on

Access programming for more details.

8.3.1 Understanding ModulesModules are more efficient but more difficult to develop than macros. Modules are

usually more efficient because they can be compiled rather than interpreted as macros are.

Modules are more difficult to develop because they are written in Visual Basic for Applications

(VBA), the programming language used in all applications of Microsoft Office. VBA is a full

programming language with variables, conditional statements such as the if statement, and

looping statements such as the for statement. In addition, VBA provides a set of procedures to

reference database objects. You can use embedded SQL inside VBA to access and update tables.

There is a considerable learning curve to master VBA. Once mastered, however, you can be

productive in writing code.

35

Page 36: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Modules consist of two types: class modules and standard modules. A class module is

typically a form or report module containing event procedures. An event procedure consists of

VBA code that executes when an event is triggered. Standard modules typically contain

procedures and functions related to multiple database objects.

The Command Button Wizard creates a class module when you select actions for a

command button. However, the module does not appear in the Modules section of the Database

window. If you program a command button manually, it is saved in the Modules section of the

Database window. Figure 33 shows a new (empty) module window from the Database window.

Figure 34 shows the event procedure of the Return to Menu button from the Part form.

However, the Return to Menu button module was saved as an event procedure in the On Click

event property rather than in the Modules section of the Database window.

Figure 33: New Module Window

36

Page 37: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 34: Event Procedure for the Return to Menu Button of the Part Form

8.3.2 The VBA Language and EnvironmentBecause this section only provides an introduction to VBA, a complete description of the

language and environment is not given. Language elements used in the examples are explained

assuming that you have prior experience with computer programming. To become a skilled

database programmer, you need to obtain a detailed understanding of modules and VBA. You

are encouraged to seek other sources for your study.

The VBA language is an object-based procedural language. Objects represent parts of an

application such as the primary Access objects (tables, queries, forms, reports, macros, and

modules) as well as components of those objects such as controls on a form. Objects contain

properties (characteristics such as the Enabled and the Locked status of a control) and methods

37

Page 38: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

(procedures that perform actions). In a VBA procedure, you can access most of the objects and

properties defined in forms and reports.

To fully utilize its object collection, VBA provides the full range of procedural

statements. VBA is considered a complete programming language with all the power of other

contemporary languages. Because much of the functionality of VBA is contained in its object

collection, the statements are relatively easy to understand. Table 4 summarizes the syntax of the

most significant statements. If you have studied other programming languages, the VBA syntax

should be easy to learn.

Table 4: Description of Selected VBA Statements

Statement Meaning and Example

DIM Declares a variable of a specified type.

DIM SalaryVariable AS CurrencyDIM MaritalStatus AS String

IF … THEN … ELSE Allows execution of statements based on conditions.

IF SalaryVariable > 10000 SalaryVariable = SalaryVariable * 1.1ELSE SalaryVariable = SalaryVariable * 1.2ENDIF

SELECT CASE Executes one of several statements depending on the value of a test expression.

SELECT CASE MaritalStatus CASE "Single" DeductionFraction = 0.1 CASE "Married" DeductionFraction = 0.25END SELECT

FOR … NEXT Executes a group of statements a specified number of times.

FOR Counter = 1 TO 10 Product = Product * CounterNEXT Counter

DO … LOOP Executes a group of statements while a condition is true or until a condition becomes true.

DO WHILE TimeRemain > 0 TimeRemain = TimeRemain – 0.5LOOP

Access provides a number of tools to help you write code. Most of the code that you

write resides in procedures that are associated with events such as mouse clicks. Some tools are

38

Page 39: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

available as menu choices while other tools are available as you enter code. To view the menu

tools, you need to open a module. Follow these steps to open a module.

1. Open the Properties Window of the RepairOrder2 Form : After opening the RepairOrder2

form in design view, open the Properties window of the form.

2. Open the Code Builder : In the Event tab, select the ellipsis (…) button for the On Activate

event and choose “Code Builder” from the list, as shown in Figure 35.

3. View the Empty Procedure : The class module window opens to reveal an empty procedure

named “Form_Activate”, as shown in Figure 36. The second button in the lower left-hand

corner indicates the full module view. Click the first button in the lower left-hand corner to

display the procedure view (Figure 37). The full module view shows global variables that are

hidden in the Procedure View.

Figure 35: Choose Builder Window

Figure 36: Empty Form_Activate Procedure in Full Module View

39

Page 40: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 37: Empty Form_Activate Procedure in Procedure View

With the empty procedure open, you can view the menu tools. Click the Edit, View,

Insert, Debug, and Run menus to see the lists of tools. Table 5 provides a brief explanation of

the most important tools. Figures 38 and 39 display the menus.

40

Page 41: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 38: VBA Edit and Debug Menus

41

Page 42: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 39: VBA View, Insert, and Run Menus

42

Page 43: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Table 5: Description of Selected VBA Menu Items

Menu Item Description

Edit List Properties/Methods

Displays a list of relevant objects, properties, and methods in the Module window.

Edit List Constants

Displays a relevant list of constants for methods and functions.

Edit Quick Info Provides the syntax for the programming item (variable, function, statement, method, or procedure) selected in the Code window

Edit Parameter Info

Displays documentation about the parameters of the selected function or statement.

View Definition Shows the definition of an identifier in the object browser.

View Object Browser

Displays a list of objects and associated properties and methods in a window.

View Immediate Window

Displays the Immediate window that allows you to type and execute code.

View Locals Window

Displays the Locals window that shows the variables in the current stack and their values.

View Watch Window

Displays the Watch window that shows the current watch expressions.

Debug Compile X Compiles the modules of the current form (X).

Debug Add Watch Allows expressions to be entered that will be watched in the Debug window.

Debug Toggle Breakpoints

Sets and unsets breakpoints. A breakpoint is a place in the code where execution stops.

Run Sub/UserForm

Runs the current procedure if the cursor is in a procedure or runs the form if a form is currently active.

Run Break Stops executing the procedure and returns to break mode.

8.3.3 Coding Your Own ProceduresAfter this background, you are ready to use the VBA environment to code procedures.

This section presents several procedures similar in complexity to the last macros. Several

additional tools will be introduced as you code. Before beginning the next subsection, close the

module to return to the Properties window. Then, close the RepairOrder2 form but do not save

the changes.

Procedures for Updating the Units in Stock

You will first code procedures to perform the same tasks as the Before Update and After

Update macros. You will use the code builder instead of the macro builder and enter VBA

43

Page 44: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

statements instead of conditions, actions, and arguments. The code in these procedures uses the

VBA statements, functions, methods, and properties as depicted in Table 6.

Table 6: VBA Elements Used in the BeforeUpdate and AfterUpdate Procedures

VBA Element Description

NZ function Converts null value into 0.

DoCmd object A special object that performs macro actions in a VBA procedure.

MsgBox statement Displays a window containing a warning or informational message.

Single quotation mark Denotes a comment. You can place comments on a separate line or on the same line as a statement.

RunCommand method Executes a command from a menu or toolbar. The constant after the method name identifies the command to execute.

1. Copy and Paste Forms : In the Forms section, copy and paste RepairOrder2 as

RepairOrder3. Then copy and paste Parts: Subform2 as Parts: Subform3. Open

RepairOrder3 in design view when you are finished.

2. Change Subform Properties : Change the Name and the Source Object properties of the

embedded subform. Click on the embedded subform and open its Properties window.

Change both property values to “Parts: Subform3”. Double-click the embedded subform to

open it in design view.

3. Open the Code Builder for the Before Update Event : In the Properties window of the

Quantity Used textbox, delete the macro name (ValidateQtyUsed) in the Before Update

event. Click on the ellipsis (…) button and select “Code Builder” in the Choose Builder

window. Click the OK button to continue.

4. Examine the Empty Procedure : The class module window opens to reveal an empty

procedure. Click the first button in the lower left-hand corner to display the procedure view

(Figure 40).

44

Page 45: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

5. Enter the First Code Line : Type the first part of the IF statement as shown below followed

by a carriage return.

If (Quantity_Used.Value - Nz(Quantity_Used.OldValue)) > In_Stock.Value Then

6. Use the Properties/Methods List : On the next line, type “DoCmd.” (notice the period at the

end of “DoCmd”). The Properties/Methods list displays allowing you to choose a property or

method. This feature provides a convenient memory aid. Type “C” and the list positions to

“CancelEvent”, as shown in Figure 41. Type a space to choose the CancelEvent method.

7. Complete the Coding : Use Figure 42 as a guide to complete the coding. Notice that the first

three lines are comments.

8. Compile the Code : To ensure that your code does not have syntax errors, click Debug

Compile Loaded Modules. If the compiler detects errors, use Figure 42 to help correct your

code. When you are finished, close the module to return to the Properties window of the

Quantity Used textbox.

9. Create the Empty AfterUpdate Procedure : In the After Update event, delete the existing

macro name (UpdateStock). Click the ellipsis (…) button to open an empty procedure

(Figure 43).

10. Code the AfterUpdate Procedure : Using Figure 44 as a guide, code the procedure. Notice

that you can use the Properties/Methods list for the textbox names (“In_Stock” and

“Quantity_Used”). After typing or selecting the RunCommand method (last code line),

notice that a list box appears showing constant values. You should choose

“acCmdSaveRecord” from the list. Close the module when you are finished. Close the

subform to return to the main form.

11. Execute the Procedures : Open the main form of the RepairOrder3 form in form view. In the

subform, enter a new value for a part in the Quantity Used column. A corresponding change

45

Page 46: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

should take place in the In Stock column. If you enter a value in which the additional parts

required exceed the In Stock value, a message box should appear. Close the form and save

the changes when prompted.

Figure 40: Empty BeforeUpdate Procedure in Procedure View

Figure 41: Properties/Methods List for the DoCmd Object

Figure 42: Complete BeforeUpdate Procedure

46

Page 47: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 43: Empty AfterUpdate Procedure

Figure 44: Complete AfterUpdate Procedure

Procedures for Deletion of Subform Records

The macros and procedures for the Before Update and After Update events handle

updates to existing and new records. In each case, the UnitsInStock field of the related record in

the Part table is updated to reflect the change in quantity used. However, deletion of a subform

record does not add the quantity used back to the units in stock. To accomplish this task, you

will write code for another pair of procedures and add hidden textboxes to the subform.

One difficulty with updating the units in stock after a deletion is knowing the part

number and the quantity used of the deleted subform record. These values must be saved before

the deletion occurs and then referenced to perform the update after the deletion. To save the

values, you will add “hidden” textboxes to the subform. You will code a Click procedure for the

subform that saves the part number and the quantity used in the hidden textboxes. Before

47

Page 48: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

deleting a subform record, the entire record must be selected. Thus, the subform’s Click

procedure seems to be a good place to save the part number and the quantity used.

A second difficulty with updating the units in stock is that you cannot just update the

corresponding form field. Deleting the subform record makes the form field unavailable. Instead

you must use the application programming interface (API) known as the ActiveX Data Objects

(ADO) to perform the update. The ADO is a collection of objects that you can use inside VBA

procedures to query, modify, and manage databases. In this section, you will be introduced to

the ADO through a simple example. For more information about the ADO, consult the chapter

“Microsoft ActiveX Data Objects” in the Contents section of the Access help documentation and

the Microsoft Developers Network website (msdn.microsoft.com).

You will write code to manipulate ADO objects in the After Del Confirm event. This

event occurs just after the user confirms the deletion and the records are deleted from the buffer.

The After Del Confirm event is typically used to update related records and recalculate totals

affected by a deletion.

The code for the After Del Confirm event of the subform uses the ADO objects and other

VBA elements described in Table 7. The code for the Click event of the subform only uses

assignment statements.

Table 7: VBA Elements Used in the After Del Confirm Procedure

VBA Element Description

Connection object ADO object that represents an open connection to a data source

CurrentProject object Access object that refers to the project for the current Access database

RecordSet object ADO object that represents a set of records retrieved from a base table or executed SQL statement

Open method A method of the Recordset object that opens a cursor representing records from a base table, the results of a query, or a previously saved Recordset.

Close method A method that closes the associated Connection or Recordset object.

CSTR function Converts a number to a string.

48

Page 49: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

To create the procedures, use the following steps. In addition to creating procedures, the

steps include adding hidden textboxes to the subform.

1. Create the QtyUsedTemp Textbox : Open the Parts: Subform3 subform in design view. Using

the toolbox, create an unbound textbox in the footer section (Figure 45). Set its properties as

follows:

Name : “QtyUsedTemp”.

Visible : “No”.

Locked : “Yes”.

Enabled : “No”.

2. Create the PartNoTemp Textbox : Using the toolbox, create a second unbound textbox in the

footer section (Figure 45). Set its properties as follows:

Name : “PartNoTemp”.

Visible : “No”.

Locked : “Yes”.

Enabled : “No”.

3. Enter the Code for the Subform’s On Click Event : Open the Properties window for the

subform. In the On Click event, select the ellipsis (…) button and choose “Code Builder”

from the list. Type the VBA statements shown in Figure 46. Only type the statements

between the procedure heading and End Sub. Save and compile the procedure before you

exit the procedure window.

4. Enter the Code for the Subform’s After Del Confirm Event : In the Properties window of the

subform, open the code builder for the After Del Confirm event. Type the VBA statements

shown in Figure 47. Save and compile the procedure before you exit the procedure window.

49

Page 50: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Technical Note: If a compilation error occurs, you need to load the ADO library. When you are inside

the Code Builder, select Tools References… to open the References window. Check the Microsoft

ActiveX Data Objects Library (there also may be a version number such as 2.5) inside the References

window. Close the References window and recompile your code.

5. Execute the Procedures : Open the main form of RepairOrder3 in form view. In the subform,

select a subform record and notice the part number, the quantity used, and the units in stock

values. When you delete the subform record, you should see two message boxes confirming

the deletion of the PartsUsed record and the update of the UnitsInStock column of the

related record in the Part table. To confirm that the update worked correctly, select the same

part number in a new subform record. The UnitsInStock column should be increased by the

quantity used in the deleted subform record. Close the form and save the changes when

prompted.

Figure 45: Subform with Unbound Textboxes Added

50

Page 51: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Figure 46: Click Procedure for the Subform

Figure 47: AfterDelConfirm Procedure for the Subform

Closing ThoughtsThis chapter has covered features that allow you to create a user interface for your

database. You have learned how to create a switchboard form for navigating among your

database objects. For direct navigation, you created command buttons to link forms and reports.

51

Page 52: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

To navigate directly from an Access form to a Data Access Page, you created a button with a

hyperlink.

You also have seen how macros and procedures can save time by consolidating steps for

routine actions. You defined simple macros and also a more complex macro containing

conditions. You defined several procedures and learned about the programming environment for

the Visual Basic for Applications (VBA) language.

After completing this chapter, you should have a good understanding of all database

objects in Access. You have seen that Access is a powerful desktop DBMS. You created a

moderate-size database with powerful forms and reports. Most application development work

was accomplished with no coding. The ease of developing powerful database application

systems is a strong feature of Access. Many other DBMSs require more coding to build such

powerful applications. If you outgrow the limitations of Access databases, your applications can

connect to an enterprise DBMS such as Microsoft SQL Server.

To complete your database and Access background, you are encouraged to develop

additional database application systems. In developing other systems, you will cement the

database concepts and practice emphasized in the textbook and the lab manual. You also are

encouraged to explore beyond this chapter, especially to increase your knowledge of VBA. With

the solid background of database concepts and the practice provided by the textbook and the lab

manual along with additional study, you will become a highly skilled Access developer and

database professional.

52

Page 53: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Chapter ReferenceThe chapter reference section summarizes procedures that you practiced. For wizards

discussed in the chapter, the procedures highlight important parts of the wizards but do not list

all of the steps.

Procedure 1: Creating a Switchboard Form (Section 8.1.1)1. Open the Switchboard Manager window by clicking on Tools Database Utilities

Switchboard Manager. A dialog box may appear stating that there is no valid switchboard in this database and asks if you would like to create one. Click Yes.

2. In the Switchboard Manager window, click Edit… to open the Edit Switchboard window. Type a name for the switchboard form. Click New to open the Edit Switchboard Item window.

3. Using the Edit Switchboard Item window, assign form, report, or other commands as items. For each item, you need to identify a caption, the action performed (usually opening a form or report), and the database object. Access only supports a maximum of eight items. If you need more items, create a multiple-level switchboard in which an item in the main switchboard opens another switchboard.

4. If you need to edit or delete an item, while in the Edit Switchboard window, click the item in the list and then click Edit or Delete.

5. If you want to rearrange items, select an item, and then click Move Up or Move Down.

6. If already returned to the Database window, then click Tools Database Utilities and choose Switchboard Manager. When the window opens, click Edit to open the Edit Switchboard window and follow the above instructions.

7. To make modifications to the appearance of the switchboard form, open it in design view and make changes to fonts, background color, and so on.

Procedure 2: Create a Command Button with a Hyperlink (Section 8.1.2)8. Drag a Command Button from the toolbox onto a form. When the wizard begins,

cancel it. Open the Command Button Properties window.

9. Go to the Hyperlink Address property, click inside the area, and click the ellipsis (…) button to open the Edit Hyperlink window.

10. In the Link to file or URL area, type the address for the hyperlink. The address can be a standard Internet address beginning with “http://” or a file in your hard disk. For additional choices, click a button on the left side to reveal files and database objects in which to link.

11. Toggle to form view and see if the hyperlink is active.

Note: In form design view, you can create a general hyperlink by clicking Insert Hyperlink or by clicking the Hyperlink button on the toolbar. This action will insert a label into the form that is bound to a hyperlink address.

53

Page 54: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Procedure 3: Displaying a Switchboard Form at Startup (Section 8.1.3)1. To open the StartUp window, click Tools Startup.

2. In the Display Form/Page area, click the arrow and select “Switchboard”. Directly under that area, unclick the Display Database Window box to clear it. You do not want the Database window to appear. Click OK to close the Startup window. Note: This change will go into effect the next time you open the database.

3. To access the Database window to make modifications to forms or reports, type F11 or ALT+F1. This action returns the Database window to the front. You also can click Windows Unhide.

Procedure 4: Formulating a Macro in a Form (Sections 8.2.1 and 8.2.2)1. Open the form in design view.

2. Determine the control and the event that will contain the macro. You may need to carefully study events for more than one kind of control. Open the Properties window for the control and click the Event tab.

3. Click in the desired event property and click again on the ellipsis (…) button to open the Choose Builder window. Select Macro Builder and click OK.

4. When prompted to name the macro, type in the name and click OK.

5. In the Action column of the window, click the arrow and scroll down to select the action of the macro.

6. In the area below, you may be asked to type in the name of the control or form object affected by the macro. You also may be asked to set additional properties.

7. Close the window and save changes when prompted.

8. Toggle to form view and test the macro for all conditions.

Note: If you are inserting a condition, you must add a Condition column to the Macro Builder window. Click View Conditions or point on the blue Macro Builder title bar, right-click, and select Conditions.

Procedure 5: Formulating a Procedure in a Form (Sections 8.3.3)1. Open the form in design view.

2. Determine the control and the event that will contain the macro. You may need to carefully study events for more than one kind of control. Open the Properties window for the control and click the Event tab.

3. Click in the desired event property and click again on the ellipsis (…) button to open the Choose Builder window. Select Code Builder and click OK.

4. Enter code in the empty procedure. Use the Properties/Methods and Constants lists to select objects, methods, properties, and constants in your code. Use comments to document the meaning of your code.

5. Close the window and save changes when prompted.

6. Toggle to form view and test the procedure for all conditions.

54

Page 55: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Additional PracticeThe following problems provide additional practice with the extended auto repair

database as well as the textbook databases.

Part 1: User Interface for the Extended Auto Repair Database

1. Revise the Switchboard form to have multiple levels as follows:

Add a switchboard page called “Forms Menu”. Add items to this switchboard page to

open the Customer, Vehicle, RepairOrder2, Part, Labor (the form created in problem (3)

of Chapter 4), and RepairLabor (the form created in problem (1) of Chapter 5) forms.

Add a switchboard page called “Reports Menu”. Add items to this switchboard page to

open the Complete Part Report, the Monthly Part Report, the Part Expense Report, and

the Labor Usage Report (the report created in problem (2) of Chapter 6).

Revise the “Auto Repair Shop Database Menu”. Remove the previous items on the menu

and add new items to open the “Forms Menu” page and the “Reports Menu” page.

2. Add command buttons to the RepairLabor form as follows:

Use the Command Button Wizard to add a button to open the Customer form on a record

containing the current customer number.

Without using the Command Button Wizard, add another button. Write a Click macro to

open the Vehicle form with the current serial number.

Using the Command Button Wizard, add a button to open the Labor Usage Report

created in problem (2) of Chapter 6.

Use the Command Button Wizard to add a button that opens the Switchboard form.

3. Add command buttons to the Vehicle and the Customer forms to open the RepairLabor form.

55

Page 56: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

Part 2: User Interface for the University Database in Textbook Chapter 10

1. Create a multiple-level switchboard form as you did in problem (1) for the extended auto

repair database.

2. Create command buttons to navigate among the forms and the switchboard.

3. Write a macro(s) to decrease the number of seats remaining in a course offering when a

student registers for a course offering. You will need to add a field to the Offering table for

the seats remaining and modify the registration form.

4. Write a procedure(s) to increment the seats remaining when a student drops a course

offering.

5. Write a macro to refresh the query for the OfferNo combo box in the subform. You should

use the RunCommand action with the Refresh command in the macro. Search for an event

that only executes the macro one time even if there are multiple rows in the subform.

Part 3: User Interface for the Order Entry Database in Textbook Chapter 10

1. Create a multiple-level switchboard form as you did in Part 1, problem (1) for the extended

auto repair database.

2. Create command buttons to navigate among the forms, the reports, and the switchboard.

3. Create a macro or procedure in the Order Form. The macro or procedure should copy the

billing data (customer name (first and last), street, city, state, and zip) into the corresponding

shipping fields (order name, street, city, state, and zip). The replication should occur after a

user enters a value for the customer number. The macro or procedure saves data entry time

as the billing (in the Customer table) and the shipment (in the OrderTbl table) data are

identical in most orders.

56

Page 57: Chapter 8: User Interface Labfaculty.strose.edu/avitabij/cis321fall06/lab/Chapter8.doc · Web viewLearning Objectives This chapter covers Access 2003 features that allow you to define

5/23/2023 Chapter 8: User Interface Lab Page

4. Create a macro in the Purchase Form to refresh the combo box query for the product number

in the subform. Refresh the query on an event that will occur only one time for the subform,

not one time for each subform record.

57