VB IDE (Interface)

Introduction
Visual Basic is an extremely flexible programming product designed for a variety of applications. Students, managers, and people in various technical fields use Visual Basic to learn how to write practical, Windows-based programs; business professionals use Visual Basic to write macros that leverage the documents and capabilities of their Microsoft Office applications; and experienced software developers use Visual Basic to build powerful commercial applications and corporate productivity tools.

Event Driven
An event is a system or user action.

Visual Basic is event-driven, meaning that the code remains idle until called upon to respond to some user or system action (button clicking, menu selection, form opening, etc. Visual Basic is governed by an event processor. Nothing happens until an event is detected. Once an event is detected, the code corresponding to that event (event procedure) is executed. Program control is then returned to the event processor. The operation is event-driven because everything that executes does so as the result of some kind of action. The role of the programmer is to anticipate the events and to write the code that will be executed when the event occurs. A VB application is interactive in the sense that the user is constantly interacting with the program. The user inputs a Customer Id, the program checks the Id in the database and immediately brings up the customer's file or displays a message that the particular Id is invalid.

Creating the Project
First thing to do is to create a Directory/ Folder where you will store your VB Project. Call it "project", for example.

Starting Visual Basic 6.0
In Windows, click Start, point to Programs, and point to the Microsoft Visual Basic 6.0 folder. From the sidekick menu that appears Click the Microsoft Visual Basic 6.0 program icon. The first screen (new project dialog box) contains three tabs; New, Existing, Recent. Here you select the new tab to create a new project. In New tab there are several iconsx prompting you for the type of programming project you want to create., choose Standard EXE. Then you can maximize all the windows for better visibility. Now, save your project. You will first have to save the form - save as marks.frm - and then the Project - call it marking.vbp. From now on, do save the files very frequently.

Writing the first Program
The Visual Basic development environment contains all the resources you need to build powerful Windows-based programs quickly and efficiently. This chapter introduces you to the features and capabilities of the Visual Basic 6.0 program development system, helps you get started with Visual Basic, and describes the various Visual Basic tools and windows that are available to you. you can always obtain help from MSDN

The Visual Basic development environment contains these programming tools and windows, with which you construct your Visual Basic programs: Menu bar, Toolbar, Visual Basic toolbox, Form window, Properties window, Project Explorer, Immediate window, Form Layout window. The exact size and shape of the windows depends on how your system has been configured. In Visual Basic 6.0, you can align and attach (dock) the windows to make all the elements of the programming system visible and accessible.

Loading and Running a Program
Before you can work with a Visual Basic program, you need to load the program into memory, just as you would load a word processing document in a word processor for editing. To load a Visual Basic program into memory and run it
 * 1) On the File menu, click Open Project. The Open Project dialog box appears. With this dialog box, you can open any existing Visual Basic program on your hard disk, attached network drive, CD-ROM, or floppy disk.
 * 2) If necessary, use the Look In drop-down list box and the Up One Level button to locate the program you want to load. Then, double-click the program name. The project file loads the Visual Basic user interface form, properties, and program code(Visual Basic project files are distinguished by the .VBP file name extension.)
 * 3) If the program user interface does not appear, open the Forms folder in the Project window, select the first form, and then click View Object in the Project window. This is an optional but useful step, especially if you want to look at the program user interface in the Form window before you run it.
 * 4) On the Visual Basic Standard toolbar, click Start to run the program. The toolbox and several of the other windows disappear, and the Visual Basic program starts to run.
 * 5) On the toolbar, click End when you want to exit the program.
 * 6) Package & Deployment Wizard makes distributing your applications simple.

Properties Window
The Properties Window is used to establish initial property values for objects. The drop-down box at the top of the window lists all objects in the current form. Two views are available: Alphabetic and Categorized. Under this box are the available properties for the currently selected object. You can view the properties of an object using two methods; right click an object and from the menu select the "properties" option, select an object and click on the properties window icon (button) from the tool bar. If the window is currently docked, you can enlarge it by double-clicking the title bar. To re-dock the Properties window, double-click its title bar again.

Using the Properties window, you can change the characteristics/ attributes (property settings) of the objects in a project this includes form and element properties. A property setting is an attribute of a the interface object. For example, you can change the height and background of a text box control. With Visual Basic, you can display text in any font installed on your system, just as you can in Microsoft word or Microsoft PowerPoint.

Attributes in the Properties Window

The Properties window contains the following elements:

properties you would like to view or to set up.
 * 1) A drop-down list box (menu) at the top of the window, from which you can select the object whose
 * 1) Two window tabs (pages), which list the properties either by alphabetic order or by category.
 * 2) A description pane displayed at the bottom, it shows the name of the selected property and a short description of it.

How to Change Property Settings

You can be able to change property settings by using the Properties window while you are designing the graphical user interface or by using the program code (these changes are applied while the program runs).

Form Layout Window
The Form Layout Window shows where (upon program execution) your form will be displayed relative to your monitor’s screen. It is a visual design tool. With it, you can control the placement of the form, you can use the form layout window to determine the positioning of a form when executed.

When you have more than one form in your program, the Form Layout window can be useful in arranging the forms onscreen exactly the way you want. To position a form in the Form Layout window, simply drag the miniature form (displayed in the form layout window) to the desired location in the window.

Project Window
The Visual Basic project may consist of several files that are linked together to make the program run. The Visual Basic 6.0 development environment includes a Project window that displays files within the project. You can use this window to switch back and forth between these components as you work on the project.



The Project Window displays a list of all forms, reports (connections) and modules making up your application. You can also obtain a view of the Form or Code windows (window containing the actual Basic coding) from the Project window.

Project Window Components

This window lists all the files used in the programming process and provides access to them with two special buttons: View Code and View Object.

To display the Project window, click the Project Explorer button (icon) located on the Visual Basic toolbar. If the window is currently docked, you can enlarge it by double-clicking its title bar. To re-dock the Project window, double-click on its title bar again.

How to Add and Remove Files

The project file maintains a list of all the supporting files in a Visual Basic programming project. You can recognize Visual Basic project files by their .vbp file name extension. You can add individual files to and remove them from a project by using commands on the Project menu or by right clicking on a file in the project explorer. The changes that you make will be immediately reflected in the Project window.

Visual Basic versions 1 through 3, project files have the .mak file name extension but Visual Basic versions 4, 5, and 6.0, project files have the .vbp file name extension. Adding Projects You can have more than one project in the Visual Basic environment, when you load additional projects into Visual Basic with Add Project command on the File menu, outlining symbols appear in the Project window (as illustrated in the image above) to help you organize and switch between different projects.

Code Window
The Code Window is where the actual coding is done. You can activate the code window by double clicking an object or clicking on the code window icon at the top of the project window.

Form Window
When the Visual Basic application is started, a default form (named Form1) with a standard grid (a window consisting of regularly spaced dots) appears in a pane called the Form window. The Form window grid can be used to line up interface elements. Form window is where the user interface is ‘drawn’. When you activate a form during design time, then it is displayed via the form window. There are two ways to place controls on a form:


 * 1) Double-click the tool in the toolbox and it is created with a default size on the form. You can then move it or re-size it.
 * 2) Click on the control in the toolbox, then move the mouse pointer to the form window. The cursor changes to a cross hair. Place the cross hair at the upper left corner of where you want the control to be, press the left mouse button and hold it down while dragging the cursor toward the lower right corner until you attain the size you want for the element. When you release the mouse button, the control is drawn.

To move a control you have drawn, click the object in the form window and drag it to the new location. Release the mouse button.

To re-size a control, click the object so that it is select and sizing handles appear. Use these handles to re-size the object you can also set the size via the properties window or using the code. You can adjust the size of the form by using the mouse or the properties window, the form can take up part or the entire screen.

Tool Box
Contains controls that are used to draw form elements. The controls (Objects) include: Labels, Text box, Combo box, Timer, ActiveX data Objects among others.

VB invisible and visible controls

When you run Visual Basic program, most controls on the toolbox operate like the standard objects in any Windows -based application, and they will be visible to the user. But the toolbox also contains controls that can be used to perform special, operations in the background, within the Visual Basic program. The powerful objects (elements) you create with these controls can be made invisible to the user and do useful work in the background when the program runs. These objects can be used for:
 * 1) Connecting to and manipulating database information.
 * 2) Working with other Windows-based applications.
 * 3) Tracking the time in your programs.

Object Prefix (Object naming convention):
A very important property for each object is its name. The name is used by Visual Basic to refer to a particular object in code. A convention has been established for naming Visual Basic objects. This

Convention is to use a three letter prefix (depending on the object) followed by a name you assign. Below is an example of object prefix Object names can be up to 40 characters long, must start with a letter, must contain only letters, numbers, and the underscore (_) character. Names are used in setting properties at run time and also in establishing procedure names for object events.
 * 1) Form Start with frm e.g frmWatch
 * 2) Combo box start with cmb e.g cmbCountry
 * 3) List box start with lst e.g lstTowns
 * 4) Command Button Start with cmd e.g cmdExit, cmdStart
 * 5) Label Start with lbl e.g lblStart, lblEnd
 * 6) Text Box Start with txt e.g txtTime, txtName
 * 7) Report start with rpt e.g rptEmployees
 * 8) Menu Start with mnu e.g mnuExit, mnuSave
 * 9) Check box Start with chk e.g chkChoice

Building the Program
Building a Windows-based application with Visual Basic involves several programming steps which include: creating the user interface, setting the object properties, writing the program code, saving the project and creating an executable.

Creating the User Interface

After you have establishing a clear goal (purpose) for the program, you can now embark on designing the look and thinking of how it will process information. The complete set of forms and controls used in a program are referred to as the program user interface, this includes all the menus, dialog boxes, buttons, objects, and pictures that users see when they operate the program. In the Visual Basic development environment, you can create all the components of a Windows based application quickly and efficiently (Rapid application development).

Setting the Properties

Properties are programmable characteristics (attributes) associated with forms and their controls (elements). You can set these properties either as you design your program (at design time) or while you run it (at run time). You change properties at design time by selecting an object, clicking the Properties window, and changing one or more of the property settings to new settings. To set object properties at run time, you include the statements in the Visual Basic program code.

Writing the Program Code

You complete building the program (project) by typing the program code for the user interface elements. Writing the program code gives you more control on determining how the program works than you can get by just setting properties of user interface elements at design time. By using the program code, you completely establish your thoughts about how the application:
 * 1) Accepts data
 * 2) Processes data
 * 3) Tests for conditions
 * 4) Determines the order in which the program carries out instructions.
 * 5) How the program outputs and the type of output

The Visual Basic Programming Language

The Visual Basic programming language contains several hundred statements, functions, and special characters. Program code follows particular rules (syntax) required by the Visual Basic compiler. Program code is entered and edited via the Code window, a special text editor designed to track and correct (debug) program statement errors.

Types of programming errors
There are several types of errors that can occur during program development and execution. These include: Syntax errors, Logical errors, Run time errors.

Syntax errors

These are errors that occur as a result of not following the rules of the programming language. For example if you declare a key word (reserved word) as a variable this will result in a syntax error.

Examples:

Wrong declaration

Dim marks As Integers 'Wrong data type assignment Dim array(2) as String 'also wrong variable declaration

Correct declaration

Dim marks As Integer 'Wrong data type assignment Dim marks(2) as String 'also wrong variable declaration

Logical errors

This errors occur as a result of the program not making meaning. The syntax of the program may be o.k but it is not generating the expected results. for example using or using AND instead of OR in an expression.

e.g Typing

If (x > y) OR (x >z) then print x: End if

Instead of If (x < y)

Debugging the program
Debugging is the process of identifying and correcting errors in a program are several ways of debugging the program: Dry running, using VB debugging tools.

Dry running

This is where the programmer writes the program then manually goes through it trying to identify errors within it.

Using the VB Debugging Tools
Visual Basic has several debugging tools that are available for use. Access to these tools is provided both via the menu bar (by using menu options) and the tool bar (by using buttons on the Debug toolbar). These tools include breakpoints, watch points, calls, step into, step over, and step out.

The simplest tool is the use of direct prints to the immediate window.

Printing to the Immediate Window:
It is a special window used at design time to debug and evaluate expressions, print variable values and to execute statements(to test how a statement will perform. You can print to the immediate window directly while an application is running. A few carefully placed print statements can sometimes clear up all logic errors, especially in small applications. To print to the immediate window, use the Print method: Debug.Print [List of variables separated by commas or semi-colons]

Example: Debug.Print X; Y Run the application and then examine the immediate window. Note how, at each iteration of the loop,the program prints the value of X and Y. You could use this information to make sure X is incrementing correctly and that Y values look acceptable.Then remove the Debug.Print statement.

Breakpoints:
In the above example that we placed in the immediate window, the program ran to completion before we could look at the debug window. Sometimes you may want to stop the application while it is running, so that you examine variables and then continue running it again. This can be done by using breakpoints.

A breakpoint is a line in the code that you identify, it is where you want to stop the execution of the program (temporarily), this forces the program into break mode.

To set a breakpoint, put the cursor in the line of code you want to break on. Then, press  or click the Breakpoint button on the toolbar or select Toggle Breakpoint from the Debug menu. The line will be highlighted.

When you run your program, Visual Basic will stop anytime that it reaches lines with breakpoints and allow you to use the immediate window to check variables and expressions. In order to continue program operation after a breakpoint, press the  key, click the Run button on the toolbar, or choose Start from the Run menu. Variable values can also be changed using the immediate window. Simply type a valid expression though you need to be careful since this may completely change program operation.

Watch Expressions:
The Add Watch option found on the Debug menu allows you to establish watch expressions in your application. The watch expressions can be variable values or logical expressions you may want to view or test. Values of the watch expressions are normally displayed in the watch window.

When in break mode, you can use the Quick Watch button located on the toolbar to add watch expressions that you need. To do this put the cursor on the variable or expression that you want to add to the watch list and then click the Quick Watch button. Watch expressions can be edited using the Edit Watch option found on the Debug menu.

Call Stack:
Selecting the Call Stack button from the toolbar (or pressing Ctrl+L or selecting the Call Stack option from the View menu) will display all active procedures (that have not been exited).

Call Stack helps the programmer to unravel situations with nested procedure calls so that you have some idea of where you are in the application.

Single Stepping (Step Into):
When you are at a breakpoint, you may execute your program one line at a time by pressing , choosing the Step Into option in the Debug menu, or by clicking the Step Into button found on the toolbar. This process is called single stepping. It allows you to watch how variables change (in the locals window) or how your form changes, one step at a time.

You may step through several lines at a time by using the Run To Cursor option. With this option, click on a line below your current point of execution. Then press Ctrl+ (or choose Run To Cursor in the Debug menu). doing this will make the program to run through every line up to the cursor location, then stop.

Procedure Stepping (Step Over):
While single stepping your program, when you come to a procedure call that you know is functioning properly, you can perform procedure stepping. This option executes the entire procedure at once, rather than one step at a time.

To move through a procedure using this method, press Shift+, choose Step Over from the Debug menu, or press the Step Over button found on the toolbar.

Function Exit (Step Out):
While stepping through your program, if you wish to complete the execution of a function you are in, without stepping through it line-by line, you can choose the Step Out option. This option completes the function and returns the program execution to the procedure that is accessing that function.

To perform the step out process, press Ctrl+Shift+, choose Step Out from the Debug menu, or press the Step Out button on the toolbar.

Debugging Strategies:
Being the programmer and knowing your code best, you can usually quickly narrow down the areas with bad lines. Don’t set up so many elaborate debugging procedure if you haven’t tried a simple approach to find your error(s) first. Many times, just a few intelligently-placed Debug.Print statements or a few examinations of the immediate and locals windows can be able to solve the problem.

The best approach to debugging is called Divide and Conquer. If you’re not sure where the error is, guess somewhere in the middle of your application code. Then set a breakpoint there. If the error hasn’t shown up by then, you move to the second half of your code then you start looking for it there. But if it shows up, then it’s in the first half. Repeat the division process over and over until when you’ve narrowed your search.

Linked Pages

 * 1) VB Data Types Variables & Constants
 * 2) VB Operators
 * 3) VB Input/Output
 * 4) VB Expressions
 * 5) VB Control Structures
 * 6) VB Data Formatting & Conversion
 * 7) VB Data Structures
 * 8) VB Message Box
 * 9) VB Procedures and Functions
 * 10) VB File I/O
 * 11) VB Error Handling
 * 12) VB Database Connection
 * 13) VB 6.0 Sample Codes
 * 14) Free VB 6.0 Projects