Debugging Code

 < Day Day Up > 

It is not often that code will be completely free of error the very first time you write it, so it is inevitable that you will have to fix errors. Debugging is the term given to finding and fixing errors in code.

There are two primary ways that code can fail. The first is that instructions might be written incorrectly. They might use the wrong syntax or have spelling errors. The second is that the instructions you have written are the wrong instructions. They might have perfect syntax, but the way they are ordered or structured may cause them not to do what you expect them to do. The VBE will try to help prevent the first type of error through built-in syntax checking. It assists in the second type by allowing you to set breakpoints and watches to closely monitor what the code is doing step-by-step.

Syntax Checking

When you enter code and move the cursor off a line, the VBE automatically checks the syntax of the line. For example, if you omit Then from the end of an If statement, the VBE generates a compile error with the message Expected Then or GoTo . This feature of the VBE eliminates many errors that might otherwise be hard to find, before you even start testing. The VBE can detect a large number of syntax errors.

Breakpoints, Watches, and the Immediate Window

When you have code with a large number of steps and you know only the initial state and the outcome, it is difficult to figure out where the root of your problem lies. The VBE provides the ability to view your code as it executes and to check the values of your variables . The main tools to do this are breakpoints, watches, and the Immediate window.

The VBE lets you step line-by-line through code, but if you have a large amount of repetitive code, it could take a long time to get to the point in which you are interested. To facilitate this, you can set breakpoints in code. Breakpoints allow code to run freely up until the specific line in which the breakpoint is set. The code then stops, and you can evaluate the status of different variables or use the breakpoint to start stepping through the code line-by-line .

To test code as part of debugging, follow these steps:

  1. In the VBE, click anywhere on the For Each instruction line and select Debug, Toggle Breakpoint, or press F9. This highlights that row of code in red and places a red dot in the margin to the left.

  2. Select Run, Run Sub/UserForm, or press F5. This executes the subroutine the cursor is in. The red line turns yellow, to indicate that it is the next instruction to be executed. A yellow arrow is displayed in the left margin. Project stops executing the code when it reaches your breakpoint.

  3. Press F8 once. This makes Project single-step through the code. The For Each statement turns red again, and the first If statement turns yellow.

  4. Hover your mouse over T in (T Is Nothing) . You should see a ToolTip with T = 1 in it. This means Task ID 1 is the current active task (each object has a default property; the default property for a task object is its ID).

  5. Click and drag with your mouse to select T Is Nothing , and then move the mouse over the selected (blue) area. Now the ToolTip says T Is Nothing = False .

  6. Hover your mouse over each property ( OutlineLevel , Summary , OutlineParent. Name , and so on) and see what values they have.

  7. Press Ctrl+G. This activates the Immediate window and places the cursor there. You can use this window to investigate all the properties of any active object. You can also use it to test any single lines of code, to assist you in getting them right and deciphering exactly what is happening. For example, if T is pointing to each task in turn , you can find out which task it's pointing to when stopped at a specific breakpoint by typing ?T.Name or ?T.Id in the Immediate window and then pressing Enter. The name or ID of the task pointed to by T is then displayed.

  8. Type ?T.Name and press Enter. ? is shorthand for Debug.Print . The value of the task's name should appear in the next row. The ? allows you to have the value of any variable or property printed to the Immediate window. A line without the ? is used to execute a statement.

  9. In a blank line, type T.Name="Test" (because Test is the task name displayed by ?T.Name ) and press Enter.

  10. Scroll back to and click on the ?T.Name line and press Enter again. The task name should now be Test .

  11. Press Alt+Tab to return to Project 2003 and confirm that the task name has changed to Test . Use Alt+Tab to get back to the VBE, and then reset the task name by using another T.Name= line in the Immediate window.

When you're testing code, you can also try typing ?Activecell.Task.Name . This can be very useful, but it only has a few properties. By using the Task property in this example, you can access all the task information for the task in the selected row.

Other common and very useful properties of tasks that you need to work with are Work and Duration . In the Immediate window, go to a blank row and type ?T.Duration and then press Enter. The answer should be a rather large number that looks nothing like what you see in the Duration column of Project 2003.

Project stores all Duration and Work values in minutes. To convert these to days, divide by 60 and then by the number of hours per day listed in the Calendar tab of the Options dialog box. The default number of hours per day is 8, so ?T.Duration/60/8 should display the correct number of days' duration.

TIP

ActiveProject.HoursPerDay should give you the number of hours per day to use in the formula instead of 8. Try typing ?T.Duration/60/ActiveProject.HoursPerDay in the Immediate window to confirm this.


You have already learned that the variable T is Nothing until the For Each loop, when it points to each task in turn. You also need to be aware that the variable has no value at all in the Immediate window if the macro hasn't stopped at a breakpoint.

If you type ?T.Name in the Immediate window when the code hasn't stopped at a breakpoint, an error is generated. T only points to a task after the For Each T In ActiveProject.Tasks statement has been executed. After End Sub is reached, T has no value, and any attempt to use it in the Immediate window fails.

TIP

Remember that if you want more information on a property or object, you can click it and press F1 to call up the help system.


When the code has stopped at a breakpoint, notice that, in the gray border on the left of the VBE, the yellow line has a yellow arrow and the breakpoint has a red spot. Click the red spot to toggle the breakpoint off or on, or click anywhere on the line's code and press F9 again.

You can also click and drag the yellow arrow to move the yellow line (changing which line will be executed next). Try moving it back to the For Each row. Press F8 again and again, and watch the code being stepped through. At any time, you can press F5 to have it run continuously to the next breakpoint or the end of the subroutine, whichever comes first.

Enter a line like Set T=ActiveProject.Tasks(1) in the Immediate window and press Enter, and the line is executed. This is a good way to get a line of code working properly before you copy it back to the editing pane.

Remember that when you click any instruction and press F9, the code can be made to stop. You then hover your mouse over any variable to get a ToolTip that displays its value, or you can use ? to print values in the Immediate window. You can also single-step through the code to see exactly what is happening, correcting the code as you go. You can also use Alt+Tab to swap between the VBE and Project (or any other program you are controlling from Project VBA) to look at what the code has done.

There are two alternatives to using the Immediate window. The first is the Locals window, which you display by selecting Locals from the View menu. The Locals window contains the names , values, and properties of the objects that are being evaluated in code. Because it shows every possible piece of information about the objects, it can be a bit difficult to navigate through, but it is helpful if you aren't exactly sure of the specific property you are looking for.

The second alternative is the Watch window, which you display by selecting Watch from the view menu. This window first appears with no watches set. To set a watch, highlight the item in the code that you want to watch, and then go to the Watch window, right-click, and select Add Watch. You can set a watch to display the value of an expression (just as you can get the value by hovering the mouse above the variable). You can also set a watch to break when the value of an expression you are watching turns either true or false. Figure 6 shows the code stopped where the expression "T is Nothing" has become true.

Figure 6. The Watch window shows watches for an expression "T is Nothing" and for a variable.

graphics/ar02fig06.jpg

If you know what you are looking for, using the Watch window is the simplest way to display the values you want.

 < Day Day Up > 


Special Edition Using Microsoft Office Project 2003
Special Edition Using Microsoft Office Project 2003
ISBN: 0789730723
EAN: 2147483647
Year: 2004
Pages: 283
Authors: Tim Pyron

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net