Working with the Watch Window
Sometimes setting a breakpoint isn't enough to catch an error. You might have a variable that you know is being changed somewhere by your code (perhaps incorrectly). By using the Watch window, you can examine a variable as your code runs, ask Visual Basic to halt when an expression that uses the variable becomes true, or ask Visual Basic to halt when the variable changes.
An interesting set of variables in the Conrad Systems Contacts sample database are gint- DontShowCompanyList, gintDontShowContactList, and gintDontShowInvoiceList (all defined in the modGlobals module). When any of these variables are set to True, the main switchboard bypasses the intermediate list/search form for companies, contacts, and invoices, respectively. You played with one of these variables earlier, but it would be interesting to trap when these are set or reset.
Remember:
There are a couple of known issues with setting breakpoints in Access 2010. First, code will not halt if you have cleared the Use Access Special Keys check box in the Application Options section of the Current Database category of the Access Options dialog box (click the File tab on the Backstage view and then click Options). Second, the Break When Value Is True and Break When Value Changes options in the Add Watch dialog box will not work if the value or expression you're watching is changed in a form or report module that is not already open in the VBE. For this example to work, the form modules for frmMain, frmSignon, and frmUsers must be open. You can verify that these modules are open by opening the Windows menu in the VBE window. The Contacts. accdb sample file should have modules open, but these modules might not be open in your copy if you have closed them and compiled and saved the project. You can find these modules in the Project Explorer window. Open the list of objects in the Microsoft Class Objects category and then double-click the form modules that you need to open them.
In the Expression box, enter the name of the variable you want the code to watch. In this case, you want to watch when the gintDontShowContactList variable changes. You don't know where the variable is set, so set the Procedure and Module selections to (All Procedures) and (All Modules), respectively. Under Watch Type, select the Break When Value Changes option, and click OK to set the watch. Go to the Immediate window and set gint- DontShowContactList to True by entering gintDontShowContactList = True and pressing Enter. Now return to the Navigation pane and start the application by opening the frmSplash form. (Code in the Load event of this form hides the Navigation pane and then opens the Conrad Systems Contacts Sign On form.) Because you set a watch to halt when gintDontShowContactList changes, the code execution should halt in the module for the frmSignOn form.
Note that the code halts at the statement immediately after the one that reset the watched variable. If you didn't set the variable to True before you started the application, Visual Basic won't halt because the value won't be changing.
Click Continue (or press F5) to let the code continue executing. Return to the Access window, and in the Conrad Systems Contacts Sign On dialog box, select my name (Jeff Conrad), type password in the Password text box, and press Enter or click Sign On. The dialog box will close, and the main switchboard form opens. In the main switchboard, click Users to open the user edit form. The first record should be my record unless you've created other users. Select the Don't Show Contact List check box in my record and click Save. The procedure halts again.
It appears that this code is setting the gintDontShowContactList variable to some value on the user edit form. (As you'll learn later, Me is a shorthand way to reference the form object where your code is running, so Me.DontShowContactList references a control on the form.) Click Continue again to let the code finish execution. Return to the Access window and click the Close button on the Users form to return to the main switchboard. (The Users form might be showing behind the main switchboard form.)
If you open frmUsers in Design view (you can't do this while the procedure is still halted) and examine the names of the check box controls on the form, you'll find that the check box you selected is named DontShowContactList. When the code behind frmUsers detects a change to the options for the currently signed-on user, it makes sure the option variables in modGlobals get changed as well. Be sure to close the frmUsers form when you're finished looking at it.
The Procedure Call Sequence (Call Stack)
After stopping code that you're trying to debug, it's useful sometimes to find out what started the current sequence of code execution and what procedures have been called by Visual Basic. For this example, you can continue with the watch on the gintDontShowContactList variable.
You should now be at the main switchboard form (frmMain) in the application. Click Exit to close the application and return to the Navigation pane. (You'll see a prompt asking you if you're sure you want to exit-click Yes. You might also see a prompt offering to back up the data file-click No.) The code should halt again at the Close event of the frmMain form. Click the Call Stack button on the toolbar or click Call Stack on the View menu to see the call sequence.
The Call Stack dialog box shows the procedures that have executed, with the most recent procedure at the top of the list, and the first procedure at the bottom. You can see that the code started executing in the cmdExit_Click procedure of the frmMain form. This happens to be the Visual Basic event procedure that runs when you click Exit. If you click that line and then click Show, you should see the cmdExit_Click procedure in the module for the frmMain form (the switchboard) with the cursor on the line that executes the DoCmd. Close command to close the form. This line calls the Access built-in Close command (the <Non-Basic Code> you see in the call stack list), which in turn triggered the Close event procedure for the form. It's the Close event procedure code that sets the gintDontShow- ContactList variable back to False (0). Be sure that the Call Stack dialog box is closed and click Continue on the toolbar to let the code finish running.
Note:
Be sure to delete the watch after you are finished seeing how it works by right-clicking it in the Watch window and clicking Delete on the shortcut menu.
In this tutorial:
- Visual Basic Fundamentals
- Visual Basic Development Environment
- Visual Basic Editor Window
- Relationship Between Access and Visual Basic
- Visual Basic Debugging Tools
- Working with the Watch Window
- Variables and Constants
- Variable and Constant Scope
- Declaring Constants and Variables
- Dim Statement
- Enum Statement
- Event Statement
- Private Statement
- Public Statement
- Static Statement
- Type Statement
- Collections, Objects, Properties, and Methods
- DAO Architecture
- ADO Architecture
- Referencing Collections, Objects, and Properties
- Use Exclamation Points and Periods
- Assigning an Object Variable-Set Statement
- Object Methods
- Manipulating Complex Data Types Using DAO
- Working with ADO Recordsets
- Functions and Subroutines
- Sub Statement
- Understanding Class Modules
- Property Let
- Property Set
- Controlling the Flow of Statements
- Do...Loop Statement
- For...Next Statement
- For Each...Next Statement
- If...Then...Else Statement
- RaiseEvent Statement
- Stop Statement
- With...End Statement
- Running Macro Actions and Menu Commands
- Executing an Access Command
- Trapping Errors
- Working with 64-Bit Access Visual Basic for Applications
- Using LongPtr Data Types
- Supporting Older Versions of Access
- Using LongLong Data Types