The VBE Interface

Perhaps the most noticeable change to the Access development environment is the new VBE interface. This interface brings Access 2000 more in line with Microsoft Word, Excel, and PowerPoint from a development interface perspective. This section will explore the use and layout of the windows and show how to use them for debugging. It will also review the use of the Object Browser.

VBE Windows

Access 2000 offers at least three routes to VBE for modules that are not behind a form or a report. First, those familiar with development in other Office components can choose Tools-Macro-Visual Basic Editor from the Database window. A convenient shortcut for this is Alt+F11. This takes you immediately to VBE. You can use Alt+F11 to toggle back and forth between the Database window and VBE. Second, if you already have any standard modules, click the Modules button in the Database window and then double-click any standard module that you want to view. This opens VBE with that module displayed. Third, if you want to create a new standard module, click the Modules button in the Database window and then click New on the toolbar. This opens a blank module in VBE.

NOTE
The Office development team is working on a fourth route to VBE that will be familiar to some Access developers. You will be able to click a Code button in the Database window with a standard module, form, or report selected. The Code button will open VBE with the corresponding module.

To get to the module behind a form or a report, you must first open the object in Design view. You can click the Code button on the Design toolbar. This opens the module and positions your display at the top of the module. You can move directly to the event procedure for a specific object on a form or a report by clicking the Build button next to the event in the property sheet. If there is no event procedure for an object, clicking the Build button and choosing Code Builder will move you to a blank event procedure for the object.

Once you navigate to VBE, you'll probably want to open the Project and Properties windows. These are convenient for opening and inspecting the other modules in an application. The Project window displays modules that are not behind a form or a report in the Modules folder. Modules behind forms and reports appear in the Microsoft Access Class Objects folder. You can select a folder associated with a form or a report to view and set the objects associated with the class. To open the Project or Properties window, choose the appropriate command from the View menu or use the keyboard and toolbar shortcuts.

NOTE
In order for the Properties window to show the objects for an Access class object, that object must be open in Design view. Double-clicking a module or a class in the Project window displays the corresponding instructions and declarations in the corresponding Code window, but the objects appear only if the class object is also open in Design view.

Figure 1-7 shows the code for this chapter's sample database loaded into VBE. The Project window shows the Form_frmSample1 class selected. Below it, the Properties window shows the label control for the form's title selected. This window also shows that the label control has an event procedure associated with its OnClick event. The Code window to the right of the Project and Properties windows shows the code for this event procedure. You can move, resize, and dock any of these windows.

click to view at full size.

Figure 1-7. VBE's Project, Properties, and Code windows showing the code for this chapter's sample database.

Double-clicking any other Access class or Module object in the Project window opens the corresponding Code window. There you can inspect, edit, or copy the code. The Code window has a familiar layout, with Object and Procedure drop-down list boxes at the top. You can use these to navigate around a large module or open new procedures in an existing one. The Properties window shows the properties for the currently selected Access class or module. While you can edit object properties in this window, it is usually more convenient and flexible to edit object properties for a form or a report in Design view.

Debugging

You can use the Code window to debug and examine your code. You can add or remove a breakpoint on a statement by clicking the margin to the left of the statement. VBE marks the breakpoint with the traditional round dot in the left margin. Figure 1-8 shows the debugger stopped on the second of four lines of code in a procedure; the arrow in the left margin indicates the next line of code that will execute.

click to view at full size.

Figure 1-8. The Code window for the load event of the Form_frmSample1 class at a breakpoint, with an arrow pointing to the next line that will execute.

You choose Continue from the Run menu to restart code execution after a breakpoint. Figure 1-9 shows the result of executing the rest of the code in the Form_frmSample1 procedure. Notice that the form's caption reads, "New Caption." The text box reads, "Hi, there!"

You can use the arrow shown in Figure 1-8 to skip over one or more lines of code in the execution path. For example, Figure 1-10 shows the result of dragging the arrow to the line that invokes the SetFocus method and choosing the Continue command from the Run menu. This procedure skips over the assignment of "New Caption" to the form's Caption property, so the form looks almost the same as in Figure 1-9 but its caption reads, "My Default Caption."

Figure 1-11 shows the result of dragging the arrow in Figure 1-8 to the final line of code that makes an assignment to the text box. This, of course, generates an error message because of the attempt to assign a property value without the object having focus.

click to view at full size.

Figure 1-9. The outcome of running all three remaining lines in Figure 1-8.

click to view at full size.

Figure 1-10. The outcome of skipping over the second remaining line in Figure 1-8 and continuing execution.

Figure 1-11. The outcome of skipping to the last line in Figure 1-8 and continuing execution.

Much of the functionality of the Microsoft Access 97 Debug window is available in Access 2000. The Debug window in Access 97 had a Watch page and a Locals page; each had a split screen in which the Immediate window was available as part of the page. Access 2000 has separate Watch, Locals, and Immediate windows. You open these windows in VBE by using the View menu. You can drag, drop, and resize any of these windows alongside your Code, Project, and Properties windows. Choose Tools-Options and use the Docking page to designate which windows are dockable.

You can use the Watch window to track the values of expressions, variables, and objects as your code executes. After opening a Watch window, you can add variables to watch by choosing Debug-Add Watch. Select a variable to watch before invoking the command. When the Add Watch dialog box opens, select a Watch type and then click OK to close the dialog box. The value of the variable is shown in the Watch window as your code executes. If you step through your code in break mode, you can verify the value of critical variables after each step.

Figure 1-12 shows the value of frmSample1's Caption property immediately after a line of code changes it from "My Default Value" to "New Caption." The Watch window shows the value of txtMyTextBox as Null since the code has not yet executed its assignment statement.

click to view at full size.

Figure 1-12. A Watch window showing the status of expressions as an event procedure executes.

The Locals window can display all the variables in scope while your code executes in break mode. When execution stops on a breakpoint, the Locals window contains a Me object. You can successively expand Me and its selected components to search out the values of all properties and variables. This can be helpful if you require exhaustive detail to help you determine how a program performs.

The Immediate window might be the most convenient tool for code development and debugging. From this window, you can run any function or subroutine that is in scope. You can also evaluate expressions and different ways of writing functions. Before the availability of Watch expressions, the Immediate window was a convenient place to print intermediate results in break mode, and it can still serve related functions in special circumstances.

Figure 1-13 shows three uses of the Immediate window. Our initial discussion of procedures described these functions, but it did not show their output to the Immediate window. Typing MyFirstCalculator invokes the procedure of the same name. When you press Enter in the Immediate window, it runs the procedure and prints the result, 3, in the window. The same is true for the CallSecondCalculator procedure. In this case, the result is 4. The last sample demonstrates how to specify arguments for the MySecondCalculator function—you just type the name, a space, and the arguments separated by a comma. This sample is critical because it shows how to use the arguments passed to a procedure or function to calculate a return value.

click to view at full size.

Figure 1-13. The Immediate window showing how to run procedures and built-in functions.

You can also use the Immediate window to run built-in and custom functions. With a function, you must specify a print command to return the result of the function. You can use the keyword Print or simply ? followed by the function name and a carriage return. The last sample in the Immediate window shows how to call the built-in Date function.

The Object Browser

The Object Browser, shown in Figure 1-14, is a powerful tool for learning about object models. It is especially important to Access 2000 developers because of the many significant new object model innovations. For example, Access 2000 introduces a new data access development language—ActiveX Data Objects (ADO)—that will eventually make Data Access Objects (DAO) obsolete. At least three object models underlie the Access 2000 implementation of ADO.

click to view at full size.

Figure 1-14. The Object Browser view of the ADODB Recordset class and a select set of its members.

Before viewing the ADO object classes in the Object Browser, you must set a reference to the appropriate library. Choose Tools-References to verify or create links to ADO type libraries. Open the Object Browser by clicking its button on VBE's Standard toolbar. You can also open it using the View menu or by pressing the F2 shortcut key. One of the ADO libraries has the name ADODB. Select this from the drop-down list at the top of the Object Browser. This changes the contents of the Classes list and the Members list in the browser. Figure 1-14 shows the Recordset class selected in the Classes list and a mix of properties, methods, and events appearing in the Members list. Select a member entry and click the ? button for detailed Help on that topic. The Object Browser can also search for specific classes and members with its search engine. Use the second drop-down list box to specify a search criterion. This sometimes returns result sets in several different libraries. The Object Browser lets you examine these separately.



Programming Microsoft Access 2000
Programming Microsoft Access 2000 (Microsoft Programming Series)
ISBN: 0735605009
EAN: 2147483647
Year: 1998
Pages: 97
Authors: Rick Dobson

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