Microsoft Access VBA Macro Programming

(Tina Sui) #1
You can restart your code by clicking the Run symbol on the toolbar (the triangle symbol
pointing to the right) or pressingF5; it will start from the point where it was stopped. A yellow
arrow will appear to the left of the code, indicating the current execution point. Try dragging the
yellow arrow to a new start point, such asx=2, and then rerun the code. This is useful if you
want to restart a loop or an If condition after you have changed code due to a bug.

Breakpoints.


You can add a breakpoint to your code so it will halt at that line. You can then use step commands
by pressingF8to move through one line at a time and use instant watch to examine variables as
the code steps through.
Breakpoints can be toggled on and off by usingF9or by selecting Debug | Toggle Breakpoint
from the menu. They appear as a solid brown circle in the leftmost column of the code window.
If you run the preceding loop example and place a breakpoint on theLoopstatement, you
will see that every time you click the Run symbol orF5, it stops on theLoopstatement.
You can step through the program one line at a time by clickingF8or by using Debug |
Step Into (F8) from the code menu. Other options allow you to Step Over (SHIFT+F8), Step
Out (run program normally), or Run to Cursor (run code down to where the cursor is). While
you are stepping through a program, you can still use your cursor to examine values.
You can also open a Debug window to watch expressions and see what they are doing.

Using Stop Statements


Entering aStopstatement in your code is the same as entering a breakpoint, except it is in
your code. When VBA encounters aStopstatement, it halts execution and switches to break
mode. AlthoughStopstatements act like breakpoints, they are not set or cleared in the same
way.
If you set breakpoints usingF9, when you leave your project and then reload it, the breakpoints
are all cleared. However,Stopstatements form part of the code and are only cleared when you
delete them or put a single quote (') character in front to change them into a comment. Make sure
that once you have your code working properly, you remove allStopstatements.

Running Selected Parts of Your Code


If you know where the statement is that is causing an error, a single breakpoint will locate the
problem. However, it’s more likely that you will only have a rough idea of the area of code
causing the problem.

84 Microsoft Access 2010 VBA Macro Programming

Free download pdf