IntroVB lll
IntroVB lll
Visual Basic (VB) is the fastest and easiest way to create applications for MS Windows.
Whether you are an experienced professional or brand new to Windows programming, VB provides
you with a complete set of tools to simplify rapid application development.
The “Visual” part refers to the method used to create the graphical user interface
(GUI). Rather than writing numerous lines of code to describe the appearance and location
of interface elements, you simply put prebuilt objects into place on screen. The “Basic”
part refers to the BASIC (Beginners All-purpose Symbolic
Instruction Code) language. We need to notice that VB has evolved from original BASIC
language and now contains several hundred statements, functions, and keywords, many of
which relate directly to the Windows GUI.
The VB programming is not unique to Visual Basic. The following applications or
programming also used VB:
-VBA in MS Word, Excel, and Access
-VB Script for web-based programming
It is necessary for us to have a better understanding some of the key concepts VB has
built. A simplified version of the workings of Windows involves three key concepts –
windows, events, and messages.
A window is a rectangular region with its own boundaries. You are probably already aware
of several different types of windows: an explorer window, a document window, or dialog box.
Other types of windows include:
-command buttons
-icons
-text boxes
-option buttons, and
-menu bars
The MS Windows operating system manages all of these many windows by assigning
each one a unique id number (window handle or hWnd). Events can occur through user actions
such as mouse click or a key press, through programmatic control, or even as a result of another
window’s actions. Each time an event occurs, it causes a message to be sent to the operating
system. The system processes the message and broadcasts it to the other windows.
Starting VB IDE
You can start VB IDE either from clicking Start on the Taskbar or clicking VB icon on the
desktop (Figure 1).
Menu bar
Tool bar
Toolbox
Project E xplorer
Form designer
Properties window
Figure 1. VB IDE.
-Object browser
-Code editor window
-Form layout window – allows you to position the forms in your application using a small graphical
representation of the screen.
-Immediate, Locals, and Watch windows under View menu are used for debugging.
Environment Options
VB provides a great deal of flexibility, allowing you to configure the working environment to
best suit your individual style. Two different styles are available for the VB IDE:
-single document interface (SDI): with SDI, all of the IDE windows are free to be moved anywhere on
screen.
-Multiple document interface (MDI): all of the IDE windows are contained within a single resizable parent
window.
The IDE will start in the selected mode the next time you start Visual Basic.
I am using the classical example of “Hello World!” in programming textbook. There are three
main steps to creating an application in Visual Basic:
In our example here, we want to display “Hello World!” in a text box once you click a
command button.
-Setting properties
-Writing code: double click the command button, then code editor window will be displayed, type
text1.text = “Hello World!”
-Running application
Once the user enters the DBH for a tree, the basal area for that tree should be displayed in a
text box. The basal area is calculated by using the following equation:
2
BA = 0.005454154*DBH
2
Where, BA is basal area in ft and DBH is the tree’s diameter at breast height in inches.
The results should be displayed in a list box for comparison among trees. Here are the
controls we need in this project:
-text box
-list box
-command button
VISUAL BASIC CHARACRER SETS AND DATA TYPES
VB Character set
Visual Basic Character set refers to those characters acceptable or allowed in VB
programming eg GB is unknown in English likewise X is unknown in Yoruba language.
The Microsoft Visual basic character sets consists of the following:
1. Alphabet A/ a – Z/z (Both Upper and Lower Case)
2. Numeric Digits 0 – 9
3. Decimal point (.)
4. Grouping Characters (eg. Comma, colons, Semicolons, single and double apostrophe,
parenthesis)
5. Relational Operators (eg. =, <>,>,< etc)
6. Arithmetic Operators (eg. +, -, /, =. etc)
7. Blank character
Data Types
The Visual Basic language works with all kinds of data. Before you learn how to manipulate data,
you will learn how to distinguish among the various data types, that Visual Basic supports. Some
data falls into more than one category. For example, a dollar amount can be considered both a
currency data type and a single data type. When you write a program, you need to decide which data
type best fits your program's data values.
Creating a VB Project
From the Start menu, click All Programs|MS Visual Studio 6.0|Visual Basic 6.0.
You start a new project by choosing New Project from the File menu, then selecting
Standard EXE in the New Project dialog box (when you first start Visual Basic, the New Project
dialog box is presented). VB creates a new project and displays a new form for you. Now we
need to design the interface. What you need to do are as follows:
Coding
Double click the command button and a code-editing box will pop out. Type the following
lines under the command button 1 (cmdCalBA).
Dim DBH, BA
DBH = txtDBH.Text
BA = 0.005454154 * DBH * DBH
txtBA.Text = BA
End Sub
End
End Sub
Remember!! Now, you need to save the project again by clicking the save button on the menu
bar.
Run the Project
Use the arrow button on the menu bar to run the project. You enter 12 in DBH box, then
click ‘Calculate BA’ button, you will add the first result to the list box. If you change the DBH
from 12 to 13, then click the ‘Calculate BA’ button; you will add the second result to the box
(Figure 3). You can repeat the above procedures as you wish.
This application demonstrates how a data control and a DB grid control can be used to
display a table of information from a database. VB makes it easy to access database
information from within your application. The data control provides the ability to navigate
through the database recordset, synchronizing the display of records in the grid control with
the position in the recordset.
The database we are going to use is dbStudent created in the database application section.
The recordset is table tblCourse in the database.
Creating a Project
You begin creating the application by choosing New Project from the File menu, then
selecting Standard EXE in the New Project dialog box (when you first start Visual Basic, the
New Project dialog box is presented). VB creates a new project and displays a new form.
Now we need to design the interface – putting data control, DBGrid control and buttons on
the form. Since the DBGrid is not in the default toolbox, we need to add it there. What we can
do are:
a.Select Components under Project menu, then the Components dialog box will be displayed.
b.Find Microsoft Data Bound Grid Control 5.0 (SP3) in the controls list box and check the box to its
left.
c.Click the OK button, the icon for the DBGrid control will appear in the toolbox.
Use the toolbox to draw the controls on the form (Figure 4).
Figure 4. Controls on the form.
Setting Properties
In the Properties window, set properties for the objects according to Table 2. Use the
default settings for all other properties.
Now, save your project with a name of prjFirstapp. The interface of your project will look like
that (Figure 5).
Figure 5. The interface of your application.
Double-click the form or control to display the Code window, and then type the code for
each event procedure.
Add this code to Form_Load event procedure to connect the database and retrieve data from
the table when the program first starts.
End Sub
Add the following code to cmdDelete_Click event procedure to delete a selected record from
the DBGrid when the Delete button is clicked.
End Sub
Add the code to cmdClose_Click event procedure to end the application when you click Close
button.
End Sub
There are two ways that you can use to run the application:
a.From Run menu, click Start
b.From toolbar, click the button
Start
Microsoft Corporation. 1998. Visual Basic 6.0 – Programmer’s guide. Microsoft Press. Redmond,
WA.