Using Breakpoints to Troubleshoot
As mentioned, a breakpoint is a point at which Access will unconditionally halt the execution of code. You can set multiple breakpoints in your code. You can add and remove breakpoints as your code executes.A breakpoint enables you to halt your code execution at a suspicious area of code. This enables you to examine everything that is going on at that point in your code execution. By strategically placing breakpoints in your code, you quickly can execute sections of code that you already debugged, stopping only at problem areas.To set a breakpoint, follow these steps:
- Press your F9 function key.
- Click in the gray margin area to the left of the line of the code that will contain the breakpoint.
- Click the Toggle Breakpoint button on the Debug toolbar.
- Choose Debug, Toggle Breakpoint.
The line of code containing the breakpoint appears in a different color, and a dot appears, indicating the breakpoint.
Now that you have suspended your code, you can step through it one line at a time, change the value of variables, and view your call stack, among other things.Keep in mind that a breakpoint is actually a toggle. If you want to remove a breakpoint, click in the gray margin area, press F9, or click Toggle Breakpoint on the Debug toolbar. Access removes breakpoints when you close the database, when you open another database, or when you exit Access.It is easiest to get to know the debugger by actually using it. The following example gives you hands-on experience in setting and stopping code execution at a breakpoint. The example is developed further later in the chapter.
![]() | Start by creating a form called frmDebug that contains a command button called cmdDebug. Give the button the caption Start Debug Process. Place the following code in the Click event of the command button: |
Call Func1
End Sub
Create a module called basFuncs. Enter three functions into the module:Sub Func1 ()
Dim intTemp As Integer
intTemp = 10
Debug.Print "We Are Now In Func1()"
Debug.Print intTemp
Call Func2
End Sub
Sub Func2 ()
Dim strName As String
strName = "Bill Gates"
Debug.Print "We Are Now In Func2()"
Debug.Print strName
Call Func3
End Sub
Sub Func3 ()
Debug.Print "We Are Now In Func3()"
MsgBox "Hi There From The Func3() Sub Procedure"
End Sub
Now you should debug. Start by placing a breakpoint within the Click event of cmdDebug on the line called Call Func1. Here are the steps:
Figure 15.6. Code execution is halted at a breakpoint.
