Class 1
Class 1
Class 1
Preview
Course Objectives
Visual Basic is a tool that allows you to develop Windows (Graphic User
Interface - GUI) applications. The applications have a familiar appearance to
the user. As you develop as a Visual Basic programmer, you will begin to look
at Windows applications in a different light. You will recognize and
understand how various elements of Word, Excel, Access and other
applications work. You will develop a new vocabulary to describe the
elements of Windows applications.
Visual Basic is event-driven, meaning code remains idle until called upon to
respond to some event (button pressing, menu selection, ...). 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.
Suman Das
Introduction to the Visual Basic Environment 1-3
The event-driven nature of Visual Basic makes it very easy to work with. As
you develop a Visual Basic application, event procedures can be built and
tested individually, saving development time. And, often event procedures are
similar in their coding, allowing re-use (and lots of copy and paste).
Suman Das
1-4 Learn Visual Basic 6
The original Visual Basic for DOS and Visual Basic For Windows were
introduced in 1991.
And, now Visual Basic 6 - some identified new features of Visual Basic 6:
Faster compiler
New ActiveX data control object
Allows database integration with wide variety of applications
New data report designer
New Package & Deployment Wizard
Additional internet capabilites
Applications built using Visual Basic 6 will run with Windows 95,
Windows 98, Windows 2000, or Windows NT.
Suman Das
Introduction to the Visual Basic Environment 1-5
Suman Das
1-6 Learn Visual Basic 6
These same steps are followed whether you are building a very simple
application or one involving many controls and many lines of code.
The event-driven nature of Visual Basic allows you to build your application in
stages and test it at each stage. You can build one procedure, or part of a
procedure, at a time and try it until it works as desired. This minimizes errors
and gives you, the programmer, confidence as your application takes shape.
Suman Das
Introduction to the Visual Basic Environment 1-7
Six windows appear when you start Visual Basic. Each window can be viewed
(made visible) by selecting menu options, depressing function keys or using
the toolbar. Use the method you feel most comfortable with.
The Main Window consists of the title bar, menu bar, and toolbar.
The title bar indicates the project name, the current Visual Basic
operating mode, and the current form. The menu bar has drop-down
menus from which you control the operation of the Visual Basic
environment. The toolbar has buttons that provide shortcuts to
some of the menu options. The main window also shows the
location of the current form relative to the upper left corner of the
screen (measured in twips) and the width and length of the current
form. Of particular interest is the Help menu item. The Visual Basic
on-line help system is invaluable as you build applications. Become
accustomed with its use. Usually just pressing <F1> can get you the
help you need.
Suman Das
1-8 Learn Visual Basic 6
Suman Das
Introduction to the Visual Basic Environment 1-9
Suman Das
1-10 Learn Visual Basic 6
The Project Window displays a list of all forms 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.
As mentioned, the user interface is ‘drawn’ in 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 resize it.
2. Click the tool in the toolbox, then move the mouse pointer to the form
window. The cursor changes to a crosshair. Place the crosshair 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. 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 resize a control, click the object so that it is select and sizing handles
appear. Use these handles to resize the object.
Suman Das
Introduction to the Visual Basic Environment 1-11
Example 1-1
1. Start a new project. The idea of this project is to start a timer, then stop the
timer and compute the elapsed time (in seconds).
2. Place three command buttons and six labels on the form. Move and size the
controls and form so it looks something like this:
Suman Das
1-12 Learn Visual Basic 6
Each form and control has properties assigned to it by default when you start
up a new project. There are two ways to display the properties of an object.
The first way is to click on the object (form or control) in the form window.
Then, click on the Properties Window or the Properties Window button in the
tool bar. The second way is to first click on the Properties Window. Then,
select the object from the Object box in the Properties Window. Shown is the
Properties Window for the stopwatch application:
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. A few of the prefixes are (we’ll see more as we progress
in the class):
Suman Das
Introduction to the Visual Basic Environment 1-13
You can also set or modify properties while your application is running. To do
this, you must write some code. The code format is:
ObjectName.Property = NewValue
frmStart.BackColor = vbBlue
The names you assign to controls are used by Visual Basic to set up a
framework of event-driven procedures for you to add code to. The format for
each of these subroutines (all event procedures in Visual Basic are
subroutines) is:
Visual Basic provides the Sub line with its arguments (if any) and the End Sub
statement. You provide any needed code.
Suman Das
1-14 Learn Visual Basic 6
Example 1-2
Form1:
BorderStyle 1-Fixed Single
Caption Stopwatch Application
Name frmStopWatch
Command1:
Caption &Start Timing
Name cmdStart
Command2:
Caption &End Timing
Name cmdEnd
Command3:
Caption E&xit
Name cmdExit
Label1:
Caption Start Time
Label2:
Caption End Time
Label3:
Caption Elapsed Time
Label4:
BorderStyle 1-Fixed Single
Caption [Blank]
Name lblStart
Label5:
BorderStyle 1-Fixed Single
Caption [Blank]
Name lblEnd
Suman Das
Introduction to the Visual Basic Environment 1-15
Label6:
BorderStyle 1-Fixed Single
Caption [Blank]
Name lblElapsed
Suman Das
1-16 Learn Visual Basic 6
Writing Code
The last step in building a Visual Basic application is to write code using the
BASIC language. This is the most time consuming task in any Visual Basic
application. As controls are added to a form, Visual Basic automatically builds
a framework of all event procedures. We simply add code to the event
procedures we want our application to respond to. And, if needed, we write
general procedures. For those who may have never programmed before, the
code in these procedures is simply a line by line list of instructions for the
computer to follow.
Code is placed in the code window. Learn how to access the code window
using the menu (View), toolbar, or by pressing <F7> (and there are still other
ways). At the top of the code window are two boxes, the object (or control)
list and the procedure list. Select an object and the corresponding event
procedure. A blank procedure will appear in the window where you write
BASIC code.
Suman Das
Introduction to the Visual Basic Environment 1-17
Variables
We’re now ready to attach code to our application. As controls are added to
the form, Visual Basic automatically builds a framework of all event
procedures. We simply add code to the event procedures we want our
application to respond to. But before we do this, we need to discuss
variables.
Variable Declaration
1. Default
2. Implicit
3. Explicit
If variables are not implicitly or explicitly typed, they are assigned the variant
type by default. The variant data type is a special type used by Visual Basic
that can contain numeric, string, or date data.
Suman Das
1-18 Learn Visual Basic 6
To implicitly type a variable, use the corresponding suffix shown above in the
data type table. For example,
Amount% = 300
To explicitly type a variable, you must first determine its scope. There are
four levels of scope:
Procedure level
Procedure level, static
Form and module level
Global level
Procedure level variables declared in this manner do not retain their value
once a procedure terminates.
To make a procedure level variable retain its value upon exiting the procedure,
replace the Dim keyword with Static:
Suman Das
Introduction to the Visual Basic Environment 1-19
Form (module) level variables retain their value and are available to all
procedures within that form (module). Form (module) level variables are
declared in the declarations part of the general object in the form's (module's)
code window. The Dim keyword is used:
Global level variables retain their value and are available to all procedures
within an application. Module level variables are declared in the declarations
part of the general object of a module's code window. (It is advisable to keep
all global variables in one module.) Use the Global keyword:
What happens if you declare a variable with the same name in two or more
places? More local variables shadow (are accessed in preference to) less
local variables. For example, if a variable MyInt is defined as Global in a
module and declared local in a routine MyRoutine, while in MyRoutine, the
local value of MyInt is accessed. Outside MyRoutine, the global value of MyInt
is accessed.
Suman Das
1-20 Learn Visual Basic 6
Module1
Global X as Integer
Form1 Form2
Dim Y as Integer Dim Z as Single
Sub Routine2()
Static B as Double
.
.
End Sub
Suman Das
Introduction to the Visual Basic Environment 1-21
Example 1-3
All that’s left to do is attach code to the application. We write code for every
event a response is needed for. In this application, there are three such events:
clicking on each of the command buttons.
1. Double-click anywhere on the form to open the code window. Or, select ‘View
Code’ from the project window.
2. Click the down arrow in the Object box and select the object named (general).
The Procedure box will show (declarations). Here, you declare three form
level variables:
Option Explicit
Dim StartTime As Variant
Dim EndTime As Variant
Dim ElapsedTime As Variant
The Option Explicit statement forces us to declare all variables. The other
lines establish StartTime, EndTime, and ElapsedTime as variables global
within the form.
3. Select the cmdStart object in the Object box. If the procedure that appears is
not the Click procedure, choose Click from the procedure box. Type the
following code which begins the timing procedure. Note the Sub and End Sub
statements are provided for you:
In this procedure, once the Start Timing button is clicked, we read the current
time and print it in a label box. We also blank out the other label boxes. In the
code above (and in all code in these notes), any line beginning with a single
quote (‘) is a comment. You decide whether you want to type these lines or
not. They are not needed for proper application operation.
Suman Das
1-22 Learn Visual Basic 6
Here, when the End Timing button is clicked, we read the current time (End
Time), compute the elapsed time, and put both values in their corresponding
label boxes.
This routine simply ends the application once the Exit button is clicked.
6. Did you notice that as you typed in the code, Visual Basic does automatic
syntax checking on what you type (if you made any mistakes, that is)?
7. Run your application by clicking the Run button on the toolbar, or by pressing
<f5>. Pretty easy, wasn’t it?
8. Save your application - see the Primer on the next page. Use the Save
Project As option under the File menu. Make sure you save both the form
and the project files. This is saved as Example1-3 in LearnVB6\VB
Code\Class 1 folder.
Suman Das
Introduction to the Visual Basic Environment 1-23
9. If you have the time, some other things you may try with the Stopwatch
Application:
A. Try changing the form color and the fonts used in the label boxes
and command buttons.
B. Notice you can press the ‘End Timing’ button before the ‘Start
Timing’ button. This shouldn’t be so. Change the application so you
can’t do this. And make it such that you can’t press the ‘Start
Timing’ until ‘End Timing’ has been pressed. Hint: Look at the
command button Enabled property.
C. Can you think of how you can continuously display the ‘End Time’
and ‘Elapsed Time’? This is a little tricky because of the event-
driven nature of Visual Basic. Look at the Timer tool. See the
exercise at the end of the class for help on this one.
Suman Das
1-24 Learn Visual Basic 6
When saving Visual Basic applications, you need to be concerned with saving
both the forms (.FRM) and modules (.BAS) and the project file (.VBP). In either
case, make sure you are saving in the desired directory. The current directory is
always displayed in the Save window. Use standard Windows techniques to
change the current directory.
The easiest way to save a new project is to click the Save Project button (it looks
like a floppy disk) on the Visual Basic toolbar. First, you will be asked where you
want to save your forms and modules, then where you want to save your project
file. Once you’ve done this, subsequent clicking on the Save Project toolbar
button will automatically save your forms, modules, and project file in their
specified locations. To open a saved project, simply click the Open Project
button (looks like a file folder).
If your prefer to save without the toolbar, there are four Save commands available
under the File menu in Visual Basic:
Save [Form Name] Save the currently selected form or module with the
current name. The selected file is identified in the
Project window.
Save [Form Name] As Like Save File, however you have the option to change
the file name
Save Project Saves all forms and modules in the current project
using their current names and also saves the project
file.
Save Project As Like Save Project, however you have the option to
change file names. When you choose this option, if you
have not saved your forms or modules, you will also be
prompted to save those files. I always use this for new
projects.
There is a corresponding Open command under the File menu to open project
files.
Suman Das
Introduction to the Visual Basic Environment 1-25
Class Review
Suman Das
1-26 Learn Visual Basic 6
Practice Problems 1*
Problem 1-3. Enabled Problem. Build an application with two command buttons.
When you click one button, make it disabled (Enabled = False) and make the
other button enabled (Enabled = True).
Problem 1-4. Date Problem. Build an application with a command button. When
the button is clicked, have the computer display the current date in a label control.
*Note: Practice Problems are given after each class to give you practice in
writing code for your Visual Basic applications. These are meant to be quick and,
hopefully, short exercises. The Visual Basic environment makes it easy to build
and test quick applications – in fact, programmers develop such examples all the
time to test some idea they might have. Use your imagination in working the
problems – modify them in any way you want. You learn programming by doing
programming! The more you program, the better programmer you will become.
Our solutions to the Practice Problems are provided as a separate chapter to
these notes.
Suman Das
Introduction to the Visual Basic Environment 1-27
Exercise 1*
Calendar/Time Display
Design a window that displays the current month, day, and year. Also, display the
current time, updating it every second (look into the Timer control). Make the
window look something like a calendar page. Play with object properties to make
it pretty.
Suman Das
1-28 Learn Visual Basic 6
Suman Das