Sams Teach Yourself Visual Basic 6 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- Introduction
- Who Should Read This Book
- What This Book Will Do for You
- Can This Book Really Teach Visual Basic in 24 Hours?
- What You Need
- Files on the Visual Basic Distribution CD-ROM
- Conventions Used in This Book
- Enough! Time Is Ticking!
- Part I: Introducing Visual Basic
- Hour 1. Visual Basic at Work
- Hour 2.Analyzing Visual Basic Programs
- Hour 3.Controls and Properties
- Hour 4.Examining Labels, Buttons, and Text Boxes
- Part II: Coding the Details
- Hour 5.Putting Code into Visual Basic
- Hour 6.Message and Input Boxes
- Hour 7.Making Decisions
- Hour 8.Visual Basic Looping
- Part III:Putting Code to Work
- Hour 9.Combining Code and Controls
- Hour 10.List Boxes and Data Lists
- Hour 11.Additional Controls
- Hour 12.Dialog Box Basics
- Part IV:Programming with Data
- Hour 13.Modular Programming
- Hour 14.Built-In Functions Save Time
- Hour 15.Visual Basic Database Basics
- Hour 16.Printing with Visual Basic
- Part V:Sprucing Up Programs
- Hour 17.Menus and Visual Basic
- Hour 18.The Graphic Image Controls
- Hour 19.Toolbars and More Graphics
- Hour 20.Writing Correct Applications
- Part VI:Advancing Visual Basic Applications
- Hour 21.Visual Basic and ActiveX
- Hour 22.Object Basics
- Hour 23.Distributing Your Applications
- Hour 24.Online Visual Basic
- Part VII:Appendixes
- Appendix A.Operator Precedence
- Appendix B.Answers
- Appendix C.Using the CD-ROM
Setting Breakpoints
You'll always enter break mode from the runtime mode. Only after you begin a program's execution will the break mode be available, because only at runtime are the variables and controls initialized with values. Here are the ways that you can move from runtime mode to break mode:
- Press Ctrl+Break during the program's execution at the place where you want to enter break mode. Stopping on one exact line of code is virtually impossible when using Ctrl+Break.
- Select Run | Break from the Visual Basic menu bar during the program's execution.
- Click the Break toolbar button (the toolbar button with two small vertical bars next to the Run button).
- In design mode or during a break mode, set a breakpoint on a particular line at which you want the execution to halt. By setting a breakpoint, you can specify the exact line of code where Visual Basic is to enter break mode.
- The menu's Debug | Add Watch dialog box lets you specify a break expression that Visual Basic monitors and uses to halt the program's execution when the expression becomes true.
- If a runtime error occurs, such as the undefined divide-by-zero math operation you saw earlier, Visual Basic enters the break mode at the offending line.
The most accurate and common way to enter break mode is by setting a breakpoint. To set a breakpoint, find the line where you want execution to halt at a breakpoint and set a breakpoint at that particular line of code. The following steps walk you through setting a breakpoint:
- Load the Controls.vbp project that comes with Visual Basic (look in the Samples folder).
- Press F7 to open the Code window.
- Locate the opt486_Click() event procedure.
-
Find the following line of code in opt486_Click():
strComputer = "486"
- Move the mouse cursor to the line and click the mouse button. The text cursor appears at the mouse click's location.
-
Select Debug | Toggle Breakpoint to set a breakpoint. (You'll see from this menu bar command that F9 is the shortcut key for this command. Also, clicking the toolbar's hand icon would place a breakpoint on this line of code as would clicking to the left of the line.) Figure 20.6 shows how your Code window should appear. Visual Basic changes the color of the line to let you know that a breakpoint will take place on that line during the program's execution.
You can turn off a breakpoint by selecting Debug | Toggle Breakpoint (or by pressing F9) once again. You can set as many breakpoints as you need throughout a program. Leave this breakpoint in place for now. By setting the breakpoint, you're requesting that Visual Basic halt the program and enter break mode when execution reaches this line of code. Close the Code window and run the program by pressing F5.
Figure 20.6 The breakpoint line is highlighted.
The program appears to run as usual. The opening dialog box appears. Click the Option Buttons command button to see what happens when execution reaches the breakpoint. The execution continues, as usual, as long as the breakpoint isn't reached, but when the breakpoint line is reached, execution halts.
As soon as Visual Basic reaches a breakpoint's line, Visual Basic enters break mode before executing the breakpoint line. The opt486_Click() event procedure assigns a string literal to a string variable and then calls another procedure to load that variable into a label. The breakpoint that you set occurs in the middle of the assignment code.
Follow these steps to see what kinds of things you can do at a breakpoint:
- Move your mouse to the string variable. After a brief pause, a ToolTip-like message pops up to tell you that the variable contains a null string value (nothing is yet assigned to the string). Drag the mouse to highlight the strComputer variable on the breakpoint's line.
-
Select Debug | Quick Watch. The menu option produces the Quick Watch dialog box. Visual Basic displays the breakpoint line, the variable name, and the current value that's a null string, as shown in Figure 20.7.
Figure 20.7 Looking at the variable's null value.
- Click Add to add the value to the Watch dialog box. Whereas the Quick Watch dialog box is useful for looking at a variable at its current location, the Watch dialog box keeps track of multiple variables that update as the program executes.
-
Select View | Toolbars | Debug to display the special floating Debug toolbar. Most tools you need for interactive debugging appear on this toolbar. As Figure 20.8 shows, the Debug toolbar includes its own Quick Watch button. In addition, Figure 20.8 shows you the Watches window, where the variables and controls you want to watch reside.
The difference between the Quick Watch window and the Watches window is that you can, at any breakpoint, highlight a variable and display its value and surrounding code and datatype by clicking the Debug toolbar's Quick Watch button. If you want to keep a running list of watch variables, however, you must add the variables and controls to the Watches window. If you start the program again and hit another breakpoint later (you can set multiple breakpoints), the Watches window still shows the variables and controls you placed there, but the Quick Watch window will no longer appear until you request it again with another highlighted value. When you single-step through code, you execute subsequent program instructions, one statement at a time, looking at values and testing the logic as you go.
Figure 20.8 Looking at the variable in the Watches window.
- Usually, the programmer will single-step through a few lines of code after reaching a breakpoint. To step through the code one line at a time, you can choose the Debug | Step Into option (or press F8). As you single-step though the code, Visual Basic highlights the next line of execution. At any point during the single-step process, you can examine variables and controls with the Quick Watch dialog box and add them to the Watches window.
Stepping Through Code | Next Section

Account Sign In
View your cart