Online Book Reader

Home Category

Access Cookbook - Ken Getz [141]

By Root 1937 0
calls that got you there. There are many other uses for this functionality (see the next solution, for example), but one simple use is to retrieve the name of the current procedure in a global error-handling procedure.

The kind of data structure you'll need for maintaining your list is called a stack. As you enter a new procedure, you "push" its name onto the top of the stack. When you leave the procedure, you "pop" the name off the stack. Figure 7-2 shows a graphical representation of a procedure stack in action. The arrows indicate the direction in which the stack grows and shrinks as you add and remove items.

Figure 7-2. The call stack and the sample routines to fill it

To see the procedure stack in action, load 07-02.MDB. Open the module basTestStack in design mode. Open the Immediate window (choose View ‡ Immediate Window). In the Immediate window, type:

? A( )

to execute the function named A. Figure 7-2 shows A and the procedures it calls. At each step, the current procedure pushes its name onto the procedure stack and then calls some other procedure. Once the calling procedure regains control, it pops its name off of the stack. In addition, each procedure prints the name of the current procedure (using the acbCurrentProc function, discussed later in this solution) to the Immediate window. Once all execution has finished, you should see in the Immediate window output like that shown in Figure 7-3.

Figure 7-3. The output from running the sample procedure

Follow these steps to incorporate this functionality into your own applications:

Import the module basStack into your application. This includes the procedures that initialize and maintain the procedure stack.

Insert a call to the acbInitStack subroutine into code that's executed when your application starts up. Consider adding this procedure call to the code in your main form's Load event procedure. You'll want to call acbInitStack any time you restart your program during development, so you probably don't want to call it from the Autoexec macro, which is executed only when you first load the database. To call acbInitStack, either place its name alone on a line of code, like this:

acbInitStack

or use the Call construct, as follows:

Call acbInitStack

For each procedure in your application, place a call to acbPushStack as the first statement. This procedure will place the value it's passed on the top of the stack. As the single argument for each call, pass the name of the current procedure. Our example places a pair of parentheses after function names and nothing after subroutine names, as a matter of style. As the last line in each procedure add a call to acbPopStack, which will remove the current name from the top of the stack.

You can retrieve the name of the currently executing procedure at any time by calling the acbCurrentProc function. This function looks at the top of the stack and returns the string it finds there. You can use this as part of an error handler or, as in the next solution, to help track procedure performance.

Discussion


The module you imported from 07-02.MDB, basStack, includes code for maintaining the procedure stack and a module-local variable that is the stack itself. There are just six entry points (nonprivate procedures) in the module. Table 7-1 lists those procedures. Since all the code for the stack is encapsulated in that one module, you never really have to know how it all works. However, it's quite simple.

Table 7-1. The six entry points into basStack

Procedure name

Purpose

Parameters

acbInitStack

Initialize the stack.

acbPushStack

Add an item to the stack.

A string to push

acbPopStack

Remove an item from the stack.

acbCurrentProc

Retrieve the name of the current procedure.

acbGetStack

Retrieve a specific item from the stack.

The item number to retrieve

acbGetStackItems

Retrieve the number of items on the stack.

basStack includes two module-level variables: mastrStack, the array of strings that is the stack itself; and mintStackTop, an integer that holds

Return Main Page Previous Page Next Page

®Online Book Reader