Sunteți pe pagina 1din 159

GUI Programming

2201-2412

INTRODUCTION TO
Visual Basic 6.0

Instructor: Anjan Mahanta


Copyright Mr.Anjan Mahanta LCCT International Studies Program 1
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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 2
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 3


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 4


Introduction to Visual Basic 6.0
 What programming language is Visual Basic
based on?
BASIC (stands for Beginner’s All Purpose
Symbolic Introduction Code)

Copyright Mr.Anjan Mahanta LCCT International Studies Program 5


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 6


Visual Basic 6.0 Programming Window

Copyright Mr.Anjan Mahanta LCCT International Studies Program 7


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.

Copyright Mr.Anjan Mahanta LCCT International Studies Program 8


Creating Your First Application

• Design your form as below:

Label
Text
Text Box Button
Command
Box

Command Button

Copyright Mr.Anjan Mahanta LCCT International Studies Program 9


Writing the program

• Double click on the command button ADD

Copyright Mr.Anjan Mahanta LCCT International Studies Program 10


Writing the program
• Enter the following code

Number 1

Label5= val(text1) + val(text2)


To display answer Number 2
Value

Copyright Mr.Anjan Mahanta LCCT International Studies Program 11


Running the program
• Enter number 1 (any number)
• Enter number 2 (any number)

Click on ADD

Copyright Mr.Anjan Mahanta LCCT International Studies Program 12


Running the program
• Click on ADD Command button

Copyright Mr.Anjan Mahanta LCCT International Studies Program 13


Calculator

Copyright Mr.Anjan Mahanta LCCT International Studies Program 14


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.

Copyright Mr.Anjan Mahanta LCCT International Studies Program 15


Addition of three numbers

Copyright Mr.Anjan Mahanta LCCT International Studies Program 16


Area of a Triangle

Copyright Mr.Anjan Mahanta LCCT International Studies Program 17


Code

• Double click on AREA


• Type,
Private Sub Command2_Click()
Text 3 = 0.5 * val(Text1) * Val(Text2)
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 18


Area of Circle

Copyright Mr.Anjan Mahanta LCCT International Studies Program 19


Code

• Double click on AREA


• Type,
Private Sub Command2_Click()
Text2 = 3.14 * (Val(Text1) ^2)
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 20


Discount

Copyright Mr.Anjan Mahanta LCCT International Studies Program 21


Code

• Double click on Discount


• Type,
Private Sub Command2_Click()
Text3 = Val (Text1) * (Val(text2) / 100)
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 22


Code

• Double click on Total


• Type,
Private Sub Command3_Click()
Text4 = Val(Text1) - Val(Text3)
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 23


Exercise

Copyright Mr.Anjan Mahanta LCCT International Studies Program 24


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.

Copyright Mr.Anjan Mahanta LCCT International Studies Program 25


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.
Copyright Mr.Anjan Mahanta LCCT International Studies Program 26
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 27


Logical Operators

AND: Both sides must be true


Side A Side B Result
TRUE TRUE ?
TRUE
TRUE FALSE ?
FALSE

Copyright Mr.Anjan Mahanta LCCT International Studies Program 28


Logical Operators

OR: One side or other must be true


Side A Side B Result
TRUE TRUE ?
TRUE
FALSE TRUE ?
TRUE
FALSE FALSE ?
FALSE
Copyright Mr.Anjan Mahanta LCCT International Studies Program 29
The general format for the if...then...else statement is

If conditions Then
VB expressions
Else
VB expressions
End If

Copyright Mr.Anjan Mahanta LCCT International Studies Program 30


Program for next class..
Design Mode

Text Box
Command Button

Copyright Mr.Anjan Mahanta LCCT International Studies Program 31


Run Mode
• When the program is Run, if the
user enters value greater than 8.

Copyright Mr.Anjan Mahanta LCCT International Studies Program 32


Run Mode
• When the program is Run, if the
user enters value less than 8.

Copyright Mr.Anjan Mahanta LCCT International Studies Program 33


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 34
Form Color

Option2 Option4 Option6

Option1 Option3 Option5

Copyright Mr.Anjan Mahanta LCCT International Studies Program 35


Code
• Double click on Option1

Private Sub Option1_Click()


If Option1.Enabled = True Then
Form1.BackColor = vbRed
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 36


Code
• Double click on Option2

Private Sub Option2_Click()


If Option2.Enabled = True Then
Form1.BackColor = vbBlue
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 37


Code
• Double click on Option3

Private Sub Option3_Click()


If Option3.Enabled = True Then
Form1.BackColor = vbWhite
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 38


Code
• Double click on Option4

Private Sub Option4_Click()


If Option4.Enabled = True Then
Form1.BackColor = vbBlack
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 39


Code
• Double click on Option5

Private Sub Option5_Click()


If Option5.Enabled = True Then
Form1.BackColor = vbYellow
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 40


Code

• Double click on Option6

Private Sub Option6_Click()


If Option6.Enabled = True Then
Form1.BackColor = vbGreen
End If
End Sub
Copyright Mr.Anjan Mahanta LCCT International Studies Program 41
Form Font

Label1

Option 1 Option 4
Option 2 Option 5
Option 3 Option 6

Copyright Mr.Anjan Mahanta LCCT International Studies Program 42


Code

• Double click on Option1

Private Sub Option1_Click()


If Option1.Enabled = True Then
Label1.FontBold = True
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 43


Code
• Double click on Option2

Private Sub Option2_Click()


If Option2.Enabled = True Then
Label1.FontItalic = True
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 44


Code
• Double click on Option3

Private Sub Option3_Click()


If Option3.Enabled = True Then
Label1.FontUnderline = True
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 45


Code
• Double click on Option4

Private Sub Option4_Click()


If Option4.Enabled = True Then
Label1.FontSize = 10
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 46


Code

• Double click on Option5

Private Sub Option5_Click()


If Option5.Enabled = True Then
Label1.FontSize = 20
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 47


Code

• Double click on Option6

Private Sub Option6_Click()


If Option6.Enabled = True Then
Label1.FontSize = 30
End If
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 48


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 49


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 50
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 51


If ..Then..Else..Endif Statement
If logical_expression Then
.....
executable statements
.....
Else
.....
executable statements
.....
End If
Copyright Mr.Anjan Mahanta LCCT International Studies Program 52
Select Case Statement

Select Case expression


Case value1
executable statements
Case value2
executable statements
.......
Case Else
executable statements
End Select
Copyright Mr.Anjan Mahanta LCCT International Studies Program 53
For - Next Looping

For index = value1 To value2


.......
executable statements
.......
Next index

Copyright Mr.Anjan Mahanta LCCT International Studies Program 54


Do - While Looping

Do While logical expression


..........
executable statements
..........
Loop

Copyright Mr.Anjan Mahanta LCCT International Studies Program 55


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 56
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 57


VB Control Fundamentals

• Control tool categories

Entering Text Drawing


Text Box Line Button
Combo Box Shape Button

Copyright Mr.Anjan Mahanta LCCT International Studies Program 58


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 59


VB Control Fundamentals
• Control tool categories
Displaying Graphics Viewing Windows
Image Box Frame
Picture Box Horizontal Scroll Bar
Frame Vertical Scroll Bar

Copyright Mr.Anjan Mahanta LCCT International Studies Program 60


VB Control Fundamentals
• Control tool categories
Managing Files Accessing Existing Data
File List Box Data
Drive List Box
Directory List Box

Copyright Mr.Anjan Mahanta LCCT International Studies Program 61


VB Control Fundamentals

• Control tool categories


Initiating Events Linking with Other Objects
Command Button OLE

Executing Timed Events


Timer

Copyright Mr.Anjan Mahanta LCCT International Studies Program 62


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 63


MsgBox
• Syntax
Msgbox “TEXT”, VALUE, “TITLE”

• Example 1
Msgbox “Sorry !!”, vbcritical, “Error”

• Example 2
Msgbox “Correct Password”, vbinformation, “Check”

Copyright Mr.Anjan Mahanta LCCT International Studies Program 64


Message box Example 1

• Design the following form

Label
Text Box Command Button

Copyright Mr.Anjan Mahanta LCCT International Studies Program 65


Code

• Double click on OK

Private Sub Command1_Click()


MsgBox “Hello!!” + Text1
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 66


Program Output

Copyright Mr.Anjan Mahanta LCCT International Studies Program 67


Message box Example 2

• Add a new form – FORM 2


• Click on Project in the Menu bar
• Click on Add Form

Copyright Mr.Anjan Mahanta LCCT International Studies Program 68


Program Output

Copyright Mr.Anjan Mahanta LCCT International Studies Program 69


Solution

• Double Click on OK

Private Sub Command1_Click()


MsgBox Text1 + “ Loves ” + Text2
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 70


Message box Example 3
• Design the following form

Copyright Mr.Anjan Mahanta LCCT International Studies Program 71


Program Output

Copyright Mr.Anjan Mahanta LCCT International Studies Program 72


Code
• Double click on OK

Private Sub Command1_Click()


MsgBox Text1 + “ ” + Text3 + “ ” + Text2
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 73


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 Copyright Mr.Anjan Mahanta LCCT International Studies Program 74
Example1 - List Box
• Design the following form

List Box

Copyright Mr.Anjan Mahanta LCCT International Studies Program 75


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 76
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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 77
Example 2: Grade Calculation
• Design the form2

Copyright Mr.Anjan Mahanta LCCT International Studies Program 78


Code
• Double click on Command Button Total

Private Sub Command1_Click()


Text5 = Val(Text3) + Val(Text4)
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 79



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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 80


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 81
Code
• Double click on Command Button Exit

Private Sub Command4_Click()


End
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 82


Form1 - frmmain

Copyright Mr.Anjan Mahanta LCCT International Studies Program 83


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 84
Double click on cancel
Private Sub Command2_Click()
MsgBox “Thankyou!!”
End
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 85


Form2 - frmoneway

Copyright Mr.Anjan Mahanta LCCT International Studies Program 86


Double click on frmoneway
Private Sub Form_Load()
Label2 = Date
Label3 = Time
Combo1.AddItem “F101”
Combo1.AddItem “F102”
Combo1.AddItem “F103”
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 87


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)
Copyright Mr.Anjan Mahanta LCCT International Studies Program 88
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 89


Double click on confirm
Private Sub Command1_Click()
MsgBox “Thankyou!! Enjoy your trip”
End
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 90


Double click on cancel

Private Sub Command1_Click()


unload me
frmmain.show
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 91


Form 3: frmroundtrip

Copyright Mr.Anjan Mahanta LCCT International Studies Program 92


If..Then..Elseif..Else..Endif
Exercise

Copyright Mr.Anjan Mahanta LCCT International Studies Program 93


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!!
Copyright Mr.Anjan Mahanta LCCT International Studies Program 94
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 95


Example Timer
• Design the following form

Timer

Label

Copyright Mr.Anjan Mahanta LCCT International Studies Program 96


Property of Timer1
• In the property of Timer1

Interval 600

Copyright Mr.Anjan Mahanta LCCT International Studies Program 97


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 98
Example 2

• Determine the greater number

Copyright Mr.Anjan Mahanta LCCT International Studies Program 99


On Run mode

Copyright Mr.Anjan Mahanta LCCT International Studies Program 100


Click GO

Copyright Mr.Anjan Mahanta LCCT International Studies Program 101


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 102


Password validation
• Design the following form

Label

Text Box

Command
Button

Copyright Mr.Anjan Mahanta LCCT International Studies Program 103


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 104


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 105


Example: Program to enter your age

Text Box

Copyright Mr.Anjan Mahanta LCCT International Studies Program 106


• Double click on OK
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 107


Example: Program to insert any string

Copyright Mr.Anjan Mahanta LCCT International Studies Program 108


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”
Copyright Mr.Anjan Mahanta LCCT International Studies Program 109
• Double click on OK
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 110


Hotel Example

Copyright Mr.Anjan Mahanta LCCT International Studies Program 111


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 112


Select case - Example

Copyright Mr.Anjan Mahanta LCCT International Studies Program 113


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 114


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 115


Option Button & Frames - Example

Label

Frame

Option Button

Copyright Mr.Anjan Mahanta LCCT International Studies Program 116


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 117
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 118


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 119


Quiz Example

• Design the following form

Copyright Mr.Anjan Mahanta LCCT International Studies Program 120


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 121


For Loop Example
• Design the following form

Combo Box

Copyright Mr.Anjan Mahanta LCCT International Studies Program 122


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 123
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 124


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 125


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 126


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 127


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.

Copyright Mr.Anjan Mahanta LCCT International Studies Program 128


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 129


Menu Editor

Menu Caption

Menu Name

Menu Shortcut Key


To add new menu
To delete menu
Directions
To insert new menu
(Left, Right, Up, Down)

Copyright Mr.Anjan Mahanta LCCT International Studies Program 130


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 131


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 132


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 133
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.

Copyright Mr.Anjan Mahanta LCCT International Studies Program 134


Creating a Database Application
Step 1
• Create a table in MS-Access
• Save as, student

Field Name DataType FieldSize

Primary Code Number Integer


Key
Name Text 50

• Enter any five records


Copyright Mr.Anjan Mahanta LCCT International Studies Program 135
Creating a Database Application

Step 2
• Design the following form in VB

Text Box

Data

Copyright Mr.Anjan Mahanta LCCT International Studies Program 136


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 137


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 138


Design the form grade in VB

Copyright Mr.Anjan Mahanta LCCT International Studies Program 139


• 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 = ""

Copyright Mr.Anjan Mahanta LCCT International Studies Program 140


• Double click on delete
Data1.Recordset.Delete
Data1.Recordset.Movefirst

Double click on edit


Data1.Recordset.Edit
Text1.SetFocus

Copyright Mr.Anjan Mahanta LCCT International Studies Program 141


• 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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 142


Phone book Application

Copyright Mr.Anjan Mahanta LCCT International Studies Program 143


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 144


Designing Status bar

Copyright Mr.Anjan Mahanta LCCT International Studies Program 145


Designing Status bar
• Index 1
• Text Date
• Click on Insert Panel
• Index 2
• Text Time
• Click OK

Copyright Mr.Anjan Mahanta LCCT International Studies Program 146


Writing Code
• Double click on form

Private Sub Form_Load()


StatusBar1.Panels(1) = Date
StatusBar1.Panels(2) = Time
End Sub

Copyright Mr.Anjan Mahanta LCCT International Studies Program 147


Quiz Project
• Start MS-ACCESS 97
• Save Project as, Quiz
• Save Table 1 as, Computer_Quiz
• Save Table 2 as, Score_Quiz

Copyright Mr.Anjan Mahanta LCCT International Studies Program 148


Table 1 - Computer_Quiz
FieldName DataType
No Autonumber
Question Memo
Option1 Text
Option2 Text
Option3 Text
Answer Number

Copyright Mr.Anjan Mahanta LCCT International Studies Program 149


Table 2 - Score_Quiz

FieldName DataType
Code Autonumber
Name Text
Score Number

Copyright Mr.Anjan Mahanta LCCT International Studies Program 150


Start Visual Basic (VB)
Form1: Password

Copyright Mr.Anjan Mahanta LCCT International Studies Program 151


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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 152
Start Visual Basic (VB)
Form2: Quiz

Copyright Mr.Anjan Mahanta LCCT International Studies Program 153


Connect Data1 (On-line Computer Test)

• Select Data1
• In the property of Data1,
• Database name Quiz
• Recordsource Computer_Quiz

Copyright Mr.Anjan Mahanta LCCT International Studies Program 154


• Double Click on Command 1
Code
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
Copyright Mr.Anjan Mahanta LCCT International Studies Program 155
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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 156


Designing Toolbar

Black Blue Red White Yellow

Copyright Mr.Anjan Mahanta LCCT International Studies Program 157


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

Copyright Mr.Anjan Mahanta LCCT International Studies Program 158


Select Case Button.Index
Case 1
notepad = Shell(“notepad”, vbNormalFocus)
Case 2
Form2.Show
Case 3
End
End Select

Copyright Mr.Anjan Mahanta LCCT International Studies Program 159

S-ar putea să vă placă și