Chapter 3: Building a Simple Desktop and Web Application Page 1 of 17

Chapter 3: Building a Simple Desktop and Web Application Understanding Access’s Approach to Application Design

Unlike other members of Office XP, Access 2002 requires that you build an application to take advantage of the product’s power as a development platform. Word 2002 and Excel 2002 let you automate simple repetitive operations by recording Visual Basic for Applications (VBA) macros. Access 2002 supports a set of commands for compatibility with previous versions, but Access macros don’t use VBA. Access doesn’t capture your mouse clicks and keystrokes and turn them into a series of macro commands or VBA code. It’s up to you to design and implement the Access applications you need for your database projects.

A full-scale Access application involves at least the following three basic Access object types:

z Tables that store the data you or others add to the database

z Forms for displaying and entering data, controlling the opening and closing of other forms, and printing reports

z Reports to print detail information, summary information, or both in tables

Most Access applications also use Query objects to filter, sort, and combine your data, and Module objects to store VBA code. Access 2002 forms can (and usually do) contain VBA code in a special type of Module object, called a Class Module. All objects that make up your application are stored in a container called a Database object, which is a single file with an .mdb extension, such as Northwind.mdb. Access is unique in that it can store an entire database application in a single file. Other desktop , such as Microsoft Visual FoxPro, require multiple files to store their objects.

New Access users often find it difficult to "get a grip" on how to start developing a self-contained database application. Dealing with an unfamiliar set of objects tends to intimidate first-time database developers. Fortunately, Microsoft includes with Access 2002 various wizards that guide you, step by step, through complex tasks. One of the most accomplished of the Access wizards is the Database Wizard that creates a typical Access 2002 "starter" application from a set of prefabricated database templates. In this chapter, you use the Database Wizard to create a relatively simple but useful Contact Manager application. Then you explore the objects generated by the Wizard to gain perspective on the relationship of Access objects and learn how they’re integrated within a typical Access database application.

If you’re upgrading from Access 97 to 2002, the following features introduced by Access 2000 are discussed in this chapter:

z Subdatasheets, which open when you click a plus sign to the left of the first field in Datasheet . Subdatasheets automatically display records of another table that’s related to the current table.

z Visual Basic for Applications code editor, which Access 2002 shares with other Office applications. Access 97 and earlier used its own VBA editing window for code modules.

file://J:\Alpha\chapters\JW225. 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 2 of 17

NOTE

This chapter assumes that you’ve already installed Access 2002. If you haven’t, see the "Access 2002 Installation" section of Chapter 1, "Access 2002 for Access 97 and 2000 Users: What’s New."

Creating an Access Application from a Template File

When you launch Access 2002, the new Office 2002 task pane appears at the right of the main window with the default New File page active. The New File page offers you the following options:

z Opening an existing database, such as Northwind.mdb, the Northwind Traders sample database that’s located in \\\Office10\Samples.

z Opening a new (blank) database.

z Creating a copy of a database from an existing Access file.

z Using the Database Wizard to generate a new database from one of the 10 database templates included with Access 2002. Template files contain the definitions of Table, Form, and Report objects, plus the VBA code required to automate interaction of these objects.

To use the Database Wizard to create a sample application from an Access 2002 template, follow these steps:

1. Choose Programs, from the to launch Access 2002 with the New File task pane visible. If you don’t see the New File task pane, choose View, , Task Pane to display it.

2. Click the General Templates link of the New from Template group (see Figure 3.1) to open the Templates dialog.

3. Click the Databases , and select one of the 10 database templates (*.mdz files) to build your new application from (see Figure 3.2). Access stores its templates in the \Program Files\Microsoft Office\Templates\1033 folder. This example uses the Contact Management.mdz template. Click OK to open the File New Database dialog.

4. The wizard proposes a default database with the name of the template plus a 1 suffix. The default location of the database file is your folder. Accept the default name or shorten it to Contacts.mdb (see Figure 3.3). Click Create to generate an empty Jet 4.0 database file (Contacts.mdb) and start the Database Wizard.

5. The first dialog of the Database Wizard describes the type of information that the new database stores (see Figure 3.4). Click Next to continue.

6. The second Wizard dialog lets you add or remove optional fields to each of the three tables. Optional fields appear in italic type in the list. To add or remove an optional field of the Contact

file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 3 of 17

Information table, mark or clear the check box (see Figure 3.5). Check the Home Phone field and click Next to continue.

Figure 3.1 Double-clicking the General Templates link under the New from Template heading of the task pane opens the Templates dialog with the Databases page active.

Figure 3.2 Select the Contact Management template in the Databases page of the Templates dialog.

Figure 3.3 Shorten the default database file name, Contact Management1.mdb to Contacts.mdb.

Figure 3.4 The first Database Wizard dialog offers a brief description of the new database.

Figure 3.5 Add the optional Home Phone field of the Contact Information table of Contacts.mdb.

NOTE

You can remove only the optional fields you add. You can’t remove the standard set of fields that appear in Roman type; you receive an error message if you try.

7. The third Wizard dialog offers a selection of text colors and background colors or images for the data forms that the wizard creates. As you select an item in the list, a sample of the style appears in the dialog (see Figure 3.6). Select a form style and click Next to continue.

Figure 3.6 Select the text color and background color or image for data-entry forms.

8. The fourth dialog lets you choose a style for printed reports (see Figure 3.7). Pick a report style, such as the conservative Soft Gray, and click Next to continue.

Figure 3.7 Select the header and type styles for reports in the fourth wizard dialog.

9. You enter the title to appear on your forms and reports in the fifth Wizard dialog (see Figure 3.8). The default title is the name of the template file. Accept or edit the title.

10. If you have a bitmap (.bmp or .dib, device-independent bitmap) or vector (.wmf, Windows metafile, or .emf, enhanced metafile) image of a logo that’s of a size suited to the header of your reports, mark the Yes, I’d Like to Include a Picture check box. (The logo should be about 80x80 pixels or less.) Click the Picture button to open the Insert Picture dialog. Select the image file and then click OK to insert the image and close the Insert Picture dialog.

file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 4 of 17

Figure 3.8 Accept the default title to appear on forms and reports.

11. Click Next to open the final Wizard dialog (see Figure 3.9). With the Yes, Start the Database check box marked, click Finish to add the objects whose properties you’ve specified to the database file you named in step 4.

Figure 3.9 Click Finish in the final Database Wizard dialog to start generation of the Contacts.mdb database.

A set of progress bars displays the Wizard’s actions. The time required to complete the generation of database objects depends on your CPU’s and disk drive’s speed; it takes less than 10 seconds to finish the Contact Management database with a 667MHz Pentium III PC and a high-speed fixed-disk drive. After the Wizard completes its work, the Main Switchboard form for the completed Contact Management application appears, as shown in Figure 3.10. The Database window, which lets you manually open any of the objects in the Contacts database, is minimized at the bottom left of the display.

Figure 3.10 When the Database Wizard completes its task, the Main Switchboard form opens. Touring the Contact Management Application

The Contact Management application appears complex to most first-time Access users. The Database Wizard generates Table, Form, Report, and Module objects in the new database. The following sections explain the purpose of each object in the context of your new Contact Management application.

Table Objects in the Database Window

Tables are the foundation of Access databases. To examine some Table objects generated from the Contact Management template by the Database Wizard, do the following:

1. Click the Database window’s Restore button to open the Database window in Normal mode. An Outlook-style shortcut bar appears at the left of the list.

NOTE

By default, Access 2002 saves database files in Access 2000 format for backward compatibility. Using Access 2002 format is advantageous only for very large database applications.

2. Click the Tables shortcut to display a list of the three Create Table... options and the four Table objects in the Contacts database (see Figure 3.11).

Figure 3.11 The Tables page of the Database window displays the Table objects of Contacts.mdb.

file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 5 of 17

3. Double-click the Contacts item to open the Contacts table in Datasheet view. The fields of the Contacts table correspond to the items in the list of the second Wizard dialog (refer to step 6 in the preceding section). The first field is an AutoNumber field that sequentially numbers the records you add; you can’t change the value of an AutoNumber field.

4. Type a test contact entry in the initially visible fields of the Contacts table. When you type in the First Name cell, a pencil symbol appears to the left of the Contact ID cell, indicating an edit in process, and a new —called the tentative append record in this book—appears below the test contact record (see Figure 3.12). Click in any cell of the tentative append record to save your entry; you must move to another row in the datasheet to ensure that entries add to the Contacts table.

Figure 3.12 The Datasheet view of the Contacts table displays a test entry.

5. Minimize the Contacts datasheet, return to the Database window, and double-click to open the Contact Types table. In the Contact Type field, type a typical title, such as Executive. Add records with Director, Manager, and Supervisor as Contact Types. Click in a cell of the first record to save your last entry, and then close the Contact Types window.

6. Restore the contacts table, and use the scroll bar to display the rightmost fields. Click the Contact Type ID cell of the first record of the table to display a drop-down list button in the grid. Click the button to open a list with items you added to the Contact Types table (see Figure 3.13).

Figure 3.13 The drop-down lookup list of the Contact table's Type ID field displays choices for the field value.

–› If you’re not comfortable navigating the interface yet, see "Understanding Access’s Table Display,".

NOTE

The Contact Type ID field of the Contacts table is related to the Contact Type ID field of the Contact Types table, which contains the numeric values 1 (Executive), 2 (Director), 3 (Manager), and any additional records you added in step 5 of the preceding list. Relations between tables are the foundation of relational database management systems (RDBMSs). The between the Contacts and Contact Types tables is called a many-to-one relationship because many records in the Contacts table can relate to a single record in the Contact Types table. The Contact Type ID field of the Contacts table, which actually contains numeric value 1 or 2, is called a lookup field because it looks up data in the Contact Types table based on the numeric value and substitutes the corresponding text value for the number. You can change the Contact Type ID from Director to Manager by selecting Manager from the drop-down list.

Additionally, inserting entries from a lookup field is a popular method for avoiding spelling errors and reducing the number of keystrokes for repeated selections.

file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 6 of 17

–› For other ways to use this method of data entry, see "Using Lookup Fields in Tables,".

7. Scroll to the first field (ContactID) of the table and click the plus (+) sign at the left of the ContactID field to open a subdatasheet that displays an empty Calls datasheet for your test contact entry. (You might need to click the ContactID field to display the plus sign.) Subdatasheets display records of other tables related to the currently open table. Type entries typical of a phone call in the subdatasheet fields (see Figure 3.14). The Calls table has a many-to-one relationship to the Contacts table.

Figure 3.14 The subdatasheet lets you add new records to the related Calls table.

8. Choose Tools, Relationships or click the ’s Relationships button to display graphically the relations between the Contact Types, Contacts, and Calls tables in Access’s Relationships window. Drag down the bottom of the Contacts list to expose the ContactTypeID field (see Figure 3.15).

Figure 3.15 The Relationships window shows how the Contact Types, Contacts, and Calls tables of the Contacts.mdb database relate.

NOTE

The line between the ContactTypeID fields of the Contact Types and Contacts tables illustrates a many-to-one relationship. (The arrowhead identifies the "one" table in the relation.) The line between the ContactID fields of the Contacts and Calls tables also indicates a many-to-one relationship. (The infinity symbol, 8, identifies the "many" side and the 1 represents the "one" side of the relation.) The ContactID line indicates that Access enforces the of the relationship.

–› For details on how relationships and referential integrity protect your data, see "Maintaining Data Integrity and Accuracy,".

TIP

The field names in the Relationships window and the field names that appear in the Datasheet views of the corresponding tables aren’t the same. The field names in the Datasheet views are captions, which include spaces for readability. The actual field names, which don’t include spaces, appear in the field lists of the Relationships window.

9. Close the Relationships and Contacts Table windows, saving your layout changes.

The Switchboard Form file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 7 of 17

The Main Switchboard is the controlling form of the Contact Management application (refer to Figure 3.10). Switchboard forms take the place of the conventional menu choices of Windows applications. The five buttons on the Main Switchboard perform the following functions:

z Enter/View Contacts opens the two-page Contacts form.

z Enter/View Contact Types opens a small form for adding additional records to the Contact Types table.

z Preview Reports opens the Reports Switchboard page, which lets you preview and print an Alphabetical Contact Listing Report or a Weekly Call Summary Report or return to the Main Switchboard. Preview Reports is equivalent to choosing Print Preview from the File menu.

z Change Switchboard Items opens the Switchboard Manager form, which lets you customize the Switchboard pages, add a new page, or delete a page.

z Exit This Database closes the Contacts database but doesn’t shut down Access.

Figure 3.16 shows the relationship between the buttons on the three versions of the Switchboard and the forms and reports that make up the Contact Management application. For clarity, this diagram omits the Call Details Subform, Call Listing Subform, and Report Date Range form. A subform is a form that’s contained within another form; subforms are unique to Access. Lines between forms and tables with arrows on each end indicate the capability to display and edit table data. Lines between reports and tables have only a single arrow, because reports involve only reading table data.

Figure 3.16 This diagram shows the relationship of Switchboard buttons to forms, reports, and tables of the Contact Manager application.

–› For help building a new subform, see "Creating a Transaction-Processing Form with the Form Wizard."

NOTE

Figure 3.16 shows three individual Switchboard forms. Contact Manager uses records in the Switchboard Items table to customize a single Switchboard form to perform the three functions shown in the diagram.

Compared to menu commands, switchboards offer more control over the sequence of user interaction with data display and entry forms. The Main Switchboard acts as a home base that you return to on completion of one or more specific tasks. Simplified navigation of multiple layers of forms is the reason that many Access developers use switchboards or their equivalent for complex applications.

Access Forms

The following steps introduce you to Access forms and form-based data entry: file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 8 of 17

1. Close all open Access windows except the Switchboard form and the Database window.

NOTE

If you accidentally closed the Switchboard form, restore the Database window, click the Forms shortcut, and double-click the Switchboard item in the list to open the Main Switchboard.

2. Click the Enter/View Contacts button to open the Contacts form, which displays in text boxes most of the data you entered in the first record of the Contacts table (see Figure 3.17). The record displayed in the form is called the current record of the table.

Figure 3.17 On opening, the upper part of the Contacts form displays most of the information for the first record of the Contacts table.

3. Click the Next Record button (with the right-pointing triangle) at the bottom left of the form to display successive records of the Contacts table. If you added only a single Contacts record, the text boxes empty in preparation for adding a new record.

4. Click the First Record button (with the left-pointing triangle and bar) to return to the first record you typed.

5. Click the 2 button to show the bottom part of the form, which displays the data for the remaining fields of the Contacts table. You also can navigate to the bottom of the display with the form’s scroll bar. Clicking inside the Notes text box adds a scroll bar to that box (see Figure 3.18). Click the 1 button to return to the top of the form.

Figure 3.18 Clicking the 2 button moves to the lower part of the Contacts form, which displays additional contact data.

6. Press the Tab key 10 times to move the focus to the Work Phone field. (The Tab key is the primary method of navigating through a form’s fields.) Click the Dial button to open the AutoDialer form of the Utility.mda library (see Figure 3.19). Clicking the Setup button opens the or Windows NT Modem Properties sheet. Close the Modem Properties sheet, if you opened it, and then click Cancel to close the AutoDialer form.

Figure 3.19 The AutoDialer form is a part of the Ultility.mda library database that’s attached when you launch Access.

NOTE

file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 9 of 17

AutoDialer uses the Windows 98 or /NT built-in Phone Dialer accessory. AutoDialer detects whether the call is within your local dialing area (set by the Dialing Properties sheet that you open from the Modem Properties sheet). The Phone Dialer prepends the required 1 digit to the area code and telephone number when dialing long distance.

7. Click the Calls button to open the Calls form, which superimposes the Contacts form. The Calls form displays only the records in the Calls table for the contact selected in the Contacts form, a process called synchronizing forms (see Figure 3.20). The Calls form must display multiple records, so the Calls form uses a Datasheet view of the Calls table.

Figure 3.20 The Calls form synchronizes to the active (current) record of the Contacts form.

8. Add a new Calls record for the Contacts record you entered earlier by clicking the Subject field of the tentative append record and adding some text. When you start typing text, a new tentative append record appears.

9. Click inside the Call Notes text box and type a short transcript of the call (see Figure 3.21). The Call Notes caption and text box are contained in the Call Details Subform.

Figure 3.21 A new record added to the Calls form.

10. To delete the record you just added, click the gray record selector button to the left of the Call Date field and press Delete. You receive the warning message shown in Figure 3.22.

Figure 3.22 You receive a warning message before permanently deleting a record.

NOTE

Office XP no longer installs the Office by default, so conventional Windows warning messages replace the ’s intrusive pop-up window.

11. Close the Calls form to return to the Contacts form.

12. If you want to add a new contact record to the Contacts form, click the Tentative Append record navigation button (also known as the New Record button) to open a form in which only the Contact ID field is filled in (see Figure 3.23).

13. Close the Contacts form to return to the Main Switchboard.

Figure 3.23 file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 10 of 17

Click the Tentative Append button to add a new record to the Contacts table.

NOTE

The Contact ID field uses the AutoNumber field data type, which automatically assigns the next number in sequence to an added record. If you don’t enter text in any field and then move the record pointer with the navigation buttons to an active record, the empty record isn’t appended.

To delete a record, click the record selector bar at the left of the form and press the Delete key. If you delete a record of the Contacts table that has related records in the Calls table, the related Calls records are deleted simultaneously, a process called cascading deletions. The related Calls records are deleted because referential integrity is enforced between the Calls and Contacts tables, and cascading deletions are specified in the properties of the relation in the Relationships window.

Clicking the Enter/View Other Information button regenerates the Forms Switchboard. To add a new contact type, click the Enter/View Contact Types button to open the simple Contact Types form. Like Contact ID, Contact Type ID is an AutoNumber field. Close the Contact Types form and then click the Return to Main Switchboard button to return the Switchboard form to its original status.

Access Reports

Reports are one of Access’s strongest selling points. The capability to program the generation of complex, fully formatted reports sets Access apart from its competitors, including Microsoft’s own Visual Basic. To preview and optionally print the two reports of the Contact Management application, follow these steps:

1. Click the Preview Reports button to generate the Reports Switchboard, and then click Preview the Alphabetical Contact Listing Report to open the small Print Preview window in Normal mode. Fit appears in the toolbar’s Zoom list.

2. Type or select 75% in the Zoom list to view almost all the report (see Figure 3.24).

Figure 3.24 The Print Preview window of the Alphabetical Contact Listing report displays the contacts you enter prior to printing the report.

3. To print the report, click the toolbar’s Print button.

4. Close the Print Preview window, and click the Restore button of the Switchboard form. Click the Preview the Weekly Call Summary Report button to open the Weekly Call Summary form. The default beginning and ending report dates are for the current system date and the preceding six days.

5. Edit the date in the Begin(ning) Call Date and the Ending Call Date text boxes to a range of dates file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 11 of 17

for which data is available, if necessary. You receive an error message if there are no Call records within the specified date range.

6. Click the Weekly Call Summary form’s Preview button to open the report in Print Preview mode (see Figure 3.25).

Figure 3.25 The Weekly Call Summary report displays a chronological list of sales calls.

7. Print a copy of the report if you want, and then close the Print Preview window and click the Return to Main Switchboard button.

Access Modules

Access modules contain VBA subprocedures and functions that are accessible to VBA code contained in the class module of any form or report. You also can call VBA functions in modules by using the RunCode action of an Access macro. Access 2000 was the first version of Access to take advantage of the shared VBA editor used by other Office applications. The chapters in Part VII, "Programming and Converting Access Applications," focus on writing VBA subprocedures and functions to automate your Access applications.

To see and test a simple example of VBA code for a function, follow these steps:

1. Choose Window, 1 Contacts: Database to open the Database window.

2. Click the Modules tab and then double-click the Global Code item in the list to open the Global Code module in the VBA code editor. The Global Code module contains a single function, IsLoaded (see Figure 3.26), which returns True if a specified form is open (loaded) or False if not.

Figure 3.26 The Contact Management template includes VBA code for the IsLoaded user-defined VBA function.

3. To test the IsLoaded function, press Ctrl+G to open the Immediate window. Type ? IsLoaded ("Switchboard") in the bottom pane of the Immediate window. The VBA debugger returns -1 (the numeric value of the VBA True intrinsic constant), indicating that the Switchboard form is open (see Figure 3.27).

Figure 3.27 The value returned by executing the IsLoaded function from the Immediate window depends on the value of the strFormName argument you pass and the name of the currently active form.

4. Close the Immediate window, Alt+Tab over to Access, and minimize the Database window to return to the Main Switchboard. Using the Switchboard Manager

The Switchboard Manager lets you modify the navigation process for your application. The following steps delete the Forms Switchboard and substitute a button that opens the Contact Types form directly: file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 12 of 17

1. Click the Change Switchboard Items button of the Main Switchboard to open the Switchboard Manager dialog.

2. Select the Forms Switchboard item in the Switchboard Pages list (see Figure 3.28) and click the Delete button. When the "Are you sure?" message appears, click Yes.

Figure 3.28 Select and delete the Forms Switchboard in the Switchboard manager.

3. With the Main Switchboard (Default) item selected, click the Edit button to open the Edit Switchboard Page dialog.

4. Select the Enter/View Other Information item in the Items on This Switchboard list (see Figure 3.29) and click Edit to open the Edit Switchboard Item dialog (see Figure 3.30). The Switchboard text box is empty because you deleted the associated switchboard in step 2.

Figure 3.29 Select the Enter/View Other Information item to change the navigation process.

Figure 3.30 The Edit Switchboard Item opens with the default values.

5. Replace the existing content of the Text text box with Enter/View Contact Types, and select Open Form in Edit Mode from the Command drop-down list (see Figure 3.31).

Figure 3.31 Change the entries for the Main Switchboard button to open a form directly.

6. Open the Form drop-down list (which replaces the Switchboard list) and select Contact Types (see Figure 3.32). Click OK to accept the changes and return to the Edit Switchboard Page dialog.

Figure 3.32 Select the form to edit in Edit mode.

7. The Edit Switchboard Page dialog shows the caption change you made in step 5. Click Close to return to the Switchboard Manager dialog and then click Close again to return to the Main Switchboard form. The second button reflects the caption change. Click the Edit/View Contact Types button to test your work.

8. Close the Contact Types form to return to the Main Switchboard.

You can import a Switchboard form and Switchboard Items table created by the Database Wizard into Access applications you create from scratch, and then use the Switchboard Manager to modify the original design to suit your navigational needs. Starting with a prebuilt switchboard saves a substantial amount of design work and VBA code writing.

–› To import a Switchboard form or other database objects, see "Linking and Importing External ISAM Tables,".

file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 13 of 17

Exploring Form Design View and VBA Class Modules

Designing forms and writing VBA code are advanced Access topics, but you can preview the topics covered by later chapters by following these steps:

1. With the Main Switchboard active, click the Design view button at the extreme left of the toolbar to open the Main Switchboard in Design view (see Figure 3.33). The Toolbox, which you use to add controls (text boxes, buttons, and the like) to forms, appears to the left of the Form Design window. If the Toolbox isn’t open, click the Toolbox button on the Form Design toolbar.

Figure 3.33 Open the Switchboard form in Design view.

2. Press Ctrl+R to select the entire form and then click the toolbar’s Properties button to open the Properties window. By default, the Properties window displays all the properties of the selected object—in this case, the form.

3. Click the Data tab to display only the data-related properties of the form (see Figure 3.34). The most important property is the Record Source, which specifies the Switchboard Items table as the table bound to the form. The Filter property specifies that the form obtains its data from the first record of the Switchboard Items table.

Figure 3.34 The Data page of the Form properties sheet for the Switchboard form displays data-related property values.

4. Click to select the top button (under the C of Contact Management in the Design view of the form). The Properties window displays the properties of Command Button: Option1.

5. Click the Event tab to display the Event properties of Option1 (see Figure 3.35). The =HandleButtonClick(1) value of the On Click event executes the HandleButtonClick VBA function when you click the button.

Figure 3.35 The Event page displays a list of the events that Access forms can fire.

6. Click the Code button on the toolbar to display the Contacts - Form_Switchboard (Code) window of the VBA editor. Page down until you see Private Function HandleButtonClick(intBtn As Integer), as shown in Figure 3.36. The Option1 command button calls this function to handle the On Click event. The (1) suffix of the event value is passed to the function as the value of the intBtn argument. Scroll through the code until you reach the End Function statement, which terminates the HandleButtonClick function.

Figure 3.36 The VBA code editor displays the HandleButtonClick event-handling function selected in Figure 3.35.

7. Close the VBA editor or press Alt+Tab to return to the Switchboard form, and then click the Form view button to return the Switchboard form to its original state. (You might also need to close the Properties window.)

file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 14 of 17

Downloading Templates from the Microsoft Office Update Site

Microsoft’s Office Tools on the Web site (http://officeupdate.microsoft.com) offers a Template Gallery link to collection of Access 2000 databases (.mdb files) disguised as templates. Most of the templates duplicate the functions of the .mdz template files provided with Access 2002 but have a different main switchboard design. Unlike the Access 2002 templates, the Office Update databases include sample data. Access 2000 templates work fine in Access 2002.

To view the current list of templates and give one of the templates a test run, follow these steps:

1. Click the Templates from Microsoft.com link under the New from Template topic of the task pane to connect to the Office Update site. (Reopen the task pane if necessary by choosing View, Toolbars, Task Pane.)

2. In the Office Update Worldwide window, click the location link closest to you—United States, for this example—to receive a location cookie and open the Template Gallery page. (If you've already specified a location at an earlier date, you won't see this page.) Type Access in the Search for Templates text box (see Figure 3.37), and click Go.

Figure 3.37 The Template Gallery page's selections primarily are for . Type Access in the Search for Template text box and click Go to open a list of Access .mdb files.

3. In the Search Results page (see Figure 3.38), scroll to a template and click its name to open a license page. Scroll to the bottom of the license page and click I Accept to continue. The license page appears only when selecting your first template from the site.

4. In the page for the template you selected, click Edit in Microsoft Access to start the download to the My Documents folder (see Figure 3.39). When downloading completes, Access starts if it isn't running, and displays the application's main switchboard form.

Figure 3.38 The Search Results list displays the available templates for Access 200x.

Figure 3.39 Clicking the Edit in Microsoft Access link downloads the .mdb file to the My Documents folder and opens its Main Switchboard form.

Figure 3.40 shows the Main Switchboard form for the Students and Classes database (TG_7.mdb). Switchboards of the Office Update templates use large command buttons with >> captions whose visibility is determined by the mouse position in the main region of the form. Other than the switchboard graphics and minor changes to the VBA Class Object code to handle command button visibility, the Office Update templates behave the same as databases created from the Access 2002 templates.

Figure 3.40 The Office Update templates' command buttons aren't visible until you pass the mouse pointer over the caption. Saving a Form As a Data Access Page file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 15 of 17

Access 2002’s Data Access Pages (DAP) feature has undergone a major facelift in its second incarnation. One of the most useful additions to DAP is the capability to save a conventional Access form as a Web page. There are serious limitations to the design of forms that you can convert automatically to DAP. Some of the restrictions are due to the limited repertoire of HTML control objects; others relate to problems with forms that depend on a value in another form to supply data. The advantages of DAP as a means of quickly deploying a simple multiuser Jet application on your intranet often outweigh the form design limitations.

–› For a list of the most important limitations in form design for DAP, see "Saving Forms As DAP,".

Simple forms, such as the Contacts form of Contacts.mdb, convert to DAP satisfactorily, but even they usually require a few design tweaks to optimize their design for the Web. To give Access 2002’s new Save As DAP function a test run with the Contacts form, follow these steps:

1. Open Contacts.mdb, and click the Enter/View Contacts button of the Main Switchboard to open the Contacts form.

2. Choose File, Save As to open the Save As dialog. Open the As list, and select Data Access Page (see Figure 3.41). Click OK to open the New Data Access Page dialog.

Figure 3.41 Access 2002’s Save As dialog adds the capability to save a table, query, form, or report as a Data Access Page.

3. Accept the default page name, Contacts.htm in the My Documents folder, and click OK to convert the form. When conversion completes, the Contacts page is behind the Contacts and Main Switchboard forms. Click the Contacts page to bring it to the foreground (see Figure 3.42).

Figure 3.42 The top of the Contacts page appears here in DAP Page view. Data Access Pages don’t support the Page Break control, so there’s extra vertical spacing between the controls on the form’s pages 1 and 2.

4. Scroll to the bottom of the page to expose the Data Source Control (DSC), a member of the Office Web Components (OWC) that DAP use for record navigation (see Figure 3.43).

Figure 3.43 The bottom of the Contacts.htm page also has extra space between the page 2 controls and the DSC.

NOTE

The four command buttons of the original Contacts form appear below the DSC but they don’t function. DAP require VBScript or ECMAScript (JavaScript/JScript) event-handling code, but they don’t support VBA code.

5. To open the Contacts.htm page in Explorer (IE) 5.x, close the page, choose File, Web Page Preview. Figure 3.44 shows the page after modifying its design to conserve browser space file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 16 of 17

and eliminate nonfunctioning buttons. After you deploy the page to your Web server, multiple users with Office XP installed can simultaneously edit existing contacts, add new contact records, or delete obsolete contacts with this page.

NOTE

To prevent conflicts with the preceding steps, the page shown in Figure 3.43 is saved as Contacts2.htm in the \Seua10\Chaptr03 folder of the accompanying CD-ROM.

Figure 3.44 Choosing File, Web Page Preview displays the redesigned Contacts.htm page in IE 5.x. In this example, the page was saved in the ...\Office10\Samples folder.

6. To view the XML, Dynamic HTML, and Cascading Style Sheets (CSS) code needed to create typical DAP, close IE 5, return to the Contacts.htm page, change to Design view, and click the Microsoft Script Editor button on the toolbar. Figure 3.45 shows the Script Editor displaying a fraction of the script required to generate DAP.

Figure 3.45 The Microsoft Script Editor for DAP corresponds to the Office VBA editor for VBA code in Access modules.

7. Scroll to the bottom of the window to view the nonfunctional VBA code that the Save as DAP process adds to the page for reference. Close the editor to return to Access, and return Contacts to Page view.

It’s clear from a cursory inspection of the code shown in Figure 3.45 that writing VBScript to handle page- and control-level events isn’t for the faint of heart. Fortunately, VBScript is a subset of VBA; if you gain a solid foundation in writing VBA functions and procedures, adapting to VBScript doesn’t require a major effort. In the Real World—Putting What You've Learned in Perspective

If you’re new to Access, many terms used in this chapter might sound like ancient Aramaic. The objective was to give you an overview of some of the most important objects that make up an Access application, the relationships between these objects, and how you assemble the objects you create into self-contained, easily navigable applications. Using the Database Wizard helps you quickly understand the components and comprehend the behavior of a completed Access application. If you’re interested in designing an Access inventory management application, for example, use the Inventory Control template to create an elementary sample application. You’re likely to find that one of the Database Wizard or downloaded templates bears some resemblance to your intended application. Most of the Access templates offered on the Office Templates site have sample data, which makes it easier to determine whether you can use the design as a starting point for the application you intend to create.

If you didn’t perform the step-by-step tutorial to create the sample application and Web page of this chapter, not to worry. There’s a copy of the Contacts.mdb database and Contacts2.htm in the \Seua10 file://J:\Alpha\chapters\JW225.html 11/5/2001 Chapter 3: Building a Simple Desktop and Web Application Page 17 of 17

\Chaptr03 folder of the accompanying CD-ROM. Sample databases for most of the chapters are included in corresponding \Seua10\Chaptr## folders.

The remainder of this book covers each category of Access objects in detail, beginning with Table and Query objects and then progressing to Form and Report objects. By the time you get about halfway through this book, you gain the experience necessary to design your own versions of these objects. The last half of this book deals with advanced topics, such as exporting Access forms and reports to XML- based Web pages, creating Data Access Pages with spreadsheet, PivotTable, and PivotChart controls, and writing professional-quality VBA code.

© Copyright Pearson Education. All rights reserved.

file://J:\Alpha\chapters\JW225.html 11/5/2001