Using Pop-up Hints
You now know how to declare variables and write statements. Before you proceed to more complex subjects, let's take a look at how Access helps you manage your variables.
The Access Integrated Development Environment (or IDE) has a built-in mechanism called the pop-up hint. A pop-up hint is a little message box that automatically pops up when your mouse pointer hovers over one of your variables. This works only in debug mode. Let me take a moment to explain.
Code is written in a module file. Module is Access's term for a file that contains programming language text. Almost all code, except for statements such as Option Explicit, is written in subroutines or functions. (I will explain functions and subroutines in Hour 8, "Solving Problems a Piece at a Time." For now, I will provide an example that you can copy directly.) After you have at least one function or subroutine, you can test your code line by line.
Setting Up Test Code
Debugging mode refers to running your code while you are looking at the lines of code. This is accomplished in the Microsoft Visual Basic editor. Because Visual Basic for Applications (VBA) is used in desktop tools besides Access, such as Excel, Microsoft reuses the same Visual Basic editor for code (see Figure 3.2). This is a lot of information, but a couple of examples will help you.
Follow these steps for a demonstration:
Open Access 2002.
In the New side pane, select Blank Database or click File, New from the menu.
-
In the File New Database dialog use the default database name and click the Create button. (Or, you can give the database a name that is meaningful to you as I did with the ISBN, chapter scheme shown in Figure 3.2.)
-
In the Database dialog, select Modules in the Objects group on the left and click New. This will invoke the Visual Basic editor and create a new module (refer to Figure 3.2).
You are now ready to proceed. If you completed steps 1 through 4, you are looking at the Microsoft Visual Basic editor with a module named Module1 on your desktop. This is where you write your code. For now, just add the code in Listing 3.3. (This is basically the code from Listing 3.1 with a subroutine wrapped around it.)
Figure 3.2 Access shares the microsoft visual basic editor for editing VBA code (NOTE: I named my database 661703.MDB to coincide with the ISBN and chapter number. By default your database will be DB1.MDB or something similar. Name the database something that makes sense to you.)
Listing 3.3 A Subroutine with the Code from Listing 3.1
1: Sub CalculateCircumference() 2: Dim Circumference As Double, Radius As Double 3: Const PI = 3.14159 4: Radius = 10 5: Circumference = PI * Radius ^ 2 6: End Sub 7: 8: Sub CallCalculate() 9: Call CalculateCircumference 10: End Sub
For now, if you want to try the example, just focus on the code on lines 2 through 5 but copy, excluding the line numbers, all six lines verbatim. I will be referring to this code throughout the chapter to demonstrate how Access provides you with helpful tools for writing code. Lines 8 through 10 are used to test the CalculateCircumference subroutine. Lines 8 through 10 are used to demonstrate how the Call Stack works in the section Employing the Call Stack at the end of this chapter.
Debugging and Using Pop-up Hints
You can debug code by clicking the code you want to test and pressing the F8 key. (Carefully follow the steps in the previous section "Setting Up Test Code.") The first line of code to be executed is line 1. This is indicated by line 1 being highlighted in yellow in the editor.
Every time you press F8, the debugger advances the program to the next line of code. Press F8 two more times until the line containing the code in line 5 is highlighted. This represents the location of the debugger in your program and the next line of code that will be run.
You now have some values for PI and Radius because those lines of code have been executed. You can inspect the values of PI and Radius using pop-up hints. To see the value of the Radius variable move the mouse over line 4 containing the assignment to Radius as shown in Figure 3.3.
Figure 3.3 A pop-up hint showing the value assigned to the Radius variable.
Pop-up hints are useful for quickly scanning the values of your variables while testing your code.