Last updated: Sunday 18th January 2009, 11:10 PT by AHD

 

 

 

Debugging (finding errors in) programs written in Visual Basic

 

For Visual Basic 6.0 Working Model and VB Versions 6.0 and 5.0

 

 

 

Printable version

 

 

 

1.  Setting up the development environment for debugging

 

 

Follow the procedure as shown in the following screen examples.

 

Use exactly the same settings as those shown.

 

(Note the screens shown are for Visual Basic 5.0, but

versions 6.0 and the 6.0 Working Model have very similar screen layouts.)

 

 


 


 


Set the Editor settings as shown below:

 

 


 

 

 

 

 

 

 


Set the Editor Format as shown below.

 

 


 

 

 

 

 


Set the General settings as shown below.

 

 


 

 

 

 

 


Set the Docking settings as shown below.

 

 


 

 

 

 

 


Set the Environment settings as shown below.   

Note: The text in the Templates Directory text box may be different from what you see above.

Do not change the text in the Templates Directory text box.

 

 


 

 

 

 


Set the Advanced settings as shown below.

 

 


 

 


In Version 6.0 you will also see a textbox for the HTML editor which by default is Notepad.exe.

 

 

 

IMPORTANT

 

After setting the options you now need to exit right out of the Visual Basic development environment by double-clicking in the top left-hand corner of the VB6 window, then start Visual Basic up again in order for the SDI – single document interface to take effect.  The SDI environment will allow you to see the application running and view the code and debugging windows at the same time, while seeing the desktop in the background.

 

 

Download the following files to your C:\temp folder:

 

http://www.annedawson.net/proDebug.vbp

 

http://www.annedawson.net/frmDebug.frm

 

 

Now run the project proDebug.vbp (by double clicking on the file name).

 

 

The next screen shows you how to place the Debug toolbar on the screen…

 

 


 

 

 


We also want to view the Immediate and Watch windows…

 

 


 

 

 

 

 

 

 


 

 

 

 


Now we will add a watch expression – an expression whose value will be displayed in the watch window:

 

 


 

 

 

 

 


In the Add Watch window,

 

type shpBall.Left into the Expression box

 

select Form_Load from the dropdown list in the Procedure box

 

select frmDebug from the dropdown list in the Module box

 

 

 

 

 


 

 

 


Now start to run the program one line at a time, keeping an eye on the value in the watch window.

 

Keep pressing the F8 key to execute the code one line at a time. 

 

If the immediate window or the watch window disappears,

you can always view them again by selecting them from the View menu. 

 

 

 


 

 

 


Note: When you first press the F8 key,

you will see a yellow bar in the Code window

with a little yellow arrow in the left margin.

 

Each time you press key F8, one line is executed

and the yellow bar moves on to the next line of code.

 

The code highlighted by the yellow bar will not be executed (run)

until you press key F8 again.

 

 

 

 

 


 

 

 

 


 

 

 

 


All VB programs have a Form_Load() procedure. The purpose of the Form_Load()  prodedure is to do any initial processing needed before the form itself is displayed on the screen. For example, the Form_Load()  procedure can be used to set the size and position of the form when it first appears on the screen. When in the Form_Load()  procedure, keep pressing key F8 until you no longer see a yellow bar in the Code window. When no yellow bar is visible in the Code window, this means that the Form_Load()  procedure has completed, and the form is displayed on the screen. At this point the program waits for some event to happen on the form, such as a click on a button.

 

 

 

After going through the Form_Load()  procedure, the VB application waits until the user clicks on a command button – either ‘Throw Ball’ or ‘Quit’.  Click on the ‘Throw Ball’ button.  If you can’t see the application (a blue window with a yellow ball on it) try rearranging the windows on the screen until you do see it.  Ideally, you should be able to see the code window, the application (the running program), and the immediate and watch windows all at the same time.

 

 

 

Note: the next line of code to be executed is shown by a yellow bar in the Code window with a little yellow arrow in the left margin. Each time you press key F8 one line is executed and the yellow bar moves on to the next line of code. The code highlighted by the yellow bar will not be executed (run) until you press key F8 again.

 

 

 

 


 

 

 


The purpose of the line shown in the yellow bar in the screen above is to make the command button called

cmdThrowBall invisible.  As soon as key F8 is clicked, you will no longer see that button on the form.

 

Keep selecting Debug then Step Into (or press function key F8) to step through the code one line at a time.

 

Watch what happens to the application, and keep an eye on the watch expression value.

 

 

IMPORTANT

 

We really want to keep an eye on the value of shpBall.Left in the cmdThrowBall_Click( ) event rather than the Form_Load( ) event which currently is out of context.  We need to edit the Watch – to do this select Edit Watch from the Debug menu.  Click the drop down arrow of the Procedure text box and select cmdThrowBall_Click, then click on the OK button.  Now, repeatedly press the F8 key to step through the code one line at a time.  Watch the ball move on the blue application screen.  Watch the value of shpBall.Left change in the Watch window.  Try adding another watch to the watch window by selecting Add Watch from the Debug menu.

 

 

 

 


 

 

 

 


 

 

 

 


 

 

 

 

 

 

 

 


 


 

 


See how the value of shpBall.Left changes within the Do While … Loop, as you repeatedly press key F8.  The Do While … Loop causes the two statements within it to repeat as long as the condition (shpBall.Top >= 200) is true.

 

 


 

 

 

 


If you want to look at the value of a variable at any point in the running of the code, click in the immediate window and type the variable name preceded by a ? (which means ‘print’).  Notice how this value is not automatically updated, but you can refresh the value by clicking anywhere on the name of the value (eg shpBall.Top) and pressing the Enter key. Or try just holding the cursor over the name shpBall.Top for the current value to appear. You can even change the value of the variable within the immediate window and see the immediate effect on the application.

 

 

By setting up the VB development environment as suggested, and by using the debugging techniques described above, you will be able to watch your application running and be able to monitor values at the same time.  Efficient use of the debugging tools should speed the development of your code, and produce applications which run as intended.