3.1.6 The VB Debugger - 1 PDF
3.1.6 The VB Debugger - 1 PDF
The VB Debugger
Introduction
As you may have already experienced, it is quite easy to make an error
while writing VB programs. Virtually all errors, however minute, will
cause the program to function improperly or to fail completely.
Programming is a demanding task; often anything less than perfection
is not acceptable.
Once errors are made, it can be difficult to find them; this is because the
error will often cause the program to fail without giving any reason.
Fortunately, VB comes with an indispensable tool for locating program
errors. This tool is called the VB Debugger.
Learning Objectives
By completing this module, you will learn how to use the VB Debugger
to effectively remove programming errors from a VB program.
Prerequisites
Before beginning this module, you should have a solid grasp of:
• VB programming structure
• VB syntax
2
The VB Debugger 3
The VB Debugger
The VB Debugger is a useful tool that helps the programmer find (and
correct) program errors. Without this tool, finding errors in a VB
program would be much more difficult.
As code is written in VB, the compiler checks each line for proper
syntax. If the syntax is incorrect, or incomplete, an error box will appear
advising the programmer of the error and suggesting a method of
correction. For example, typing Set objVar = and then pressing the
<Enter> key will bring up an error box that will tell you that VB expects
an object to be placed after the "=" in this line. This is called a Syntax
Error.
If, however, an object that does not exist is placed after the "=" in the
example presented above, no error box will be generated after pressing
the <Enter> key. Rather, the error will occur when the program is run.
This is because the syntax of the line is correct, but a reference to a
fictitious object can not be created. This is called a Run-Time Error.
Run-time errors are the hardest errors to find, and they are also the
errors that cause the most problems for a running program. Typically,
run-time errors will cause the VB program to crash, or to stop
functioning. An error handler can be added to the program to specify
the program’s response to a run-time error. An error handler is a line of
code, usually located at the beginning of the program that begins with
On Error .... Examples of error handlers can be found in the programs
that were developed in the previous module.
Since run-time errors may not be detected by the compiler like syntax
errors, they can be difficult to locate. The VB Debugger was developed
to help with the task of finding and removing these types of errors.
3
4 The VB Debugger
Adding Breakpoints
Run-time errors are often related to the logic or setup of the code. The
code may have the proper syntax, but the values that it produces may
be far from those expected. Error messages may help guide you to the
area in the program that is causing the errors; however, more
investigation is often necessary to determine the cause of run-time
errors.
Once the breakpoint is activated, the line will be highlighted in red and
a red dot will appear in the grey column on the left. When the program
is running, and it encounters a breakpoint, the execution is halted and
the program enters "break" mode. There are four typical actions that
the user can perform while the program is in break mode. These four
actions can be found in the Debug menu in the main menu bar as
follows:
4
The VB Debugger 5
5
6 The VB Debugger
This last application is one of the most common uses for this window.
The VB Debugger can print various types of information in the
Immediate window. Including a line like the following in the Error
handler will result in a brief description of the error being displayed in
the Immediate window.
Debug.Print Err.Description
6
The VB Debugger 7
Watches are added by selecting Add Watch from the Debug menu in the
main menu bar, or by selecting Add Watch from the menu that appears
when the code window is "right-clicked". The Add Watch Dialog
window looks like this:
The desired expression is entered into the area as indicated, the context
is chosen using the drop-down lists, and the watch type is selected.
Once all of the information is entered, pressing the OK button will add
the watch to the Watch window.
This can be used to trace the subroutine and/or function calls that
caused a certain piece of code to be executed.
The Call Stack can be accessed by selecting Call Stack from the Debug
menu in the main menu bar, or by pressing the <Ctrl><L> hot key.
7
8 The VB Debugger
Workshop
This workshop is designed to help you develop your debugging skills.
The information and techniques presented in the last several pages will
be used here to debug a VB program that has a total of nine errors. The
instructor will work through the first few problems with you and
demonstrate the various debugging tools. The remaining errors will be
left as an exercise for you to complete if time permits.
There are three controls that will interact with the VB program. The
"Start HYSYS" button will complete the link to HYSYS and open the
selected case. The "Stream Combo Box" is used to select the desired
stream. Finally, the "Load Conditions" button will move the user
specified values back into the HYSYS case and then update the values
in the spreadsheet. The following flowchart highlights the relationship
between the various controls and the subroutines.
8
The VB Debugger 9
2. Press the Start HYSYS button and note that an error appears. The
error box should look like this:
9
10 The VB Debugger
4. Return to the Excel workbook and press the Start HYSYS button
again. This time a Message Box appears that reads "Could not
load file." Return to the code view and try to spot the location of
the error. Finding an error of this type can be very difficult
without using breakpoints and stepping through the code.
5. Place a breakpoint on the line that begins On Error.... within the
StartHYSYS subroutine. We know that this line works because the
Message box displayed properly. Return to the workbook and
press the Start HYSYS button. This time the code will run
normally until the breakpoint is encountered, then it will enter
the "break" mode.
6. Notice that the line that contained the break point is highlighted
in yellow. This means that it is the current line. Press <F8> to run
this line, and notice that the yellow highlight moves to the next
line.
7. Press <F8> a few more times to move through the code. Notice,
however, that the yellow highlight skips right to the Error
Handler, so we know that the SimCase = ... line is causing the
error.
8. With the yellow highlight on the Debug.Print ... press the <F8>
hot key and then open the Immediate window (<Ctrl><G>).
10. Continue to step through the program, select the HYSYS Case
Workshop_Debug.hsc in the Debug_Starter directory when
prompted. Eventually, you will enter the UpdateComboBox
subroutine, and an error will occur on the next line.
10
The VB Debugger 11
11. Fix this error and reset the program; i.e. press the Stop/Reset
button. Save your program.
12. Press the Start HYSYS button once more and step though the
program past the location of error #3. Continue to step through
the program until another error comes up.
13. Fix this error and save the program. Press the Start HYSYS button
again. All errors in the various subroutines that this command
calls have been fixed; therefore, we can move onto the two other
commands.
14. Choose a stream from the drop-down list. Another compile error
will appear that reads "Type Mismatch". The
"VapourFractionValue" term is highlighted in the code base, so
this is a good place to start looking for the error. Inspect the
"GetStreamValue" subroutine.
15. Fix this error and save your program. Reset the program, press the
Start HYSYS button, and select another stream from the box. A
message box will appear that reads "Error in Retrieving stream
data."
16. Add a breakpoint to the top of this subroutine and step through
the code to find the error. Remember to reset the program and to
press the Start HYSYS button to reset the link.
11
12 The VB Debugger
17. Remove the breakpoint added in step 16. Press the Start HYSYS
button and select a stream from the box. An error box will appear
that reads "Error Retrieving Mass Flow." Add a breakpoint to the
GetStreamData subroutine on the line which retrieves the mass
flow. The description of the error that VB gives is "Method ’Get
Value’ of object ’RealVariable’ failed."
18. Remove the breakpoint from step 17, restart HYSYS and select a
stream. A new error box should appear that reads "Error
Retrieving Compositions". Add a breakpoint to the subroutine
that is causing the error. Notice that the error occurs in the For i =
0 to comps.count loop. The error is described as "Subscript out of
range."
20. Change one of the blue values and press the Load Conditions
button. A number of errors should appear. The error messages
should start with "Error in Setting...." Add a breakpoint to find the
source of the errors. The VB description of the error is
"Permission Denied"
12
The VB Debugger 13
21. Fix this error, and save your program. Test your solution by
restarting the program, selecting a stream, and changing a value.
13
14 The VB Debugger
14