Intro VB
Intro VB
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
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 Explorer
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
The IDE will start in the selected mode the next time you start 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:
BA = 0.005454154*DBH2
Where, BA is basal area in ft2 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
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.
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).
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).
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
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
References
Microsoft Corporation. 1998. Visual Basic 6.0 – Programmer’s guide. Microsoft Press.
Redmond, WA.