Watching the Execution of Your VBA Procedures

To help you understand what's going on when your code runs, and how your custom object works, let's walk through the cmdAdd_Click procedure. Treat this exercise as a brief introduction to the debugging techniques that are covered in detail in the next chapter.

┬ęCustom Project 8-1 (Step 9): Custom Project Code Walkthrough

1. Open frmEmployeeSalaries in Design view and choose View | Code.

2. Select cmdAdd from the combo box at the top left of the Code window.

3. Set a breakpoint by clicking in the left margin next to the following line of code:

If txtLastName.Value = "" Or txtFirstName.Value = "" Or _ txtSalary.Value = "" Then

If txtLastName.Value = "" Or txtFirstName.Value = "" Or _ txtSalary.Value = "" Then

Figure 8-4: A red circle in the margin indicates a breakpoint. The statement with a breakpoint is displayed as white text on a red background.

4. Return to frmEmployeeSalaries in Design view and choose View | Form View to run the form.

5. Enter data in the Last Name, First Name, and Salary text boxes, and click the form's Add button. Visual Basic should now switch to the Code window because it came across the breakpoint in the first line of the cmdAdd_Click procedure.

6. Step through the code one statement at a time by pressing F8. Visual Basic runs the current statement and then automatically advances to the next statement and suspends execution. The current statement is indicated by a yellow arrow in the margin and a yellow background. Keep pressing F8 to

Part I

execute the procedure step by step. After Visual Basic switches to the EmpOperations module to run the AddEmployee procedure and encounters the With emp statement, it will run the function to set the employee ID and will go out to execute the Property Let procedures in the CEmployee class module.

Figure 8-5: When Visual Basic encounters a breakpoint while running a procedure, it switches to the Code window and displays a yellow arrow in the margin to the left of the statement at which the procedure is suspended.
Figure 8-6: Setting the properties of your custom object is accomplished through the Property Let procedures.

Introduction to Access 2003 VBA Programming

7. Using the F8 key, trace the execution of the cmdAdd_Click procedure to the end. When VBA encounters the end of the procedure (End Sub), the yellow highlighter will be turned off. At this time, switch back to the active form. Enter data for a new employee, then click the Add button. When Visual Basic displays the Code window, choose Debug | Clear All Breakpoints. Now press F5 to run the rest of the procedure without stepping through it.

VBA Debugging Tools

Visual Basic provides a number of debugging tools to help you analyze how your application operates, as well as locate the source of errors in your procedures. See the next chapter for details on working with these tools.

0 0

Post a comment