Visual Basic Notes
Visual Basic Notes
Most people think that computer CPU is a very intelligent thing, which
in actual fact it is a dumb and inanimate object that can do nothing
without human assistant. The microchips of a CPU can only
understand two distinct electrical states, namely, the on and off states,
or 0 and 1 codes in the binary system. So, the CPU only understands
combinations of 0 and 1 code, a language which we called machine
language. Machine language is extremely difficult to learn and it is not
for us laymen to master it easily. Fortunately, we have many smart
programmers who wrote interpreters and compilers that can translate
human language-like programs such as BASIC into machine language
so that the computer can carry out the instructions entered by the
users. Machine language is known as the primitive language while
Interpreters and compilers like Visual Basic are called high-level
language. Some of the high level programming languages beside
Visual Basic are Fortran, Cobol, Java, C, C++, Turbo Pascal, and more
. Among the aforementioned programming languages, Visual Basic is
the most popular. Not only it is easily to learn because of its English-
like syntaxes, it can also be incorporated into all the Microsoft office
applications such as Microsoft words, Microsoft Excel, Microsoft
PowerPoint and more. Visual Basic for applications is known as VBA.
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.
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
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.
Example 2.1.1
Form1.show
End Sub
Example 2.1.2
Print 20 + 10
Print 20 - 10
Print 20 * 10
Print 20 / 10
End Sub
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
available.
You can also change the properties at runtime to give special effects
such as change of color, shape, animation effect and so on. For
example the following code will change the form color to red every
time the form is loaded. VB uses hexadecimal system to represent the
color. You can check the color codes in the properties windows which
are showed up under ForeColor and BackColor .
Form1.Show
Form1.BackColor = &H000000FF&
End Sub
Shape1.Shape = 3
End Sub
The text box is the standard control for accepting input from the user
as well as to display the output. It can handle string (text) and
numeric data but not images or pictures. String in a text box can be
converted to a numeric data by using the function Val(text). The
following example illustrates a simple program that processes the
input from the user.
Example 3.1
In this program, two text boxes are inserted into the form together
with a few labels. The two text boxes are used to accept inputs from
the user and one of the labels will be used to display the sum of two
numbers that are entered into the two text boxes. Besides, a
command button is also programmed to calculate the sum of the two
numbers using the plus operator. The program use creates a variable
sum to accept the summation of values from text box 1 and text box
% )$ (?$ 56
G ( 7
) E 5 ( 6. 5 7 6
G ! (
8 ($ E )
0 )
! ,",
,"("( &
! !
! 3 $
& ! & "$ !
1
% 0 :(( * :
,"(", $
, !
$ !
% )$ (?$ 56
0 )
,"("5 ’ 4
%
1 !
1 ’
*
% ( % E8 % 5H$IJ J J, H6
,"("/ 4
, ,
! 9
" ,
%
) % 8 %
*
, ( % E8 % 5H$IJ J, J H6
,"(" 4
8
,
# * !
( ! !
4 ,"(
% )* ?8 56
8 ( , @8 (A
8 ( , @8 7A
8 ( , @8 :A
8 ( , @8 >A
0 )
4 ! !
8 , ’
8 , ( 8 , 7
,"(": $ & 4
$
9
, +
! # *
! ( ! $
!
4 ,",
% )* ?8 56
$ ( , @, (A
$ ( , @, 7A
$ ( , @, :A
$ ( , @, >A
0 )
,"("; $ % 4
$
$ (
’ 1
$ ( E( $ $ E’ (
$
*
1
, K K 0 L
$ !
4 ,"5
% ) $ (?$ 56
, $ ( E ( $ 7 E ’
# H H
0 , $ 7 E ( $ ( E ’
# H3 H
0
# H H
0 ,
0 )
,"("< * 4
3
9 3
3 3
, ! 3
@ A
< @* A ,
3
7 : > = ’ (
M
M !
4 ,"/
% )3 (?$ 56
) ( ) E’
0 )
% )3 7?$ 56
) () E (
0 )
% )3 :?$ 56
) () E 7
0 )
% 3) >?$ 56
) ( ) E:
0 )
% 3) =?$ 56
) ( ) E >
0 )
% )3 ?$ 56
) ( ) E =
0 )
,"(" = 4
; 8 ! !
!
! !
* :>
,"(" 4
; ! 8 !
!
!
! : = *
,"(" ( ! 4
* 8 ! !
!
!
! * :=
5 $
, 7 !
/ N
0 " ! !
<
! ! +"
N 5 * 7 :6
!
! " *!
! +
" * !
! +
* 7 ( ,
I
5O ! ! 6
1 !+ %
) $ (?$ 0 ) ! )
! N
0
! ! !
! ! "
I
*&+ "’
3 " % ! 5 ! 6 *
! "-
* ( & " & > (
4 " 4 >? ?
( 7 E(’’ (’’
7" & >!
( 8 N
I
4 5"
% )$ (?
8 ( E
% )$ (?
8 7 E
8 ( $ EA A
( EA1 2A
, ( E
0
0
% )$ (?
% ( ) E
( 0 E
8 ( $ EA) $
0
, 0 > ( (
7 A<1
A ( , > 7
@ ( A
, ( , >:
% (
( @) $ A
)! 0 +
’ @ )A A" A" )@
! A8 4 @ - $ A"" - @ 4 - &" *
’ B ? !
3
% !! !
! 9
!
!!
C 6 "*
2 3
4 D M -E 041"
! - @
$ @ @ ! (’
- F
) P !
5 6 P )
! !
8 N >>
>=
4 5"/
% )* ? 56 % )* ? 56
: E ( . 7 : E 5 ( 6. 5 7 6
0 ) 0 )
! > 7> (( :
7 ! (7: ,
" " 3
> = ! (=
/ G
! ! ! *
!
M !! ! )
!
!
/"
"
! < ! !+
!
& /" 8
- 7 )
! ( ! ’ 7==
, 7 ! +:7 Q R :7 Q Q
8 > ! +7 (>Q >R: >R 7 (>Q >R: >R
/" "( 8 6
/ !
! +
! ! ;
! ! 65 3 "
! ! ! = 7
& /"( 8
- 7
) 5 8 6 ( = >’’
) 5 8 6 . (’ ! ’ 7
; R ! - ! ( (’’ ; :( SSSS
7 ! *
5 6( ! ! ;
5 6 8 .77 ! ) +
" ", - )) 4 )
8 ! ,
! * E( ;:’RST !
) :! =
& /",
- )) 4
F 8
2 )
T ;
U $ !
, M
T ) !
I
/ EH - H
/ EH(R’’+S’’+RRR+QQQH
8 ; !ET:(+; +’’T
0 ET(7I’’ T
! " " # $ %
! ! !
*
! M ! ! )
!
!
/"
" ! !
! ! +
/" " 8
/
!
!
Q !
,
8
%
M
)
;
! !
*
!
!
8 !
!
!
=(
& /" 8
- 7 )
! ( ! ’ 7==
, 7 ! +:7 Q R :7 Q
8 > ! +7 (>Q >R: >R 7 (>Q >R: >R
+: >’7R7:0.:R +( >’(7SR0+ >=
) > !
( >’(7SR0+ >= : >’7R7:0.:R
/" "( 8 6
/ !
! +
!
! ! 5!
6 3 " ! !
= 7
& /"( 8
- 7
(
) 5 8
= >’’
6
’
) 5 8
6 (’ !
- !
( (’’
; R !
;
:( SSSS
7 !
*
!
3 " > !
"
!
5 ( 6!
;
)
8 .77 +
5 6
!
/" ", - )) 4 )
8 ! ,
! * T
; ! ) =! :
& /",
- )) 4
F 8
2 )
T ;
!
U $
, M
T
!
I
/ EH -
/ EH(R’’+S’’+RRR+QQQH H
8 ; !ET:(+; +’’T
0 ET(7I’’ T
/"( G &
! " ,
! !
8
!
/"(" & 8
• , 7==
• /
• ,
• %
0 ! =>
& /"5
8 8
#!?$ #! $
1 (/ !
9 F9 * BF
8 ?/ ?$ ? &)0
/"("( &
,
! ! !!
D
I
0 =(
; )
; ! / )
; ,
; ,
; ,
; ; ;
1 !
I
; ) ! / )
,
, ! !
*
+ * +
+ " =(
9 + !
I
0 = 7I
; ! / ) B (’
! / (’ $
/", $
/"," $
0 = :
$ % ) E: (>7
$ ) E:Q
$ ) ) E(’’
8 I
" # &
;
E0
!
5 6
I
/ E(’’
/ E / +
SS
/ EH- 8!
E
8 ( E
$ ( E
8 >$
(
/
5 ( 6
E /
/ . /
"( *
, . +
!
(
& " # *
3 # 0
V 0 7V>E(
C ; (7C>E:
# 5
# (= # >E: 7== (’E=
6
J , ; 5 (SJ>E> 6
H HFH HEH
. F )
H
4 " 4 "(
; / ; ( 7 : ,
) ;
; / % * ?$
) (E 5 ( 6
; ! / 7E 5 7 6
) :E 5 : 6
E (. 7. :
% E C=
$ (?$ 56 8 ( $ E
/ 8 7 $ E
( 0 )
/
7 ,
! /
/ . HH ! !
/
8 ( $
E ! /
0 )
,
*
/
/
N
(
7
!
*
! /
! 8 (
: $ ’ !
:" $ *
! !
$ ! !
Q (
:"( *
Q7
E 0M
#
W
8 3
X
#
WE
M 3
8
XE 4
M
/
XW
0M /
/
B 1 9
I &
H HXH HXH$HXH;H XHYH
:", H
0 I
)""""" """""
- %
* 3O?$ 56
!
5 ( 6
)""" """
5 7 6
E 5
, E
)""" """
5 6XW ’
)
E
0
)
E
B ! 0 0 ,
0 , )
0 0 )
; - $ """" $ -
,
)""" ) , !
! -
$ 9 ) $
! , 0 ,
) $ ! !
5 6
, 0 , ! !
, 0 , ! !
) $ !
, 0 ,
!
) $ I
) $
$ (
$ 7
)
$ :
$ 0
)
4 ;"
; )
% ) $ ?$ 56
) $
$ HH
$ EH9 ; H
$ H +H
$ EH; H
$ HH
$ EH$ H
$ H$H
$ EH% H
$ 0
$ EH* H
0 )
0 )
<
<"
6 ;
6 ;
6 ; &
6 ;
8 &
<"( 4
) !
! 0 ; 1
0 S7
<", ! """"8 4
* E / / 5) 6
3
/
% S : S : S:
)
4 !
* K / 8 ! 4 !
< ! , K K
8 N S:
4 <"(
; ,
% ) * ? 56
8 ( , H H F F H H
;
E . (
) E ) .
8 ( , F F)
, E (’’
0 ;
0 ,
8
0 )
"#"$"%"&&"!’’( !
) ) * + )
) * !( ) ,))
* + ( * !(,)) - .- -. - )
) * +
) ) (
4 <", 4 <", &
* E( * E(
(’’’ (’
! E E .(
/ /
4 <", 4 <",
* E(
= += %
E * ?+ 5 6
(’ * E( (’
/ , W
B/ 0 *
0 ,
0
%
0 ,
0 )
& ’ #$ $ ( )
! +
5 6 !
! 8 0 1
, !
G 40 1 40 1
=" G 401 !
" # +
C I
G >G 40’ @ - @ 1
% ! ) !
!
(’ ( ! !
!
& =" -
-
8 $
’ 33 ! 3
3
( 3$ $
L !
7 L !,
,
1 /
: 1 /$
$
1
> 1 / /
L !
= L !$ $
,
! !
0 I ! # E# 5 H$ 3O H ( % H)
# H6
! # E# 5H$ 3O %
3$ H) # H6
! # !
# 5 6 ! !
! , , !
& ="(
& ="( 7 $
8
$ $ %
3
( 3
$
7 $
L
> L !
,
= ,
1
1
/
Q /
4 =" I
, I
1 % ) ?$ 56
* (’ ( ; ,
E # 5H$
H
! ="
, E (
; ! $ E H )
0
; ! $ E H
0 ,
0 )
(’ 7
3O
H !
C $
H H
!
! ="(
4 ="(
!
! 1
(’ : (’ ( !
I
0 )
,
! I
! =",
="( 40 1 !
, 5 6
!
G > 40’
@ ) . 4 @ 46
6 1
!#
! !
• % +
! !
M
• +
,
• + +
!
!
• + !+ +
G !
!
!
!
7 @ -E @
@ #& @ 4 @ @ - @ $ @ @
7 # "@ ! 4
0 1 7 !
! L ’ (
, 0 (( ( ! !
(’ ’ ( L
4 "
% ) * ?
L
* E( (’
% L
/
L !
*
(
07 2 1C 8 N
! I
, , 5L B 6 ’
=
( !
( ! !
* ! ’ SR
! ! = RR
, 5= RR6 =< (!
, !
G N 1
( < !
( $ E/ 8
!
4 "(
;
% ) $ (?$ 56
L
/ E, 5L B 6.(
8 ( $ E/
0 )
8 !
-E #& 4 ! 4 7 "
6 !
, *57 >6E7
, 5> R6E> , 5+> 6E += , 5’ ’:76E’
6 -E M *
)M 5>6E7 )M 5S6E7
6 #& )
5+R6 E R 5R6E R
(
6 4 * 0 5E(6E
7 Q(R7R(R7R>=S’
6!4
9
* 5+
* :>6E +
, 5+ :>6E+Q
67
* L 5 6
* 7= Q 7L6EQ 7 5Q
6 8
*
8 (’E 7 :’7=R=
4 ",
, 5 L6 * 5566
, ; 8 L
(’ E LQ Q6 5L B
’ Q Q &
% ) * ? 56
E(
% H H H H H, 5 6H H*>6H 5 6H HL 5
; X ((
L
E L 5L B Q Q6
% , 5 6 * 5 6 L 5 >6
E .(
0 )
- @ $ @ # "
6 )
6 $
! *
\ M’]<
(R
( M \ ! (R’
! !
5\C(R’6 9
! \ !
* (
>=] \C> ) \ "
! ( > 8 N
I
4 "5
, !
: ’] ’]
\ E>B 5(M
6
! !
5\C(R’6 ; * /
8 *
(7 >
% ) * ? 56
E >B 5(6
% H H H) H H$ H H H
* E ’ : ’ ) :’
E B 5 C (R’6
% L 5) 5 6 >6 L6 5>$6 5
L 5 5 6 >6
0 )
) 4 5
( ! !
* !
! ! !;
!! !
,L *
9
&-
!
0 1 &)
& 0 1I 4
! ! 5! !
!6
% ) * ?
0 1 - )
- ! !
9 56
)
*
4 (
% ) * ? 56
% H H< ) 5(’6< H H
0 )
# ! (’
0 1 ! )
! !
! !
* +
!
5 6 *
! 0 @ B ?1
- 4 4
^ !
/
* 5RSQ7 7:> @^
/ A6ERSQ7 7:>
* !
* 5RSQ7 7 @* A6ERSQ7 7:
) !
* 5 >RSQ7 7 = @) A6E
>R SQ7 7Q
$ ! !
* 5 >RSQ7 7 = @$ !A6E
P >R SQ7 7Q
% $
! _ * 5’ = :7> @% A6E= :7 _
4 ,
% ) * ? 56
% * 5RSQ7 7:> H^ / H6
% * 5RSQ7 7 H* H6
% * 5 >RSQ7 7 = H) H6
% * 5 >RSQ7 7 = H$ !H6
% * 5’ = :7> H% H6
0 )
/ ! I
, - G !
)
7 @ ) @ G @
@ 7 @ H
@ @ @ -
@$ # "0 1
!
0B’ ?1
8
5 6
E (( 8
5
77
%
* ?
56
4E M 5( 6
1E(7:>
YTE(’T
% 8
8 5!6
8 56
0 )
( > R ! R T
!
0 1 7 !
7 0B’ ?@ 1
L 5@ A >6 E
0 1 ) !
8 P
) 0B’ ?@ 1
8 5@ A >6 E
0 1 !
8 !
0B’ ?1
8 5@ A >6E
0 1 7 !
L !
7 0B’ ?1
L 5@ A >6 E
0 1 )
!
0B’ ?1
5@ A6 E
0 1 G !
G ,
I
G 0 @ @ 1
# 5@ A : 6 E
0 41 - )
0 @ @ & 1
,
*
, 5( @ A A A6ER
041 H )
H 3
& 5@ A6 E ,)& 8 )$
8 5@ A6 E
04 1 - )
-
04 1 $ # )
$ )$,, #
)$,, ! )$,,
)$,, @ ) $ ,, A
7== )$,, ! )$,, )
! ! ! !
! $
$ 0 1
# 0$ 1
5 $ H 6 ) !
5" $ J * !
’& ! ) 8 0# # @""""""""""1 #
’ ! ) 8 0# # @""""""""""1 #
B % "
% !
4 5"
, !
! !
!
% * * 5% 6
D* * 5* 6
D% %
* E% B 5( . C (’’6 V
0 *
% ) ?$ 56
D *
; *
; %
;
;
% E%
E
E!
* E * 5% 6
# 5H * HF* 6
0 )
4 5"( $
% *!
5
6
)
) $
$ , WE R’
EHH
$ , WE Q’
EHH
$ , WE ’
E H$H
$ , WE =’
E H;H
$ , WE >’
E H0H
$ 0
E H*H
0 )
0 *
% )
?$ 56
$
E 5 6
0 )
#
" #
! ! !
! 9
!
! *
! (’’
)
! !
! ! 5(6
576 5:6
!
"( ) #
! 3 !
!
!/ 5 6 !
! !/ 5 !6 !
!/ 5 ! 6 / !
! ! ! ! !
8
!
( ( 3 !
)
/ 5(6 / 576 / 5:6 / 5>6 / 5=6 / 56
/
( 7 ; !
/ 5( (6 / 5( 76 / 5( :6 / 5( >6
/ 57 (6 / 57 76 / 57 :6 / 57 >6
"( #
% ; ! " !
% !
; !
!
! I
; !/ 5 6 !
!
4 "
; $ / 5(’6 )
! (’ 3 (
5(6 $ / $ / 5(’6
3 (( ! $ / 5’6
$ / 5(’6
4 "(
; $ 5(’’ =’’6 ,
! $ 5(’’6
$ 5=’’6
! I
; !/ 5) ( ) 76 !
0 ( :
5 6 $
; / 5(’6 ; / 5(’6
) )
; , ; ,
% ) / %56 ) / 56
* E ( (’ * E ( (’
/ 5 6E / 5 6E
, 5H0 , 5H0
H H0 H6
/ H HH (=’’ >=’’68 ( ,
, / 5 6 / 5 6
HH /
* ( % 0 )
/ 5 6 % ) ) ?$ 56
0 /
0
0 , 0 )
/
0 )
,
!
!
!
!
!
(’
: % !
:"
& (: !
,
!
$;L [ ,
! !
:"( $ )
* K) 8 K ! * # L) 8 &
0
! !
0 I
* K MG M " 4 K ! * # L
! #! ;
! ( , ! !
H
* K# M " 4 K ! * # L
, ! 9 #8 !
* K MG M " K ! * ( # L
:"(" - ’ $ 4 )
% ) ?$ 56
; # )
; ) / )
3 H IJ#! ; J * H3 T(
# E # 5H* H6
) / E , 5H0 / H6
% T( ) /
# E # 5H H F ) / FH H6
$ T(
# E# 5H* H6
0 )
B #! ; D
! !!
:", 7 )
(Q 7 ! T 9
! 1
;,#
:"," - ’ 7 )
% )L ?$ 56
; ()
3 H IJ#! ; J * H, T(
, T( (
( E (
$ T(
0 )
B ! (
3 I
; )
% ) ?$ 56
$ ; (* E
H ‘B 6aB H
$ ; ( ) 3
,
$ ; ( * /
XW HH
3
$ ; (* /
* , T(
;
, T(
( E ( F
8 & 03*5(6
0 ,
$ T(
0 )
% ) ?$ 56
$ ; (* E
H ‘B 6aB H
$ ; () )
,
$ ; (* /
XW HH
3
$ ; (* /
* 3 T(
% T( (
$ T(
0 ,
0 )
< $ G # 6’
1 ! $;
$;
! !
G ) G $ ! !
9 # # $ !
! GG ! $ .
!
<" $ $
, ! $; ! $; ! ,
! "
$; , ! !
6 ,
% ) * ?8 56
D
8 E 5) + 6J 7
E 5) 9 + 9 6 J 7 D,
$;
!$; $ E H3 H
0 )
% ) !$;?) & 56
D&
/ $ E !$;
0 )
% ) / ?$ 56
!$; $ E H/ H
0 )
% ) % !?$ 56
!$; $ E H% !H
0 )
% ) % ?$ 56
!$; $ E H% H
0 )
% ) ) ?$ 56
!$; $ E H) H
0 )
(= $ G # 6’
, / $; !!
! $; ! ! !
! !
!
, " ! $ & 4 4 4
4 4 ! 4 ! , ) !
! G ) G
$ 0GG$ 1 ! ! ! , !
, ,
!
• $ + ! !
• ; 8 + !
%$
• ; 8 + !
• + !
• * 8 + !
L
!
!I
) (I & ! !
) 7I&
) :I& (
!! 4
) >I & ! 4 ’
) =I & - !4
$ bb
% )
% ) $ (?$ 56 !?$ 56
D !
D ! # *
$ 7?$
, 8 , E’ , $ ( 8 , E
* ( % E 5HB H6 ’
0 , 8 , E ( % ! ;
* ( % E 5HB H6 E H H
0 0 ,
* ( % E 5HB BH6 $ ( 8 , E(
0 ,
0 ) % ! ;
bb E H) M H
0 ,
% ) ; (?$ 56 % ! * /
E (
D 5
% ! $
6
H3 H
* ( % E ; (% E
% ! $
, $ ( 8 , E’ E H% !H
* ( % E 5HB H6 bb
0 ,$ ( 8 , E(
* (% E 5HB H6 0 )
0
* (% E 5HB BH6 % )
0 , ?$ 56
0 ) , % ! # E
=7> 0 )
, % ! #
XW =7=
% ! E
% ! $
E H) H
0 ,
% ! E
% ! $
E H$ H
0 )
% ) ; (?$ 56
D
; ( % E ; (;
0 )
% ) * (?$ 56
, $ (8 , E ’
* (% E 5HB H6
0 , $ ( 8 , E(
* (% E 5HB H6
0
* (% E 5HB BH6
0 ,
, L 5* (% (6 XW HJH
E* (% . HJH . * ( * /
0
E* (% .* (* /
0 ,
( E
0 )
% ) * ?8 56
D !
8 E 5) + 6 J 7
E 5) 9 +9 6 J 7
$ ( E HB H
$ ( , HB H
$ ( , HB H
$ ( , H H
0 )
( $ G # 6’
, 7’ ! ! /
! !
! !
! !
• $ + ! !
• ; 8 + !
%$
• ; 8 + !
• + !
• * 8 + !
L
! !I
) (I & ! !
) 7I&
) :I& (
! * 8
) >I & * 8 )
) =I & 0
)
$
% ) * ?8 56
D !
8 E 5) + 6J 7
E 5) 9 + 9 6J7
$ ( E H H
$ ( , H H
$ ( , H H
0 )
bb
% ) $ (?$ 56
, 8 , E’
* ( % E 5HB <B <B " <B H6
0
* ( % E 5HB BH6
0 ,
0 )
D) !
% ) ; (?$ 56
* ( % E; (%
* (% E 5HB <B <B " <B H6
0 )
D$ ;
% ) ; (?$ 56
; (% E ; (;
0 )
% ) * (?$ 56
, $ ( 8 , E’
* ( % E 5HB <B <B " <B H6
0
* ( % E 5HB BH6
0 , ,
, L 5* ( % (6 XW HJH
E * ( % . HJH . * ( * /
0
E * ( % . * ( * /
0 ,
( E
0 )
% ) ?$ 56
, L 5* ( % (6 XW HJH
E * ( % . HJH . * ( * /
0
E * ( % . * ( * /
0 ,
D
( % E 8 % 5 6
0 )
(, $ & 6’
#) ; % ,
,
D
%
/
? !
! ,
/ ,/; #8;
+ ; /
! / ,/; #;
/ + L ) !
1
! , H$ $ H
$ / 8
? ! D
;D ) ? ! 3
; * !
$ / / *= 1
D ! "
) "
7 )
1 ! * !
$ !
(5 $ & 6’
, 8 7: !
, !
!
!
I
. "7 - "G ! D#
. "7 - "G D#
. "7 - "G 8 4 D#
. "7 - "’ D#
1 I
. "7 - "# 8 D
. "7 - " D;
B I ?
, ! *
L / L % L 8 !L
1
5 (S6 !
/ + !
% ) $ 7?$ 56
L # *
0 )
% ) $ (?$ 56
L # /
0 )
% ) $ :?$ 56
L # %
0 )
% ) $ >?$ 56
L # 8
0 )
L ! !
(/ $ &
# *
, 8 77 8 7:
9 ! !
!
3 ;3 ; 4 "
;3 4+ 5
! 6 !
, !
;3 !
! $ . # 4
; $ ! ! ;3+
) 4 & )
;3 *
;3 + ) !
;3
/ !
I
* /
+
* $
;3
;3 /
8 ( /
8 ( $
8 7/
8 7 $ I
8 : / 1
8 : $ 1 % I
8 > / ,) /
8 > $ ,) /I
8 = / %
8 = $ % D
8 / ) "
8 $ ) " I
( /
( ; *
( ; )
7 / %
7 ; * 1 %
7 ; )
: / ,) /
: ; * ,) /
: ; )
> / % ,;
> ; * % ,;
> ; )
= / ) "
= ; * ) "
= ; )
$ (
)
/
$ (
F)
$
$ 7
/
$ 7
F
$
$ :
;
/
$ :
F;
$
$
$
/
$ >
F$
$
$ %
/
$
FX
$
$
/
/
$
FW
$
$ Q
0
/
$ Q
0F
$
! ;3
*"G
;3 I
6 $ ;3
6 $ $ ) !
$ &- D 3 /
% & 4 +$ G )
P ,"/ * "
/ *"G " 1
$ $ 3O
* ! L ) ! $
& & / ! !
bb
/ ! !
;3
¡¡
¡¡
* - I
% ) ) ?$ 56
L * 5H H6 E
L * 5H1 % H6 E %
L * 5H,) /H6 E ,) /
L * 5H% ,;H6 E % ,;
L * 5H) " H6 E ) "
L &
0 )
* # I
% ) ?$ 56
L /
0 )
*I
% ) ; ?$ 56
$ E # 5H ! ! [H 1/
H; $ H6
, $ E 1
L ;
# HL ; 2H H# H
0
# HL / ; 2H H# H
0 ,
0 )
bb
* $ I
% ) $ ?$ 56
E HH
% E HH
% ,; E HH
,) / E HH
) " E HH
0 )
* % 5X6
% ) % ?$ 56
, / L 3*
L # %
, L 3*
L # /
0 ,
0 ,
0 )
* / 5W6
% ) / ?$ 56
, / L 03*
L # /
, L 03*
L # %
0 ,
0 ,
0 )
( H G ) Q $ "=
, ! !
# ;3 $ ’ !
! ! !
3 ; ^ ^ ;
! ,
; ^ !
^ ;
" !
) ^# $ ; ’ !
! !
# ;3 ! 8 !
3O / ! ;
^ ;3
; ^ ;3
I
! ! # 9
, ! , !
!
I
/ ! ;3
;3 ;3;$
/ ; 8 % 5
6 , ! !
%
, 3O
;3;$ ! ;3!;$
L 7+ $ !
3O
* ! ! ! ; ^
; !) ; ^ (
1 ! ! &
! , ! !
/ I
(: H -R E
, ; ^ $ !
9
,!
)Z8 M -! )Z8 R !
& )Z8 !
! )Z8 ! )080$
! *L3#
! I
- $ ) @) (@"""""@) 8
!7*G & 8
( 7 /
1 ! M ! , !
! ! I
- $ 2 !7*G & 8
, )Z8 M
# @ @ # @ J @ - 8@
’& @ ’ & % & % " &
/ ;;;3 ^
/ # @
# $ # %
# ! 1 #!
I
/ ! ;3 %
7= 9 ! ;3;$
! L 6 $ 4
! $ -5)Z$86 2 ! !7*G & %"
¡¡
bb
/ ! I
% ) ?$ 56
( L ) E H)080$ *L3# H
(L
( $ E ( L )
0 )
% ) ?$ 56
( L ) E H)080$ *L3# H
(L
( $ E ( L )
0 )
* ! !
% ) ?$ 56
( L ) E H)080$ B *L3# H
(L
( $ E ( L )
0 )
/ ! ! ; !
! I
! ; ! !
! I
8 ! ; ! !
(; G -R S
, )Z8 ! )080$
*L3# , !
)Z8 ! 3 )Z8 !
90L0 !
)! I
- $ ) @) (@"""""@) 8
!7*G & 8 97 $
E W X XE WE XW 8
& !
* " ; ^
;3 ;3;$ !
L 6 $ 4 !
$ 5)Z86 ! - $ 2 !7*G & %" /
; ^
)Z8 M ! , Z !
I
VISUAL BASIC
Run the program and key in the following SQL query statement
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
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.
Example 21d3:
The following query statement will rank the records according to Author in ascending
order.
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:
First of all, you need to design the Welcome menu. You can follow the example as
follow:
In this form, you need to insert three command buttons and set their properties as
follow:
¡¡
main_menu.Hide
Register.Show
End Sub
¡¡
If a new user click the Register button, the registration form will appear. An example
is illustrated as follow:
This registration forms consist of two text boxes , three command buttons and an
ADO control. Their properties are set as follow:
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.
End Sub
UserInfo.Recordset.Fields("username") = txtName.Text
UserInfo.Recordset.Fields("password") = txtpassword.Text
UserInfo.Recordset.Update
Register.Hide
Login_form.Show
End Sub
¡¡
There are two text boxes and a command button, their properties are set as follow:
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
frmLibrary.Show
Exit Sub
Else
Register.UserInfo.Recordset.MoveNext
End If
Loop
Else
End
End If
End Sub
¡¡
¡¡
caption
Command button 5
cmdNext
name
Command button 5
N&ext
caption
Command button 6
cmdPrevious
name
Command button 6
caption &Previous
Command button 7
cmdExit
name
Command button 7
E&xit
caption
¡¡
¡¡
End Sub
End Sub
adoLibrary.Recordset.Fields("Title").Value = txtTitle.Text
adoLibrary.Recordset.Fields("Author").Value = txtAuthor.Text
adoLibrary.Recordset.Update
End Sub
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 = False
Image3.Visible = True
Image2.Visible = False
Image4.Visible = False
Image5.Visible = 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 = True
Image3.Visible = False
Image2.Visible = False
Image4.Visible = False
Image5.Visible = 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.
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:
End Sub
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
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
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.
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 as Animate and key in the following statements by double
clicking on this button. Bear in mind that you should enter the statements for hiding
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:
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