Programming Events on Data Access Pages


Programming events involves two steps. First you place objects on a page. Then you design code that runs when events fire for those objects or for the page itself. Access developers have experience with both of these requirements for Access forms. However, implementing these requirements is a little different for data access pages than for Access forms. The sample in this section describes the implementation of a typical event procedure for a data access page. You'll also see an event procedure sample in this chapter's "Creating Interactive Reports with Computed Fields" section.

Adding a Drop-Down List Control

Although links are a common tool in Web-based solutions, unbound drop-down list controls offer several attractive advantages as well. First, you can base a drop-down list on a record source. Therefore, you can alter the items in the list without having to redesign the form; instead, you just update the SQL statement for the record source. Second, developers are used to creating combo box controls in Access solutions. With data access pages, the drop-down list is a comparable type of control. The wizard for creating drop-down list controls further invites Access developers to try this approach. Third, the ability to create event procedures for these controls is another motivator for using them. Access developers have substantial familiarity with these powerful and often compact pieces of code.

There are two main tasks for using unbound drop-down list controls on a data access page. First, you must physically add the control to the page and specify what it shows and what it saves. Happily, the Toolbox and its Control Wizards simplify this. By the way, the Toolbox calls a drop-down list control a Dropdown List. Second you must create an event procedure that reads the value a user selects with the control and performs some action based on it. For an example, open another data access page that contains a form whose values are filtered by a selection from the drop-down list control.

The easiest way to add an unbound drop-down list control to a data access page is to invoke the Control Wizards. Click and engage the Control Wizards button within the Toolbox before clicking the Dropdown List button. Then click on the grid within the data access page where you want the control to appear. The similarities between the drop-down list and the more familiar Access combo box will be apparent immediately. This is because the wizard's first screen has the caption Combo Box Wizard in its title bar. Make your selections for the instance of the drop-down list on the page as you normally would for a combo box on an Access form.

After responding to the Combo Box Wizard dialog boxes, you'll have a drop-down list control that displays some items from a record source. Users will be able to select items from the control, but nothing will happen as a result. Use an event procedure to cause an action based on a user choice.

For this example, I added a drop-down list control to a page. This type of control specifies its source via its ListRowSource property. The property for the control was the Categories table. The control displays category names , but it saves the corresponding CategoryID value for the name a user selects. I assigned an Id setting of cboCategoryID to the control. Although I created all these settings (except for the Id property value) with the Combo Box Wizard, you can set them easily on your own. Merely add the drop-down list control with the Control Wizards turned off. Then open the Properties dialog box for the control, and select the Data tab. Type CategoryID for ListBoundField and CategoryName for ListDisplayField . Select Table: Categories for ListRowSource . Access automatically converts the last selection to Recordset: Categories. Specify cboCategoryID , or whatever name you choose for your control, to the Id property setting box on the Other tab. Any event procedures you create for the control will reference the control by its Id setting.

Figure 14-7 shows the Design view for the drop-down list control on the data access page. The display shows the cboCategoryID control selected. To the right of the cboCategoryID control is the Properties dialog box for the control. You also can see all four settings discussed in the preceding paragraph.

click to expand
Figure 14.7: An unbound combo box added to a data access page.

Before ending this section, I'd like to note that there's more to data access pages than their drop-down list controls and labels with a caption for the control. For example, the control sits on a grid. The grid is a Section object from the Office Web Component model that can represent a hierarchical element within a data access page. The Section object in Figure 14-7 contains the drop-down list control and its label, but the section resides within the body of the page. The caption on top of the grid that starts with the words Drag fields resides in a banner for the section sitting below it. The large, gray text above the banner for the section is the page's heading text. The caption in the page's title bar indicates that Page1 is the top-level object in the data access page.

Using the Microsoft Script Editor

There's good news and bad news about using event procedures for controls on data access pages. The good news is that you can create event procedures using a language much like the one you already know in an environment that bears some similarity to the integrated development environment (IDE) that you're already accustomed to. The bad news is that the language is not exactly Visual Basic for Applications (VBA) and the IDE is not the Visual Basic Editor (VBE) window. Of the two popular languages for scripting data access pages, the one you're likely to find easiest is VBScript. The IDE for the code behind a data access page is the Microsoft Script Editor. If you programmed data access pages much in Access 2000 or you used Microsoft Visual InterDev 6 or later, you probably have some familiarity with the layout and operation of the IDE for data access pages.

To open the Script Editor, click the Microsoft Script Editor tool on the toolbar for the Design view of a data access page. The Script Editor displays the data access page in HTML and scripts. Not only can a page have multiple scripts to make it dynamic and interactive, each script can be programmed in a different language. The two scripting languages that are readily available for this are VBScript and JScript.

Note  

There's plenty of help for learning about the Script Editor. From the Script Editor, you can open the Help system by choosing Help, Microsoft Script Editor Help. This takes you to a Help system with information about the Script Editor, HTML, VBScript, and JavaScript. This Help file primarily features content on the Microsoft Script Editor IDE. Meanwhile, I have found the Help files in \Program Files\Common Files\Microsoft Shared\Web Components\1033 to be useful for increasing my understanding of programming techniques for data access pages and Office Web Components generally . The last folder (1033) in the path for the Help files is a language-specific resource folder. If your primary language is something other than English, another folder will replace 1033.

After you initially open the Script Editor for a data access page and return to the Design view of the page, a control on the Windows status bar (with the symbol for the Script Editor) permits you to toggle between Design view in Access and the Script Editor. In addition, you can navigate between windows using other standard techniques, such as with the Alt+Tab key combination. Making a change in one representation knocks that representation out of sync with the other one. You need to switch between these two views because doing so resynchronizes the two representations of the data access page.

Figure 14-8 presents a collection of three windows within the Script Editor for the data access page shown in Figure 14-7. These are the Document Outline, Properties, and Document windows. The Document Outline window offers two representations of a data access page. The view appearing in Figure 14-8 is for the HTML Outline. It depicts the hierarchical relationship among the objects that comprise a page. At the top level is Page1, the name for the data access page. Within Page1 are three objects: the Data Source Control (MSODSC), a Script object representing a script for the page, and a Body object that contains other top-level HTML tags defining the data access page. MSODSC contains the specification of the data source for the drop-down list control. The Script object contains two built-in scripts that check for the availability of an appropriate browser and a version of Office Web Components.

click to expand
Figure 14.8: A Script Editor view that contrasts with the Design view of the data access page shown in Figure 14-7 .

The items within the Body object show the HTML tags that lay out content on the data access page. The Document Outline window denotes these tags by their Id setting, when one exists. Otherwise, the window just references the tags. In order of appearance, the items within the Body object are a HeadingText object, a paragraph tag (<p>), a banner for an unbound section, the unbound section, and another paragraph tag (<p>). Additional hierarchical elements, such as the drop-down list control ( cboCategoryID ) and its label, reside within the unbound section banner and objects.

The highlighted object in the Document Outline window also controls the focus in the Properties and Document windows. The Properties window appears directly below the Document Outline window. Since the Document Outline window depicts the DropdownList0_Label element selected, the Properties window indicates properties for that element. This selection is convenient for changing the Id value from its default setting to one that more precisely defines its purpose, such as cboCategoryID_Label . The Document window appears to the right of both the Document Outline and Properties windows. You can use this window to improve the appearance of the label so that it reads Category Name instead of CategoryName. Just insert a space between the words Category and Name , which appear between the <span> and </span> tags. These tags can denote a label, and the text between the tags comprises the InnerText property value for the label control.

The suggested updates in the Document window will cause the Id and InnerText property values for the label to revise in Design view when you synchronize the two data access page representations. Click the Page1 Script Editor control on the Windows status bar to toggle control back to Design view for the data access page. After a while, Access updates the view with the changes made in the Script Editor.

Adding an Event Procedure to a Control

Recall that cboCategoryID , the drop-down list control on the data access page we're building, lets a user choose a category name and stores the corresponding CategoryID value for the selected category name. However, nothing happens after that. With the help of an event procedure attached to the control, you can open the dapProducts data access page filtered by the CategoryID value in cboCategoryID . This section shows how to build an event procedure that accomplishes this.

You start to build event procedures for the objects on a data access page from the Document Outline window. This window offers two views. You saw the first of these, the HTML Outline view, in Figure 14-8. An alternative view is the Script Outline view, which can display all the client objects with their events and any existing client scripts in a data access page. Two controls just below the title bar for the Document Outline window allow you to switch between the two views.

Figure 14-9 shows the Document Outline window in its Script Outline view after you click the onchange event for the cboCategoryID object. You can expose the events for other objects by clicking the plus sign (+) next to them. Each client object has a set of events appropriate to it. The onchange event for drop-down list controls on data access pages works like an afterupdate event for combo boxes on Access forms. Therefore, the onchange event fires right after the user changes the value saved in the cboCategoryID control by making a selection from it.

click to expand
Figure 14.9: A Document Outline window with the onchange event for the cboCategoryID control selected.

Double-click onchange in the Document Outline window to make the Script Editor insert the shell for an onchange event procedure for the cboCategoryID object in the Document window. The shell appears as shown below. Notice that the for and event attributes denote the purpose of the Script object. The language attribute is the default one, vbscript. The <!-- and --> markers denote comments.



Programming Microsoft Office Access 2003
Programming MicrosoftВ® Office Access 2003 (Core Reference) (Pro-Developer)
ISBN: 0735619425
EAN: 2147483647
Year: 2006
Pages: 144
Authors: Rick Dobson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net