Using BreakPoints

Logic errors can be tricky to find. A good way to see what your code is doing is to walkthrough your code line by line “playing computer”. Often close scrutiny of the code can reveal the error but sometimes we just don’t see it. Frequently erroneous assumptions on our part send us down the wrong path of the program logic. There is a wonderful debugging tool built into the Visual Basic IDE that you can use to show you exactly what is happening when the program is executed. You can step through your code by setting a BreakPoint and then watch as each statement is executed.
 
To set and use breakpoints

1) Click the gray margin beside a statement prior to where you believe the problem occurs. For our programs it is usually best to set the breakpoint at the top of the procedure, on the first line after the Dim statements. A reddish dot will appear in the margin. This breakpoint indicates that execution of the code will pause when it reaches this statement.

2) Run the program, enter input values that produce the incorrect output, and click the appropriate command button

3) Execution will be suspended at the breakpoint. The program will be in Debugging mode and the statement will be highlighted in yellow. A statement highlighted in yellow has not yet been executed.

4) At this point you should use your mouse (point at each variable or property in the statement) to ascertain the current value of each operand. A data tip will pop up to display the current value.

a. Are the values what you expected them to be?

5) Based on the values of the operands evaluate the expression and decide what you think will happen

If is in an assignment statement determine the value that will be stored

If it is an If statement determine the next statement that will be executed

NOTE: it is VERY IMPORTANT that you THINK about what will happen before you move on

6) Press the F8 or F11 key (for a reason I have yet to determine sometimes the F8 is was is expected and sometimes F11. Look for the hot key next to the Step Into option on the Debug menu.). This will move execute the highlighted statement and move the highlight to the next statement to be executed.

If the previously highlighted statement was an assignment statement point at the variable that received a new value

Is the value what you expected it to be?

If the previously highlighted statement was an If statement did the highlighted move to the statement you expected it to?


If the result was not what you expected this MAY be the source of your error

7) Repeat steps 4 – 6 until the end of the procedure or you have found the error.
 
At anytime you can continue without stepping through the code by clicking the continue button (right pointing triangle) on the tool bar.

You can remove the breakpoint by clicking the red dot.