Visual Basic 6.0
-
Upload
anjan-mahanta -
Category
Education
-
view
166 -
download
2
Transcript of Visual Basic 6.0
1 Copyright Mr.Anjan Mahanta LCCT International Studies Program
INTRODUCTION TO
Visual Basic 6.0
Instructor: Anjan Mahanta
GUI Programming 2201-2412
2 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Course Outline
Introduction to VB Analyzing VB Programs Controls & Properties Examining Labels, Buttons & Text Boxes Putting Code into Visual Basic Message & Input Boxes Making Decisions VB Looping List Boxes & Data Lists Additional Controls Modular Programming Built-In Functions VB Database Basics Menus & VB The Graphics Image Controls Toolbars & More Graphics Database Connectivity & Programming
3 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Introduction to Visual Basic 6.0
What is Visual Basic?
Creates Graphic User Interface (GUI) Creates applications for the Microsoft Windows OS Major components:
1. Forms
2. Controls
3. Commands
4. Objects
4 Copyright Mr.Anjan Mahanta LCCT International Studies Program
History of Visual Basic
• Alan Cooper developed Visual Basic in 1988 and sold to Microsoft
• VB1 Debuts at Windows World in March 20,1991
• VB2 Debuts in November 1992
• VB3 Debuts in June 1993
• VB4 Debuts in October 1996
• VB5 Debuts in April 1997
• VB6 Debuts in October 1998
5 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Introduction to Visual Basic 6.0
What programming language is Visual Basic based on?
BASIC (stands for Beginner’s All Purpose Symbolic Introduction Code)
6 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Starting Visual Basic 6.0
• Click on Start • Select Programs • Select Microsoft Visual Studio 6.0 • Click on Microsoft Visual Basic 6.0 • Click on Open
7 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Visual Basic 6.0 Programming Window
8 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Introduction to Visual Basic 6.0
What is a Form Window?
The form window holds the application’s
form background and all its user controls,
such as command buttons.
What is a Toolbox?
The toolbox contains the controls that you
place on the Form Window. All the controls
appear on the toolbox.
9 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Creating Your First Application
• Design your form as below:
Text Box Command Button
Command Button
Label Text Box
10 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Writing the program
• Double click on the command button
ADD
11 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Enter the following code
Label5= val(text1) + val(text2)
To display answer Number 2
Number 1
Value
Writing the program
12 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Enter number 1 (any number) • Enter number 2 (any number)
Click on ADD
Running the program
13 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Click on ADD Command button
Running the program
14 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Calculator
15 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Controls in the Toolbox
• Label – A label control is used to display text that a user can’t
change directly.
• Textbox – A textbox control is used to input data from the user at run
time.
• Command button – A command button is used to perform an event at run time.
16 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Addition of three numbers
17 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Area of a Triangle
18 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on AREA • Type, Private Sub Command2_Click() Text 3 = 0.5 * val(Text1) * Val(Text2) End Sub
19 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Area of Circle
20 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on AREA • Type, Private Sub Command2_Click() Text2 = 3.14 * (Val(Text1) ^2) End Sub
21 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Discount
22 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Discount • Type, Private Sub Command2_Click() Text3 = Val (Text1) * (Val(text2) / 100) End Sub
23 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Total • Type, Private Sub Command3_Click() Text4 = Val(Text1) - Val(Text3) End Sub
24 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Exercise
25 Copyright Mr.Anjan Mahanta LCCT International Studies Program
IF …..Then…..Else Statement
• If...Then...Else statement is used for controlling the program flow.
• To control the VB program flow, we can use various conditional operators
and logical operators.
26 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Conditional Operators
Operator Meaning
= Equal to
> Greater than
< Less Than
< > Not Equal to • The conditional operators compares data
values and then decide what action to take.
27 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Examples
A=10, B=15 Operations Result Is A=B ? FALSE / NO Is A>B ? FALSE / NO Is A<B ? TRUE / YES Is A< >B ? TRUE / YES
28 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Logical Operators
AND: Both sides must be true Side A Side B Result TRUE TRUE ? TRUE TRUE FALSE ? FALSE
29 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Logical Operators
OR: One side or other must be true Side A Side B Result TRUE TRUE ? TRUE FALSE TRUE ? TRUE FALSE FALSE ? FALSE
30 Copyright Mr.Anjan Mahanta LCCT International Studies Program
The general format for the if...then...else statement is
If conditions Then
VB expressions
Else
VB expressions
End If
31 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Program for next class.. Design Mode
Text Box Command Button
32 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Run Mode
• When the program is Run, if the user enters value greater than 8.
33 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Run Mode
• When the program is Run, if the user enters value less than 8.
34 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Coding..
• Double Click on OK Private Sub Command1_Click() If Text1 > 8 Then MsgBox (“You are late !!”) Else MsgBox (“You are not late !!”) End If End Sub
35 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Form Color
Option1 Option3 Option5
Option2 Option4 Option6
36 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option1
Private Sub Option1_Click() If Option1.Enabled = True Then Form1.BackColor = vbRed End If End Sub
37 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Option2
Private Sub Option2_Click() If Option2.Enabled = True Then Form1.BackColor = vbBlue End If End Sub
38 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Option3
Private Sub Option3_Click() If Option3.Enabled = True Then Form1.BackColor = vbWhite End If End Sub
39 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option4
Private Sub Option4_Click() If Option4.Enabled = True Then Form1.BackColor = vbBlack End If End Sub
40 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option5
Private Sub Option5_Click() If Option5.Enabled = True Then Form1.BackColor = vbYellow End If End Sub
41 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option6
Private Sub Option6_Click() If Option6.Enabled = True Then Form1.BackColor = vbGreen End If End Sub
42 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Form Font
Label1
Option 2
Option 3
Option 4
Option 5
Option 6
Option 1
43 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option1
Private Sub Option1_Click() If Option1.Enabled = True Then Label1.FontBold = True End If End Sub
44 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option2
Private Sub Option2_Click() If Option2.Enabled = True Then Label1.FontItalic = True End If End Sub
45 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Option3
Private Sub Option3_Click() If Option3.Enabled = True Then Label1.FontUnderline = True End If End Sub
46 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Option4
Private Sub Option4_Click() If Option4.Enabled = True Then Label1.FontSize = 10 End If End Sub
47 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option5
Private Sub Option5_Click() If Option5.Enabled = True Then Label1.FontSize = 20 End If End Sub
48 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Option6
Private Sub Option6_Click() If Option6.Enabled = True Then Label1.FontSize = 30 End If End Sub
49 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Visual Basic Fundamentals
• Numeric constants: integer, long integers, single -
precision real (or floating point), double-precision real (or
floating point)
• String constants
• Variables
– Name must begin with a letter, letters and numbers can be included, as well as underline character ( _ )
– Data-typing characters ( %, &, !, #, $) are not permitted
– Maximum length of name = 255characters
50 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Data types and data declaration
• Data types supported: Integer, Long, Single, Double, String, Boolean, Byte, Currency, Date
• Data declaration uses the Dim statement Dim variable_name_1 As data_type_1,
variable_name_2 As data_type_2, etc.
Example:
Dim firstname as string
51 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Branching and Looping
• Relational operators and logical expressions
– Relational operators: = , < > , < , <= , > , >=
– Logical expressions, e.g., a > b, c = d + 2, x >= y,
can be either true or false
52 Copyright Mr.Anjan Mahanta LCCT International Studies Program
If ..Then..Else..Endif Statement
If logical_expression Then
. . . . .
executable statements
. . . . .
Else
. . . . .
executable statements
. . . . .
End If
53 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Select Case Statement
Select Case expression
Case value1
executable statements
Case value2
executable statements
. . . . . . .
Case Else
executable statements
End Select
54 Copyright Mr.Anjan Mahanta LCCT International Studies Program
For - Next Looping
For index = value1 To value2
. . . . . . .
executable statements
. . . . . . .
Next index
55 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Do - While Looping
Do While logical expression
. . . . . . . . . .
executable statements
. . . . . . . . . .
Loop
56 Copyright Mr.Anjan Mahanta LCCT International Studies Program
VB Control Fundamentals
• Visual Basic control tools * Check box
* Combo box
* Command Button
* Data
* Directory List Box
* Drive List Box
* File List Box
* Frame
* Horizontal Scroll Bar
57 Copyright Mr.Anjan Mahanta LCCT International Studies Program
VB Control Fundamentals
• Visual Basic control tools * Image Box
* Label
* Line
* List Box
* OLE Container
* Option Button
* Picture Box
* Pointer
* Shape
* Text Box
* Timer
* Vertical Scroll Bar
58 Copyright Mr.Anjan Mahanta LCCT International Studies Program
VB Control Fundamentals
• Control tool categories
Entering Text Drawing
Text Box Line Button
Combo Box Shape Button
59 Copyright Mr.Anjan Mahanta LCCT International Studies Program
VB Control Fundamentals
• Control tool categories
Displaying Text Selecting Among Alternatives
Label Check Box
Text Box Option Button
List Box Frame
Combo Box List Box
60 Copyright Mr.Anjan Mahanta LCCT International Studies Program
VB Control Fundamentals
• Control tool categories
Displaying Graphics Viewing Windows
Image Box Frame
Picture Box Horizontal Scroll Bar
Frame Vertical Scroll Bar
61 Copyright Mr.Anjan Mahanta LCCT International Studies Program
VB Control Fundamentals
• Control tool categories
Managing Files Accessing Existing Data
File List Box Data
Drive List Box
Directory List Box
62 Copyright Mr.Anjan Mahanta LCCT International Studies Program
VB Control Fundamentals
• Control tool categories Initiating Events Linking with Other Objects
Command Button OLE
Executing Timed Events
Timer
63 Copyright Mr.Anjan Mahanta LCCT International Studies Program
MsgBox • Message boxes are used when you want to ask the
user a question or display an error message(s) and advise the user
• There are six types of message boxes - – vbOKonly
– vbOKCancel
– vbAbortRetryIgnore
– vbYesNoCancel
– vbYesNo
– vbRetryCancel
64 Copyright Mr.Anjan Mahanta LCCT International Studies Program
MsgBox
• Syntax
Msgbox “TEXT”, VALUE, “TITLE”
• Example 1
Msgbox “Sorry !!”, vbcritical, “Error”
• Example 2
Msgbox “Correct Password”, vbinformation, “Check”
65 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Message box Example 1
• Design the following form
Label Text Box Command Button
66 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on OK
Private Sub Command1_Click() MsgBox “Hello!!” + Text1 End Sub
67 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Program Output
68 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Message box Example 2
• Add a new form – FORM 2 • Click on Project in the Menu bar • Click on Add Form
69 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Program Output
70 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Solution
• Double Click on OK
Private Sub Command1_Click() MsgBox Text1 + “ Loves ” + Text2 End Sub
71 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Message box Example 3
• Design the following form
72 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Program Output
73 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on OK
Private Sub Command1_Click() MsgBox Text1 + “ ” + Text3 + “ ” + Text2 End Sub
74 Copyright Mr.Anjan Mahanta LCCT International Studies Program
If..Then..Elseif..Else..Endif Statement If logical_expression Then
. . . . .
executable statements
. . . . .
ElseIf logical_expression Then
. . . . .
executable statements
. . . . .
various ElseIf clauses
. . . . .
Else
. . . . .
executable statements
. . . . .
End If
75 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Example1 - List Box
• Design the following form
List Box
76 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on form1
Private Sub Form_Load() List1.AddItem “……” List1.AddItem “Red” List1.AddItem “Yellow” List1.AddItem “Green” List1.AddItem “Blue” End Sub
77 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on List1 Private Sub List1_Click() If List1 = “Red” Then Form1.BackColor = vbRed ElseIf List1 = “Yellow” Then Form1.BackColor = vbYellow ElseIf List1 = “Green” Then Form1.BackColor = vbGreen ElseIf List1 = “Blue” Then Form1.BackColor = vbBlue Else Form1.BackColor = vbWhite End If End Sub
78 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Example 2: Grade Calculation
• Design the form2
79 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Command Button Total
Private Sub Command1_Click() Text5 = Val(Text3) + Val(Text4) End Sub
80 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Command Button Grade Private Sub Command2_Click()
If Val(Text5) >= 80 And Val(Text5) <= 100 Then Text6 = “A” ElseIf Val(Text5) >= 60 And Val(Text5) <= 79 Then Text6 = “B” ElseIf Val(Text5) >= 40 And Val(Text5) <= 59 Then Text6 = “C” ElseIf Val(Text5) >= 0 And Val(Text5) <= 39Then Text6 = “F” Else Text6 = “ ” MsgBox “Error!!” End If End Sub
81 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Command Button Clear
Private Sub Command3_Click() Text1 = Clear Text2 = Clear Text3 = Clear Text4 = Clear Text5 = Clear Text6 = Clear End Sub
82 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Command Button Exit
Private Sub Command4_Click() End End Sub
83 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Form1 - frmmain
84 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Double click on confirm
Private Sub Command1_Click() If Option1 = True Then frmoneway.Show frmmain.Hide ElseIf Option2 = True Then frmroundtrip.Show frmmain.Hide Else MsgBox "Please select a trip" End If End Sub
85 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Double click on cancel
Private Sub Command2_Click() MsgBox “Thankyou!!” End End Sub
86 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Form2 - frmoneway
87 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Double click on frmoneway Private Sub Form_Load() Label2 = Date Label3 = Time Combo1.AddItem “F101” Combo1.AddItem “F102” Combo1.AddItem “F103” End Sub
88 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Double click on Combo1_Click Private Sub Combo1_Click()
If Combo1 = “F101” Then
Text1 = “Bangkok”
Text2 = “Chiangmai”
Text3 = 1000
Text4 = 200
Text5 = Val(Text3) + Val(Text4)
ElseIf Combo1 = “F102” Then
Text1 = “Chiangmai”
Text2 = “Bangkok”
Text3 = 1000
Text4 = 200
Text5 = Val(Text3) + Val(Text4)
89 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Double click on Combo1_Click
ElseIf Combo1 = “F103” Then
Text1 = “Bangkok”
Text2 = “Phuket”
Text3 = 1500
Text4 = 200
Text5 = Val(Text3) + Val(Text4)
Else
MsgBox "Please select your flight no"
End If
End Sub
90 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Double click on confirm Private Sub Command1_Click() MsgBox “Thankyou!! Enjoy your trip” End End Sub
91 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Double click on cancel
Private Sub Command1_Click() unload me frmmain.show End Sub
92 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Form 3: frmroundtrip
93 Copyright Mr.Anjan Mahanta LCCT International Studies Program
If..Then..Elseif..Else..Endif Exercise
94 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Grade Criteria
Total Grade 90-100 A+ 80-89 A 70-79 B+ 60-69 B 50-59 C+ 40-49 C 0-39 F >100 or < 0 Error!!
95 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Solution • Double click on Command1 Grade Private Sub Command1_Click() If Val(Text2) >= 90 And Val(Text2) <= 100 Then Text3 = “A+” ElseIf Val(Text2) >= 80 And Val(Text2) <= 89 Then Text3 = “A” ElseIf Val(Text2) >= 70 And Val(Text2) <=79 Then Text3 = “B+” ElseIf Val(Text2) >= 60 And Val(Text2) <= 69 Then Text3 = “B” ElseIf Val(Text2) >= 50 And Val(Text2) <= 59 Then Text3 = “C+” ElseIf Val(Text2) >= 40 And Val(Text2) <= 49 Then Text3 = “C” ElseIf Val(Text2) >= 0 And Val(Text2) <= 39 Then Text3 = “F” Else Text3 = “Error!!” End If End Sub
96 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Example Timer
• Design the following form
Label
Timer
97 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Property of Timer1
• In the property of Timer1
Interval 600
98 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code for Timer1
• Double click on Timer1 Private Sub Timer1_Timer() If Label1.Visible = True Then Label1.Visible = False Else Label1.Visible = True End If End Sub
99 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Example 2
• Determine the greater number
100 Copyright Mr.Anjan Mahanta LCCT International Studies Program
On Run mode
101 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Click GO
102 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Solution
• Double click on Command1 GO
If Val(Text1) > Val(Text2) Then MsgBox “B is smaller, B= ” + Text2 Else MsgBox “A is smaller, A= ” + Text1 End If
103 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Password validation
• Design the following form
Label
Text Box
Command Button
104 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Command Button1 Login Private Sub Command1_Click() Dim password As String password = “lcct” If Text1 = password Then password = MsgBox(“You have passsed security!!”, vbOKOnly +
vbExclamation, “Access Granted”) Else password = MsgBox (“Incorrect Password!!”, vbRetryCancel +
vbCritical, “Access Denied”) If password = vbRetry Then Text1 = "" Text1.SetFocus End If End If End Sub
105 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Select Case • The Select Case structure tests whether the value
of an expression falls within predetermined ranges. • Syntax Select Case expression Case testlist {instructions} Case testlist {instructions} :
Case else {instructions} End Select
106 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Example: Program to enter your age
Text Box
107 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Double click on
Dim age as Integer age=text1 Select case age case 1 to 12 Msgbox (“Child”) case 13 to 19 Msgbox (“Teenager”) case is > 19 Msgbox (“Adult”) case else Msgbox (“Impossible”) End select
OK
108 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Example: Program to insert any string
109 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Program Explanation
• Use Select Case to do the program • When the user enters blank string
– Display Message box “Null / Blank String”
• When the user enters a or e or I or o or u – Display Message box “Vowel”
• When the user enters a to z – Display Message box “Consonant”
• When the user enters 0 to 9 – Display Message box “Numeric Digit”
• When the user enters any other characters such as $ or @ or * – Display Message box “Special Character”
110 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Double click on Dim str As String str = Text1 Select Case Left(str, 1) Case “” MsgBox (“Null String”) Case “A”, “E”, “I”, “O”, “U”, “a”, “e”, “i”, “o”, “u” MsgBox (“Vowel”) Case “A” To “Z”, “a” To “z” MsgBox “Consonant” Case “0” To “9” MsgBox “Numeric Digit” Case Else MsgBox “Special Character” End Select
OK
111 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Hotel Example
112 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Private Sub Command1_Click()
Select Case Combo1 Case “Single” Text1 = (Val(Text2) * 500) & “ Baht ” Case “Single A/C” Text1 = (Val(Text2) * 1000) & “ Baht ” Case “Double” Text1 = (Val(Text2) * 800) & “ Baht ” Case “Double A/C” Text1 = (Val(Text2) * 1500) & “ Baht ” Case “HoneyMoon Suite” Text1 = (Val(Text2) * 2000) & “ Baht ” End Select End Sub
113 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Select case - Example
114 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Form Private Sub Form_Load()
Combo1.AddItem "English"
Combo1.AddItem "Thai"
Combo1.AddItem "Japanese"
Combo1.AddItem "Chinese"
Combo1.AddItem "India"
End Sub
115 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Combo1 Private Sub Combo1_Click()
Select Case Combo1
Case "English"
Text1 = "Hello"
Case "Thai"
Text1 = "Suwadee"
Case "Japanese"
Text1 = "Konichiwa"
Case "Chinese"
Text1 = "Nihaw"
Case "India"
Text1 = "Namaste"
End Select
End Sub
116 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Option Button & Frames - Example
Frame
Option Button
Label
117 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code Private Sub Option1_Click()
Label1.FontBold = True
End Sub
Private Sub Option2_Click()
Label1.FontBold = False
End Sub
Private Sub Option3_Click()
Label1.FontItalic = True
End Sub
Private Sub Option4_Click()
Label1.FontItalic = False
End Sub
Private Sub Option5_Click()
Label1.FontUnderline = True
End Sub
Private Sub Option6_Click()
Label1.FontUnderline = False
End Sub
118 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code Private Sub Option7_Click()
Label1.FontSize = 10
End Sub
Private Sub Option8_Click()
Label1.FontSize = 15
End Sub
Private Sub Option9_Click()
Label1.FontSize = 20
End Sub
119 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
Private Sub Option10_Click()
Label1.ForeColor = vbBlue
End Sub
Private Sub Option11_Click()
Label1.ForeColor = vbRed
End Sub
Private Sub Option12_Click()
Label1.ForeColor = vbYellow
End Sub
Private Sub Option13_Click()
Label1.ForeColor = vbGreen End Sub
120 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Quiz Example
• Design the following form
121 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Score, Command Button1 Private Sub Command1_Click() Dim answer As Integer
If Option2.Value = True Then
answer = answer + 1
End If
If Option4.Value = True Then
answer = answer + 1
End If
If Option8.Value = True Then
answer = answer + 1
End If
If answer = 3Then
MsgBox “You scored 3”
ElseIf answer = 2 Then
MsgBox “You scored 2”
ElseIf answer = 1 Then
MsgBox “You scored 1”
ElseIf answer = 0 Then
MsgBox “You scored 0”
End If
End Sub
122 Copyright Mr.Anjan Mahanta LCCT International Studies Program
For Loop Example • Design the following form
Combo Box
123 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Command Button 1 Add Number
Private Sub Command1_Click() Combo1.AddItem Text1 Text1 = "" Text1.SetFocus
End Sub • Double click on Command Button 2 Count
Private Sub Command2_Click() MsgBox “Total items = ” & Combo1.ListCount End Sub
124 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Command Button 3 Maximum
Private Sub Command3_Click() Dim Max As Integer Dim i As Integer Max = 0 For i = 0 To Combo1.ListCount - 1 If Combo1.List(i) > Max Then Max = Combo1.List(i) End If Next i MsgBox “Maximum Number = ” & Max End Sub
125 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Command Button 4 Minimum
Private Sub Command4_Click() Dim Min As Integer Dim i As Integer Min = 100 For i = 0 To Combo1.ListCount - 1 If Combo1.List(i) < Min Then Min = Combo1.List(i) End If Next i MsgBox “Minimum Number = ” & Min End Sub
126 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Command Button 5 Sum
Private Sub Command5_Click() Dim sum, i As Integer For i = 0 To Combo1.ListCount - 1 sum = sum + Val(Combo1.List(i)) Next i MsgBox “Sum=” & sum End Sub
127 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code
• Double click on Command Button 6 Average
Private Sub Command6_Click() Dim average, sum, i As Integer For i = 0 To Combo1.ListCount - 1 sum = sum + Val(Combo1.List(i)) Next i average = Val(sum) / Combo1.ListCount MsgBox “Average = ” & average End Sub
128 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing a Menu Bar
• What is a Menu Bar ? – A menu bar is used to display a list of items from where
an user can select any one item.
129 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Example: Menu Bar
• Click on the Menu Editor in the Tool Bar • In the caption, type File • In the name, type mnufile • Click OK • Run
130 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Menu Editor
Menu Caption
Menu Name
Menu Shortcut Key
To add new menu
To insert new menu Directions (Left, Right, Up, Down)
To delete menu
131 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing a Sub_Menu
• Click on Menu Editor in Toolbar. • In Caption, type File • In Name, type mnuFile • Click on Next • Click on Right Arrow Button • In Caption, type Open • In Name, type mnuOpen • In ShortCut Key, Ctrl+O • Click on Next • In Caption, type - • In Name, type mnublank1 • Click on Next • In Caption, type New • In Name, type mnuNew • In ShortCut Key, Ctrl+N
132 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing a Multiple menu
• Click on Menu Editor in Toolbar. • In Caption, type File • In Name, type mnuFile • Click on Next • Click on Right Arrow Button • In Caption, type Open • In Name, type mnuOpen • In ShortCut Key, Ctrl+O • Click on Next • In Caption, type - • In Name, type mnublank1 • Click on Next • In Caption, type New • In Name, type mnuNew • In ShortCut Key, Ctrl+N
133 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing the EDIT Menu
• Click on Menu Editor in Toolbar. • Click on Left Arrow Button • In Caption, type Edit • In Name, type mnuEdit • Click on Next • Click on Right Arrow Button • In Caption, type Copy • In Name, type mnuCopy • In ShortCut Key, Ctrl+C • Click on Next • In Caption, type - • In Name, type mnublank2 • Click on Next • In Caption, type Cut • In Name, type mnuCut
• In ShortCut Key, Ctrl+X
• Click on Next • In Caption, type - • In Name, type mnublank3 • Click on Next • In Caption, type Paste • In Name, type mnuPaste
• In ShortCut Key, Ctrl+V
134 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Using the Data Control to Interact with Databases
• The VB Data Control is used to attach an
existing database, providing a link between your application and your data.
• Various controls - such as – labels – images – text boxes can be connected to the data control.
135 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Creating a Database Application
Step 1 • Create a table in MS-Access • Save as, student
• Enter any five records
Field Name DataType FieldSize
Code Number Integer Name Text 50
Primary Key
136 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Creating a Database Application
Step 2 • Design the following form in VB
Text Box
Data
137 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Creating a Database Application Step 3 Connecting the Data Control • Select Data1 • In the property window, • Select Database Name - Student • Select Record Source (Table Name) - Student • Select Text1 • In the property window, • Select Data Source – Data1 • Select Data Field - Code • Select Text2 • In the property window, • Select Data Source – Data1 • Select Data Field - Name
138 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Create a table in MS-Access Save Database as : grade
Save Table as: grade
Field name Fieldtype
S_Code Number Name Text G_Vb Text G_Datastructure Text G_P_A Text
139 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Design the form grade in VB
140 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Double click on calculate G.P.A. Text5 = (Val(Text3) + Val(Text4)) / 2
• Double click on addnew Data1.Recordset.AddNew Text1.SetFocus Text1 = "" Text2 = "" Text3 = "" Text4 = "" Text5 = ""
141 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Double click on delete Data1.Recordset.Delete Data1.Recordset.Movefirst
Double click on edit Data1.Recordset.Edit Text1.SetFocus
142 Copyright Mr.Anjan Mahanta LCCT International Studies Program
• Double click on save Data1.Recordset.Fields(“S_Code”) = Text1 Data1.Recordset.Fields(“Name”) = Text2 Data1.Recordset.Fields(“Grade_Vb”) = Text3 Data1.Recordset.Fields(“Grade_Datastructure”) = Text4 Data1.Recordset.Fields(“G_P_A”) = Text5 Data1.Recordset.Update Data1.Recordset.refresh
143 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Phone book Application
144 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing Status bar
• Right click on the Toolbox • Select Microsoft Windows Common Controls 6.0 • Click Ok • Double click on the status bar • Select the status bar • Right click • Click on Properties • Select Panels
145 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing Status bar
146 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing Status bar
• Index 1 • Text Date • Click on Insert Panel • Index 2 • Text Time • Click OK
147 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Writing Code
• Double click on form
Private Sub Form_Load() StatusBar1.Panels(1) = Date StatusBar1.Panels(2) = Time End Sub
148 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Quiz Project
• Start MS-ACCESS 97 • Save Project as, Quiz • Save Table 1 as, Computer_Quiz • Save Table 2 as, Score_Quiz
149 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Table 1 - Computer_Quiz
FieldName DataType No Autonumber Question Memo Option1 Text Option2 Text Option3 Text Answer Number
150 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Table 2 - Score_Quiz
FieldName DataType Code Autonumber Name Text Score Number
151 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Start Visual Basic (VB)
Form1: Password
152 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double click on Command1 (SignIn) Private Sub Command1_Click() If Text1 = “lcct” And Text2 = “lcct” Then Unload Me Form2.Show Else a = MsgBox(“Please Try Again”, vbCritical, vbOK) Text1 = "" Text2 = "" Text1.SetFocus End If End Sub
153 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Start Visual Basic (VB)
Form2: Quiz
154 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Connect Data1 (On-line Computer Test)
• Select Data1 • In the property of Data1, • Database name Quiz • Recordsource Computer_Quiz
155 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Code • Double Click on Command 1 Private Sub Command1_Click() If Data1.Recordset(“Answer”) = Val(Text1) Then MsgBox “Congratulations!! Correct” Label9 = Label9 + 1 Data1.Recordset.MoveNext Else MsgBox “Sorry!! Wrong” Data1.Recordset.MoveNext End If Text1 = "" Text1.SetFocus
156 Copyright Mr.Anjan Mahanta LCCT International Studies Program
If Data1.Recordset.EOF Then Data1.Recordset.MoveLast Data2.Recordset.AddNew Data2.Recordset(“Name”) = Text2 Data2.Recordset(“Score”) = Label9 Data2.Recordset.Update Data2.Refresh MsgBox “Thankyou for the test” End End If EndSub
157 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Designing Toolbar
Black Blue Red White Yellow
158 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Select Case Button.Index Case 1 Form1.BackColor = vbBlack Case 2 Form1.BackColor = vbBlue Case 3 Form1.BackColor = vbRed Case 4 Form1.BackColor = vbWhite Case 5 Form1.BackColor = vbYellow End Select
159 Copyright Mr.Anjan Mahanta LCCT International Studies Program
Select Case Button.Index Case 1 notepad = Shell(“notepad”, vbNormalFocus) Case 2 Form2.Show Case 3 End End Select