Using the Immediate Window
The Immediate Window is your most valuable ally while debugging code. The Immediate Window is the equivalent of a debug window that allows you to watch the values of the variables in your code. (Bring up the Immediate Window by clicking on the View/Immediate Window Menu Option for Access v 2.0; View/Debug Window Menu Option for Access v 8.0)
The Immediate Window can be used to return values, set values and test the validity of conditions.
1. Putting a question mark before a variable or condition (e.g. ?MyVar) returns the value of MyVar. But remember that MyVar must be a variable local to the procedure you are stepping through or it must be a global variable. Asking for the value of a variable that is not declared in the current procedure will give you an error.
2. To set the value of MyVar while stepping through your code, you can bring up the Immediate Window and set the value just like you would in the Access Basic code.
MyVar = "The new value of this variable"
3. You may terminate a program by typing
End
in the Immediate Window.
4. If you are a teeny bit lazy about typing in the variable names in the Immediate Window each time you want to check their values, you could use this alternative instead:
Debug.Print MyVar
If you put this line at strategic points in your code you can see your code run through uninterrupted and see the values in the Immediate Window!! Since there is always a downside to everything, there is one here too. This slows down the execution speed of your code tremendously. So if you do choose to use this then you either comment out these lines or delete them entirely from your modules.
5. (Access v 2.0 only) One trick is to ensure that there are at least two line breaks between each variable whose value you wish to check. You may have noticed that if you write in two consecutive lines like:
?MyVar
?MyOtherVar
and if you put the cursor on the first line and hit <Enter> to check the value of MyVar again, then the second line is overwritten by the value of MyVar, even if it is empty. So, if you skip a line, you can keep using these two lines over and over again without having to retype them in every time you want to check the values.
(This does not happen in Access v 8.0)
We use the Immediate Window extensively while testing our code. To check values, to run functions. YES! You can execute functions or subroutines by ....
?MyFunction(Arg1, Arg2)
or
?MySub Arg1, Arg2