Visual Basic Notes
Visual Basic Notes
VIJAYA COLLEGE
Page 1
VISUAL BASIC
The Visual Basic 6 Integrated Development Environment
On start up, Visual Basic 6.0 will display the following dialog box as
shown in Figure 1.1. You can choose to start a new project, open an
existing project or select a list of recently opened programs. A project
is a collection of files that make up your application. There are various
types of applications that we could create, however, we shall
concentrate on creating Standard EXE programs (EXE means
executable program). Now, click on the Standard EXE icon to go into
the actual Visual Basic 6 programming environment.
VIJAYA COLLEGE
Page 2
VISUAL BASIC
Building Visual Basic Applications
First of all, you have to launch Microsoft Visual Basic 6. Normally, a
default form with the name Form1 will be available for you to start
your new project. Now, double click on Form1, the source code window
for Form1 as shown in figure 2.1 will appear. The top of the source
code window consists of a list of objects and their associated events or
procedures. In figure 2.1, the object displayed is Form and the
associated procedure is Load.
When you click on the object box, the drop-down list will display a list
of objects you have inserted into your form as shown in figure 2.2.
Here, you can see a form with the name Form1, a command button
with the name Command1, a Label with the name Label1 and a Picture
Box with the name Picture1. Similarly, when you click on the
procedure box, a list of procedures associated with the object will be
VIJAYA COLLEGE
Page 3
VISUAL BASIC
displayed as shown in figure 2.3. Some of the procedures associated
with the object Form1 are Activate, Click, DblClick (which means
Double-Click) , DragDrop, keyPress and more. Each object has its
own set of procedures. You can always select an object and write
codes for any of its procedure in order to perform certain tasks.
Figure 2.2: List of Objects
You do not have to worry about the beginning and the end statements
(i.e. Private Sub Form_Load.......End Sub.); Just key in the lines in
between the above two statements exactly as are shown here. When
you press F5 to run the program, you will be surprise that nothing
shown up .In order to display the output of the program, you have to
add the Form1.show statement like in Example 2.1.1 or you can just
use Form_Activate ( ) event procedure as shown in example 2.1.2.
The command Print does not mean printing using a printer but it
means displaying the output on the computer screen. Now, press F5 or
click on the run button to run the program and you will get the output
as shown in figure 2.4.
VIJAYA COLLEGE
Page 4
VISUAL BASIC
You
can
also
perform
arithmetic
calculations
as
shown
in
VIJAYA COLLEGE
Page 5
VISUAL BASIC
Example 2.1.1
Private Sub Form_Load ( )
Form1.show
Print Welcome to Visual
Basic tutorial
End Sub
Example 2.1.2
Private Sub Form_Activate ( )
20
10
20
10
20
10
Print 20 / 10
End Sub
VIJAYA COLLEGE
Page 6
VISUAL BASIC
VIJAYA COLLEGE
Page 7
VISUAL BASIC
Figure 3.1 on
the right is a
typical
properties
window for a
form. You can
rename
the
form caption
to any name
that you like
best. In the
properties
window,
the
item appears
at the top part
is the object
currently
selected
(in
Figure 3.1, the
object
selected
is
Form1). At the
bottom
part,
the
items
listed in the
left
column
represent the
names
of
various
properties
associated
with
the
selected
object
while
the
items
listed in the
right
column
represent the
states of the
properties.
Properties can
be
set
by
highlighting
the items in
the
right
column
then
change them
by typing or
selecting the
options
VIJAYA COLLEGE
Page 8
VISUAL BASIC
available.
add
more
controls
as
you
progress
to
more
advanced
programming.
VIJAYA COLLEGE
Page 9
VISUAL BASIC
VIJAYA COLLEGE
Page 10
VISUAL BASIC
Example 21d1: Query based on Author
Run the program and key in the following SQL query statement
SELECT Title, Author FROM book WHERE Author='Liew Voon Kiong'
Where you click on the query button, the DataGrid will display the author name Liew
Voon Kiong. as shown below:
You may also search for data that contain certain characters by pattern matching. It
involves using the Like operator and the % symbol. For example, if you want to
search for a author name that begins with alphabet J, you can use the following
query statement
SELECT * FROM book WHERE Author Like 'J%'
VIJAYA COLLEGE
Page 96
VISUAL BASIC
Where you click on the query command button, the records where authors' name
start with the alphabet J will be displayed, as shown below:
Next, if you wish to rank order the data, either in ascending or descending order,
you can use the ORDER By , ASC (for ascending) and DESC(Descending) SQL
keywords.
The general formats are
SELECT fieldname1, fieldname2.....FROM table ORDER
BY fieldname ASC
SELECT fieldname1, fieldname2.....FROM table ORDER
BY fieldname DESC
Example 21d3:
The following query statement will rank the records according to Author in ascending
order.
SELECT Title, Author FROM book ORDER BY Author ASC
VIJAYA COLLEGE
Page 97
VISUAL BASIC
VIJAYA COLLEGE
Page 98
VISUAL BASIC
Lesson 29: Creating Advanced VB database
application using ADO control
In previous lessons, you have learned how to design database applications using
data control and ADO control. However, those applications are very simple and plain
. In this lesson, you will learn how to create a more advanced database application
using ADO control. The application you are going to create is known as an electronic
library. This electronic library will be able to accept the user registration as well as
handling login command that require the user's password, thus enhancing the
security aspect of the database. Basically, the application will constitute a welcome
menu, a registration menu, a Login menu and the main database menu. The
sequence of the menus are illustrated as follow:
VIJAYA COLLEGE
Page 99
VISUAL BASIC
In this form, you need to insert three command buttons and set their properties as
follow:
Form name
command button
Name
command button
Caption
command button
Name
command button
Caption
command button
Name
command button
Caption
main_menu
1
1
2
2
3
3
cmdRegister
Register
cmdLogin
Login
cmdCancel
Cancel
VIJAYA COLLEGE
Page 100
VISUAL BASIC
main_menu.Hide
Register.Show
End Sub
This registration forms consist of two text boxes , three command buttons and an
ADO control. Their properties are set as follow:
Form name
textbox 1 name
textbox 2 name
textbox 2
PasswordChar
command button
name
command button
Caption
command button
name
command button
Caption
command button
name
Register
txtName
txtpassword
*
1
1
2
2
3
VIJAYA COLLEGE
cmdConfirm
Confirm
cmdClear
Clear
cmdCancel
Page 101
VISUAL BASIC
command button 3
Cancel
Caption
ADO control name UserInfo
note that the PasswordChar of textbox 2 is set as * which means users will not be
able to see the actual characters they enter, they will only see the * symbol.
The codes are as follow:
VIJAYA COLLEGE
Page 102
VISUAL BASIC
There are two text boxes and a command button, their properties are set as follow:
Textbox 1 name
txtName
Textbox 2 name
txtpassword
Command button 1
cmdLogin
name
Command button 1
Login
Caption
Form name
Login_form
The codes are as follow:
Private Sub cmdLogin_Click()
Dim usrname As String
Dim psword As String
Dim usernam As String
Dim pssword As String
Dim Msg As String
Register.UserInfo.Refresh
usrname = txtName.Text
psword = txtpassword.Text
Do Until Register.UserInfo.Recordset.EOF
If Register.UserInfo.Recordset.Fields("username").Value = usrname And
Register.UserInfo.Recordset.Fields("password").Value = psword Then
Login_form.Hide
VIJAYA COLLEGE
Page 103
VISUAL BASIC
frmLibrary.Show
Exit Sub
Else
Register.UserInfo.Recordset.MoveNext
End If
Loop
Msg = MsgBox("Invalid password, try again!", vbOKCancel)
If (Msg = 1) Then
Login_form.Show
txtName.Text = ""
txtpassword = ""
Else
End
End If
End Sub
VIJAYA COLLEGE
Page 104
VISUAL BASIC
Form name
frmLibrary
ADO control name adoLibrary
ADO visible
False
TextBox 1 name
txtTitleA
TextBox 2 name
txtAuthor
TextBox 3name
txtPublisher
TextBox 4 name
txtYear
TextBox 5 name
txtCategory
Command button 1
cmdSave
name
Command button 1
&Save
caption
Command button 2
cmdNew
name
Command button 2
&New
caption
Command button 3
cmdDelete
name
Command button 3
&Delete
caption
Command button 4
cmdCancel
name
Command button 4 &Cancel
VIJAYA COLLEGE
Page 105
VISUAL BASIC
caption
Command
name
Command
caption
Command
name
Command
caption
Command
name
Command
caption
button 5
button 5
button 6
button 6
button 7
button 7
cmdNext
N&ext
cmdPrevious
&Previous
cmdExit
E&xit
VIJAYA COLLEGE
Page 106
VISUAL BASIC
Private Sub cmdNew_Click()
adoLibrary.Recordset.AddNew
End Sub
Private Sub cmdNext_Click()
If Not adoLibrary.Recordset.EOF Then
adoLibrary.Recordset.MoveNext
If adoLibrary.Recordset.EOF Then
adoLibrary.Recordset.MovePrevious
End If
End If
End Sub
Private Sub cmdPrevious_Click()
If Not adoLibrary.Recordset.BOF Then
adoLibrary.Recordset.MovePrevious
If adoLibrary.Recordset.BOF Then
adoLibrary.Recordset.MoveNext
End If
End If
End Sub
Private Sub cmdSave_Click()
adoLibrary.Recordset.Fields("Title").Value = txtTitle.Text
adoLibrary.Recordset.Fields("Author").Value = txtAuthor.Text
adoLibrary.Recordset.Update
End Sub
VIJAYA COLLEGE
Page 107
VISUAL BASIC
Lesson 30: Animation-Part I
Animation is always an interesting and exciting part of programming. Although visual
basic is not designed to handle advance animations, you can still create some
interesting animated effects if you put in some hard thinking. There are many ways
to create animated effects in VB6, but for a start we will focus on some easy
methods.
The simplest way to create animation is to set the VISIBLE property of a group of
images or pictures or texts and labels to true or false by triggering a set of events
such as clicking a button. Let's examine the following example:
This is a program that create the illusion of moving the jet plane in four directions,
North, South ,East, West. In order to do this, insert five images of the same picture
into the form. Set the visible property of the image in the center to be true while the
rest set to false. On start-up, a user will only be able to see the image in the center.
Next, insert four command buttons into the form and change the labels to Move
North, Move East, Move West and Move South respectively. Double click on the move
north button and key in the following procedure:
Sub Command1_click( )
Image1.Visible
Image3.Visible
Image2.Visible
Image4.Visible
Image5.Visible
=
=
=
=
=
False
True
False
False
False
End Sub
By clicking on the move north button, only image 3 is displayed. This will give an
illusion that the jet plane has moved north. Key in similar procedures by double
clicking other command buttons. You can also insert an addition command button
and label it as Reset and key in the following codes:
Image1.Visible
Image3.Visible
Image2.Visible
Image4.Visible
Image5.Visible
=
=
=
=
=
True
False
False
False
False
Clicking on the reset button will make the image in the center visible again while
other images become invisible, this will give the false impression that the jet plane
has move back to the original position.
VIJAYA COLLEGE
Page 108
VISUAL BASIC
VIJAYA COLLEGE
Page 109
VISUAL BASIC
You can also issue the commands using a textbox, this idea actually came from my
son Liew Xun (10 years old). His program is shown below:
Private Sub Command1_Click()
End Sub
VIJAYA COLLEGE
Page 110
VISUAL BASIC
Another simple way to simulate animation in VB6 is by using the Left and Top
properties of an object. Image.Left give the distance of the image in twips from the
left border of the screen, and Image.Top give the distance of the image in twips from
the top border of the screen, where 1 twip is equivalent to 1/1440 inch. Using a
statement such as Image.Left-100 will move the image 100 twips to the left,
Image.Left+100 will move the image 100 twip away from the left(or 100 twips to the
right), Image.Top-100 will move the image 100 twips to the top and Image.Top+100
will move the image 100 twips away from the top border (or 100 twips down).Below
is a program that can move an object up, down. left, and right every time you click
on a relevant command button.
The Code
Private Sub Command1_Click()
Image1.Top = Image1.Top + 100
End Sub
Private Sub Command2_Click()
Image1.Top = Image1.Top - 100
End Sub
Private Sub Command3_Click()
Image1.Left = Image1.Left + 100
End Sub
Private Sub Command4_Click()
Image1.Left = Image1.Left - 100
End Sub
VIJAYA COLLEGE
Page 111
VISUAL BASIC
The fourth example let user magnify and diminish an object by changing the height
and width properties of an object. It is quite similar to the previous example. The
statements Image1.Height = Image1.Height + 100 and Image1.Width =
Image1.Width + 100 will increase the height and the width of an object by 100 twips
each time a user click on the relevant command button. On the other hand, The
statements Image1.Height = Image1.Height - 100 and Image1.Width =
Image1.Width -100 will decrease the height and the width of an object by 100 twips
each time a user click on the relevant command button
The Code
Private Sub Command1_Click()
Image1.Height = Image1.Height + 100
Image1.Width = Image1.Width + 100
End Sub
Private Sub Command2_Click()
Image1.Height = Image1.Height - 100
Image1.Width = Image1.Width - 100
End Sub
You can try to combine both programs above and make an object move and
increases or decreases in size each time a user click a command button.
VIJAYA COLLEGE
Page 112
VISUAL BASIC
Lesson 32: Animation - Part III
32.1 Animation using Timer
All preceding examples of animation that you have learn in lesson 23 and lesson 24
only involve manual animation, which means you need to keep on clicking a certain
command button or pressing a key to make an object animate. In order to make it
move automatically, you need to use a timer. The first step in creating automatic
animation is to drag the timer from the toolbox into the form and set its interval to a
certain value other than 0. A value of 1 is 1 milliseconds which means a value of
1000 represents 1 second. The value of the timer interval will determine the speed
on an animation.
In the following example, I use a very simple technique to show animation by using
the properties Visible=False and Visible=true to show and hide two images
alternately. When you click on the program, you should see the following animation.
The Code
Private Sub
Timer1_Timer()
If Image1.Visible
= True Then
Image1.Visible =
False
Image2.Visible =
True
ElseIf
Image2.Visible =
True Then
Image2.Visible =
False
Image1.Visible =
True
End If
End Sub
Next example shows a complete cycle of a motion such as the butterfly flapping its
wing. Previous examples show only manual animation while this example will display
an automatic animation once you start the program or by clicking a command
button. Similar to the example under lesson 24.2, you need to insert a group of eight
images of a butterfly flapping its wings at different stages. Next, insert a timer into
the form and set the interval to 10 or any value you like. Remember to make image1
visible while other images invisible at start-up. Finally, insert a command button,
rename its caption
clicking on this button. Bear in mind that you should enter the statements for hiding
VIJAYA COLLEGE
Page 113
VISUAL BASIC
and showing the images under the timer1_timer subroutine otherwise the animation
would work. Clicking on the animate button make timer start ticking and the event
will run after every interval of 10 milliseconds or whatever interval you have set at
design time. In future lesson, I will show you how to adjust the interval at runtime
by using a slider bar or a scroll bar. When you run the program, you should see the
following animation:
VIJAYA COLLEGE
True Then
True Then
True Then
True Then
Page 114
VISUAL BASIC
Image6.Visible = False
Image7.Visible = True
ElseIf Image7.Visible = True Then
Image7.Visible = False
Image8.Visible = True
ElseIf Image8.Visible = True Then
Image8.Visible = False
Image1.Visible = True
End If
End Sub
VIJAYA COLLEGE
Page 115