Lecture 1 - Introduction to Starting Visual Basic
Table of contents
1.0 Introduction to VB - About the Language
1.1 Introduction to VB - Why Use VB?
1.2. Introduction to VB -Summary of the advantages and disadvantages of VB
1.3 Introduction to VB - Further Reading
2.0 The Visual Basic IDE
2.1 The Visual Basic IDE - Further Reading
3.0 Building VB Applications
3.1 Building VB Applications - VB Apps and Projects
3.2 Building VB Applications - The VB Design Cycle
3.3 Building VB Applications - Standard VB Controls
3.4 Building VB Applications - Writing VB Code
3.5 Building VB Applications - Further Reading
Microsoft Visual Basic
(VB) is an extremely simple, yet powerful programming language, which is useful for building many kinds of Windows applications. It is
capable of constructing almost any program that any other Windows-capable programming language can produce, however its power and the
reason why it has sold millions of copies world-wide is the ease with which you can build practical business oriented software
solutions.
Learning the VB language does not restrict you to using Microsoft Visual Basic to build applications.
Visual Basic for Applications
(VBA), a subset of the VB language that can be found in many applications such as Microsoft Office and Visio, enables you to customise
the host application,
relatively
painlessly. And
VBScript
(or Microsoft Script), a subset of VBA, which is fast becoming a popular scripting language for World Wide Web sites. In this course we
focus mainly on VB, however, in the last lecture we shall take a brief look at VBA and VBScript
VB Concepts and a Short History
As its name suggests VB is a
Visual programming language
. This makes the construction of programs a simple 3 step process.
Build the User Interface
, which will consist of a combination of windows, menus, buttons etc. Drawing the UI using the mouse much like a DTP program draws a
page.
Customise the properties
of the interface, so that it has the look and feel that you are after.
Attach Program Code
to make the interface work in the way you desire.
We shall take a closer look at this process later in section 3.2 of this lecture.
The language that you use to attach code to your program is BASIC (quite obvious really!!). BASIC has had a long history in the computer
industry, it was designed in the 1970's as a language for teaching programming. It has the advantage that its syntax is very similar to
English prose, making it very easy to pick up.
Microsoft has brought BASIC bang up to date with VB adding many new features to make windows programming that much easier. VB1 was
released in approximately 10 years ago and was one of the first of a new breed of visual programming languages which provided ease of use
through graphical design techniques. By version 5, database support was added through the use of the Microsoft Jet Database Engine which
enabled data to be shared across other Microsoft products such as Access. VB5 added native code compilation, which heralded a huge leap
forward in the speed which VB programs executed. Finally VB6, the latest version, offers features such as support for the design and
implementation of ActiveX components, strong Internet connectivity, access to remote databases, and much more
VB is
Event Driven
, which means that programs don't follow the sequential path of traditional languages such as
Pascal
. Instead snippets of sequential code are associated with certain events such as the clicking of a button. Then when the program is run,
events will occur (or are said to fire) in response to changes in the environment (such as the ticking of the computers internal clock)
or by intervention from the user (such as clicking a button). When an event fires the associated sequential code is then executed. Once
this is completed the system will wait until the next event fires, and the whole process repeats.
Example of Visual, Event Driven Programming
In this example we see a dialog box which contains the usual attributes of a window:
A Title bar,
A Close Icon, and
A Title.

FIGURE 1, SCREEN SHOT OF DIALOG BOX WITH LABEL TEXTBOX AND BUTTONS
Then within the window we see that certain objects have been added:
Two Command Buttons marked Ok and Cancel,
A Label marked Name, and
A Text Entry Box filled with the string "Text1".
These have been drawn onto the window (or form
as it is known in VB) just like you draw boxes in a drawing program. First you select the object such as a text box from a palette and
then you click and drag on the form where you want the control to appear.
Then once the interface is built, code can be attached to different events. For example, each button will have a
Click event, which will fire whenever the user presses it. To this event we can associate some code which in the case of the OK button saves
the contents of the text box and closes the window, and in the case of the cancel Button just closes the window. In workshop 1 we shall
work through a practical example of this.
VB is also an
Object-Oriented (OO) programming language, this means that most things in a VB program are designed as objects. In traditional programming languages
data is stored quite separately from the operations which can be performed on that data. For example, you may implement a queue structure
in Pascal as an array, with a collection of procedures and functions that operate on the queue, returning data such as the head of the
queue or performing actions such as adding data to the end of the queue. Not so in an OO programming language.
An object is a construct that stores data and the operations that can be performed on the data together. So we could define a queue
object which internally holds the data and makes a series of operations, which return the head and add to the tail, visible to he outside
world. The beauty of OO is that objects abstract the complex internal behaviour from the programmer, it is almost as if an object is a
black box with a series of connections that operate the box. All we are concerned with are the connections, not with how the box actually
implements its behaviour. So in our example other programmers need never know that internally the queue is stored as an array, all they
need to understand are the operations which access the data.
This makes programming conceptually easier, because a lot of the complex behaviour is hidden away. It also means that the internal
behaviour of the object can be modified without affecting the rest of the program, as long as the external interface (interface in the
sense means the objects connections with other objects) remains the same. This means that we could modify our queue object to store the
data as a linked list rather than an array. We would obviously need to change the operations of the object, but as long as they keep the
same names and arguments and return the same data, then the rest of the program will not need to be modified.
At this point in the course we shall not concern ourselves with how an object is defined internally, we shall only be interested with
it's external behaviour, which is defined by 3 factors, the information it makes available, the operations it can perform and how it
reacts to its environment. In VB terms an object
Has certain Properties , e.g. the head may be a property of the Queue object,
Can perform operations, called Methods, e.g. AddToQueue may be a method of the Queue object.
Can instruct the host program to react to Events, e.g. EmptyQueue may be an event that may fire when a Queue object becomes empty.
So to learn and understand any type of object you need to study each of its
Properties,
Methods, and
Events
The combination of Properties, Methods and Events which an object make visible to the outside world is known as its interface which is
the way in which programmers can manipulate the object. Not to be confused with a user interface which is the method by which some
objects enable end-users of your application to manipulate the object.
There are many examples of objects throughout VB, for example an individual command button is an object. A command button object can be
used when we understand its
Properties , which include the Caption
which appears on the button and its physical dimensions i.e. Height,
Width
etc.
Methods, which include Move, which moves the button, and
Events, such as, Click which fires after the user clicks the button.
Even this simple example shows the benefits of OO, we don't need to worry about how a button is implemented, how it is drawn on the
screen or how a Click event fires. All we need to know is at a much simpler abstract level e.g. where to position the button on screen.
Also because we need not understand the blood and guts of the button Microsoft can upgrade it to make it look smarter or more efficient,
as long as the external interface remains the same.
As we will see throughout this course there are many objects defined by VB. All interface controls, text boxes, buttons etc., are
objects, there are also objects which are not visible, such as the clipboard object which has properties to retrieve what is on the
windows clipboard, and methods to store data there.
As we have already stated VB is an excellent language both to learn windows programming techniques and to implement practical
business tools. It is suitable for building database applications for personal or workgroup use, as well as graphics intensive
applications, and even Internet applications. But there are situations when VB is probably not the most appropriate language, for example
if you want to do lots of very low level programming, you may be better advised to use C++.
There are even occasions when VB will not produce a satisfactory solution. For example VB applications are platform dependent, that is
they will only run under windows, so if you want to develop for the Apple Mac or Linux then you should look at a platform independent
language such as Java.
In previous versions of VB speed of the final program which was produced was a major problem. Unlike languages such as Visual C++ and
Delphi that produced compiled code, VB was interpreted to a great extent at run-time. But as we mentioned earlier this was rectified in
VB5 so that you now have the option of producing executable programs that have highly optimised native compiled code.
Advantages:
Easy to Learn
The VB language is simple and structured much like English prose making for a much shallower learning curve
Supports RAD
Large applications can be implemented in a short space of time.
Disadvantages
Slow
Older versions of VB were slow in comparisons to other languages such as
Delphi
. This was rectified in VB5.
Simplicity can be a hindrance
VB's simplicity can actually make some applications difficult to implement. In some circumstances it may be more appropriate
to choose an alternative language.
MS (98) Chapter 1 Pages 3-4
*** Jones Chapter 1 Pages 1-6
Norton Chapter 1 Pages 2-11
*** Siler
Chapter 1 Pages 12-16
Programming 10 years ago meant sitting at a terminal using an archaic text editor to bang out complex code, then using a command
line compiler that if you were lucky would build your applications. However, if you were unlucky and had bugs in your code, as usually
happened, then you were presented with error messages that were at best unhelpful and at worst gibberish. Luckily with VB Microsoft has
produced an environment, which makes the process of building apps much easier. VB provides us with an easy to use interface with
facilities to build compile and debug code in a graphical understandable way.
The VB
Integrated Development Environment
(IDE) is an extremely powerful tool, and it pays to understand it. Investing a short amount of time pays dividends because the IDE has
many laboursaving features. It has developed with each new version of VB however each of the following is present in all versions newer
that VB4:
Toolbar and Menubar window
, this contains the buttons and menu items necessary to open, save, run debug etc... your programming projects,
Toolbox Window
, this contains icons for each of the interface controls you can place on a form,
Project Window
(
Project Explorer
in VB5 and VB6), this contains a list of all the forms, modules, MDI forms and resource files in the currently open project you will
find more of what these are later,
Property Window
, this contains a list of the properties of the currently selected interface object,
Form Windows
, when you click on a form in the project window the form will appear on screen in a form window for you to edit,
Code Windows
, when you double click on an object (or select the
View Code
button the project window), a text editor appears, which enables you to enter code which responds to events.
Object Browser
, pressing the F2 key on the keyboard when designing a project causes a dialog box called the Object Browser to be displayed. This lists
all of the objects which are available in your project, from objects defined by VB such as command buttons, through to ones which you
have defined yourself.
Context Sensitive Menus
, when you right click on a control or form you will be presented with a small menu of options which are relevant to that control.
FIGURE 2, SHOWING THE IDE
The IDE can work in one of two modes Design Time (DT), which is when you build your app, and
Run Time (RT), for use when you test and debug your applications. You move from DT to RT by clicking on the Play Button on the Toolbar, by
pressing F5 or selecting Run > Start from the menu system.
2.1 Further Reading
MS (98) Chapter 2 Pages 14-17
*** Sybex Chapter 1 Pages 3-31
Jones Chapter 2 Pages 7-16
Norton Chapter 5 Pages 161-183
To learn how to build a VB application there is no better way than to roll up your sleeves and get your hands dirty. So in this
section we introduce you to the foundations of VB development, you will then finish the lecture by completing the associated exercise and
workshop sheets, which will take you step by step through the construction of you first VB app.
When building your VB app you will inevitably have to create many files, so to make the management of these files easier VB enables
you to create Project files. These are container files, given the file suffix .vbp, that point to every file your app may need. Each
project can contain any of the following:
Form Files (.frm)
These are the basic building blocks of any application. You may create a form for each of the features your app is intended to
implement. For example, a finance package may contain a form for payments and one for receipts. Forms contain all of the interface
controls necessary to achieve its aim, along with the associated VB code to make the form run. A project may contain many form files.
Module Files (.bas)
These store procedures, functions, constant and variable declarations that are available to every form in the project. Modules are
cannot contain any interface controls such as labels and textboxes because they have no user interface to draw onto. Instead they are
useful when designing well engineered software, because they enable you to share common code throughout your project. A project may
contain many module files.
MDI Parent Form Files (.frm)
These are like container windows that can hold lots of forms. For example Word 97 makes use of an MDI parent form, which contains all
of the document windows. There are several restrictions about what interface controls can be placed on an MDI parent form, and a project
may contain at most one such form.
Class Files (.cls)
As mentioned earlier VB enables you to build your own objects, class files are the construct used to achieve this. A project can contain
any number of classes.
There are other types of file, which a project can store, but only those above are used in the remainder of this course.
You will find each of the files that you use in your project listed in the project window (project explorer) to make managing projects
easier, as shown in figure 3. Also because each form, module, MDI parent form and class is saved in its own file it is possible to reuse
existing code in other projects.

FIGURE 3, SHOWING A CLOSE UP OF THE PROJECT EXPLORER
When building software, the actual implementation should form only a small part of the whole software engineering process, proper
design before and rigorous testing after are vital to the smooth construction of efficient code. However VB does aid the implementation
phase by breaking it down into a seven-stage process:
Create a new project,
Create a new form,
Add controls to the form,
Modify the properties of each control on the form,
Write the code to make the form operate,
Repeat steps 2-5 to add more forms or alternatively build MDI forms, modules or class files,
Save the project,
This takes two steps, first save the project file then save each form, module, MDI form and class,
Run and debug the project
In exercise sheet 1 we shall introduce exactly how this process is achieved, but for the rest of this lecture we are going to look at
what foundation knowledge you need before you can do this.
So you have started a new project, got your first form file, now you need to add some controls. Here we list the standard palette of
VB Controls that are found in the toolbox to the left of the screen, as shown in figure 4. Balloon help is available so that you can
hover the mouse pointer over an icon to get a brief idea of what it does, and you can get help by selecting a tool and pressing the F1
key.
Labels
are used to place text on forms. Most of their properties control the appearance of the text and where the label is to be placed on the
form. It has several events such as mouse over (when the mouse hovers over the top) click (when the user clicks on the label) and
double-click (when the user double clicks on it).
Text Boxes
are the standard control for receiving user input. Again like the label most of its properties modify the appearance of the text in the
box, but you can also set a maximum number of characters that can be entered or whether multiple lines of text can be input. It also
shares most of the same events as the label control, as well as some that are useful for data validation.
Command Buttons
, as we have already seen, enable you to place buttons on a form so that it can do certain things for example act as an OK or Cancel
button. The most useful event buttons have is the Click event which is fired when the button is pressed.
Lists and Combo Boxes
let you create a list of data items that the user can select from; there are subtle differences between the two that will be discussed
later.
Frames
are a container control, i.e. a control that can contain other controls. Frames are used to group controls together, to make a form
easier to understand.
Image Boxes and Picture Boxes
rather obviously let you place pictures onto a form, there are subtle differences between the two, mainly concerned with memory
allocation, but again we will discuss this later.
Drawing Tools
let you draw simple shapes, such as boxes, circles, lines and ellipses onto the forms.
FIGURE 4, SHOWING THE TOOLBOX
There are many other controls that come with VB as standard, and ones that can be purchased as optional extras or even
downloaded from the Internet. We will discuss a few of these in Lecture 4, but most will not be covered because once you understand how
controls in general work and how they interact with applications then it is a fairly trivial step to learn new controls.
The last piece of the puzzle which you must understand, to be able to build your first VB app is how to write the code which plugs
everything together. As was stated earlier in section 1.1 VB is an event driven language, that is you build your app by attaching code to
the events of the controls that you are interested in. The
Event Procedure
is the construct used to associate a block of code with an event. Creating event procedures is easy (!!), all you do is
1.
Select the control with which you want to associate some code,
2.
Double click on it, and
3.
A code window will be displayed with the
Procedure Stub
of the default event. To write code for a different event than the default click on the procedure drop-down list box in the top right
of the code window, you can then select the appropriate event.
A procedure stub defines the name and arguments for the event, for example
Private sub cmdClose_click()
End sub
Would be the procedure stub for the click event of a control called cmdClose. VB will always write the stub for you, and you should
leave it alone, as modifying the stub will prevent the procedure from firing when the event occurs. The part you modify is the blank line
in between the Private and the End Sub lines.
To give another example, if we wanted to add an event procedure for the Click event of the OK button in Example used in Section 1.1 we
would double click on the button, and the code window would appear as shown in figure 5.

FIGURE 5 SHOWING AN EXAMPLE CODE WINDOW
As you see a code stub has already been created by VB and between the first line and the
end sub
command, we can insert any valid VB code. In chapter 3 we shall look more in depth at the BASIC language, but for this example we shall
use some simple code. We may want to change the value of another object's properties for example,
frmName.caption = "Hello"
would display the word Hello in the title bar of frmName. Alternatively we could call a VB procedure, e.g.
MsgBox "Hello"
would create a simple dialog box as shown in figure 6.

6 SHOWING AN MSGBOX WITH CAPTION HELLO
MS (98) Chapter 2 Pages 18-29
Sybex Chapter 1 Pages 33-37
Menu: Home, Services, Events, Features, Interviews, Profiles, Reviews, News, Resources, Press