Visual Basic 6.
Presented by-
Sri Jahnab Kr. Deka
1
How to open a Project?
2
How to open a Project-2
3
How to write codes in an event
4
Comments line in VB
How to write comments in VB
' This is a comment
Some important data types in VB
String
Double
Integer
Date
How to declare variables
Implicit
Total = Total + 10
Explicit
Dim Total as double
Total= Total + 10
5
Scope of a Variable
The term Scope refers to whether the variable is available outside the procedure in which it
appears. The scope is procedure-level or module-level.
A variable declared with Dim at the beginning of a procedure is only available in that
procedure. When the procedure ends, the variable disappears. Consider the following example:
Option Explicit
Dim Total2 As Integer
Private Sub Command1_Click ()
Dim Total1 As Integer
Static Total3 As Integer
Total1 = Total1 + 1
Total2 = Total2 + 1
Total3 = Total3 + 1
End Sub
Private Sub Command2_Click ()
Dim Total1 As Integer
Total1 = Total1 + 1
Total2 = Total2 + 1
Total3 = Total3 + 1
End Sub
6
What are different properties of a
Text Box
7
Different controls of a Text Box
Name prefix is as txt, e. g. txtName
E.g.
Text1.BackColor = vbRed
Text1.FontSize = 20
Text1.Alignment = 2
Text1.ForeColor = vbYellow
Text1.BorderStyle = 0
Text1.FontBold = True
Text1.tooltiptext=“enter something”
Text1.text=Text1
8
Multi line property of Text boxes
Select the multi line
property as one of the
following:
True
false
Select the scroll bar option
Horizontal
Vertical
None
Both
9
Different controls of a label box
Name prefix is as lbl, e. g. lblName
E.g.
Label1.BackColor = vbRed
Label1.FontSize = 20
Label1.Alignment = 2
Label1.ForeColor = vbYellow
Label1.BorderStyle = 0
Label1.FontBold = True
Label1.ToolTipText = "enter
Something"
Label1.Caption = "HI"
10
Frame Control
Frame1.BackColor = vbRed
Frame1.FontSize = 20
Frame1.FontBold = True
Frame1.BorderStyle = 1
Frame1.Caption = "Hello"
Frame1.ToolTipText = "enter
Something"
11
Command button Control
Private Sub Command1_Click()
MsgBox "You have Clicked Submit
button", vbInformation
End Sub
Private Sub Form_Load()
Command1.ToolTipText = "Click here"
Command1.Caption = "Submit"
End Sub
12
Controls of Check boxes
Private Sub Check1_Click()
MsgBox "You have Clicked Assam"
End Sub
Private Sub Command1_Click()
Check1.BackColor = vbYellow
Check1.Caption = "Command1"
Check2.BackColor = vbBlack
Check2.ForeColor = vbWhite
End Sub
Private Sub Form_Load()
Check1.Caption = "Assam"
Check2.Caption = "Bihar"
End Sub
13
Controls of Option Button
Private Sub Command1_Click()
If Option1.Value = True Then
MsgBox "You have selected the first
option“
End If
If Option2.Value = True Then
MsgBox "You have selected the second
option"
End If
End Sub 14
Combo Box Control
Private Sub cmdAddNew_Click()
Combo1.AddItem Text1.Text
Text1.Text = ""
End Sub
Private Sub cmdClear_Click()
Combo1.Clear
End Sub
Private Sub Form_Load()
Combo1.BackColor = vbRed
Combo1.FontSize = 5
Combo1.ForeColor = vbWhite
Combo1.List(0) = "Assam"
Combo1.List(1) = "Bihar"
Combo1.List(2) = "Urissa"
Combo1.List(3) = "Uttar Pradesh"
End Sub
15
List Box Control
Private Sub cmdAddNew_Click()
List1.AddItem Text1.Text
Text1.Text = ""
End Sub
Private Sub cmdClear_Click()
List1.Clear
End Sub
Private Sub Form_Load()
List1.BackColor = vbRed
List1.FontSize = 5
List1.ForeColor = vbWhite
List1.List(0) = "Assam"
List1.List(1) = "Bihar"
List1.List(2) = "Urissa"
List1.List(3) = "Uttar Pradesh"
End Sub
16
IF-Else Condition
Private Sub cmdAdd_Click()
If txtFirstNumber = “ “ Then
MsgBox "Please Enter First Number"
txtFirstNumber.SetFocus
ElseIf txtNumberTwo = “ “ Then
MsgBox "Please Enter Second Number"
txtNumberTwo.SetFocus
ElseIf IsNumeric(txtFirstNumber) = False Then
MsgBox "Please Enter Numberic Value"
txtFirstNumber.SetFocus
ElseIf IsNumeric(txtNumberTwo) = False Then
MsgBox "Please Enter Numberic Value"
txtNumberTwo.SetFocus
Else
lblResult = Val(txtFirstNumber) + Val(txtNumberTwo)
End If
End Sub
Private Sub cmdClear_Click()
txtNumberTwo = ""
txtFirstNumber = ""
End Sub
17
SELECT CASE
Can be used as an alternative to the
If...Then...Else structure, especially when
many comparisons are involved.
Private Sub Command1_Click()
Select Case Val(txtEnterCase)
Case 1: lblResult = "Small"
Case 2: lblResult = "Medium"
Case 3: lblResult = "big"
Case 4: lblResult = "Extra Large"
Case Else
MsgBox "Invalid Choice"
End Select
End Sub
18
DO…..LOOP
Used to execute a block of statements an
unspecified number of times.
Do While condition
statements
Loop
First, the condition is tested; if condition is True,
then the statements are executed. When it gets to
the Loop it goes back to the Do and tests condition
again. If condition is False on the first pass, the
statements are never executed.
Dim i, j As Integer
Private Sub Command1_Click()
i = InputBox("Enter the limit", "DO-WHILE-LOOP", 5)
j=1
Do While j <= i
Form1.Print j;
j=j+1
Loop
End Sub 19
WHILE….WEND
Used to execute a block of statements an
unspecified number of times.
While condition
statements
Wend
First, the condition is tested; if condition is True,
then the statements are executed. When it gets to
the Loop it goes back to the Do and tests condition
again. If condition is False on the first pass, the
statements are never executed.
Dim i, j As Integer
Private Sub Command1_Click()
i = InputBox ("Enter the limit", "DO-WHILE-LOOP", 5)
j=1
While j <= i
Form1.Print j;
j=j+1
Wend
End Sub 20
FOR….NET
Private Sub Form_Load() 'display the numbers from 0 to 9 For i = 0 To 9
Text1.Text = i & ","
Next i End Sub
-----------------------------------------------------------------------------------------------------
Let us take a closer look at our for loop:
' For - for loop
' i - use i as our integer
' 0 - start value = 0
' To - between start and stop value
' 9 - stop value = 9
' Next - go to next step (if i < 9 then end for loop)
Description: ' The for loop will loop from the given start walue to the given stop value. ' The amount of loops
will then be 10 (first loop i=0).
21
Function procedure and Sub
Procedure in VB
Difference between Function & Procedure is
A procedure is a set of code that does the work but does not return a value whereas a
function accepts parameters and does the calculation and does return a value back.
How to write Function Procedure and Sub Procedure in Visual Basic
Sub tellOperator(ByVal task As String)
Dim stamp As Date
stamp = TimeofDay()
MsgBox ("Starting " & task & " at " & CStr(stamp))
End Sub
Private Sub Command1_Click()
Call tellOperator("Dwipen Laskar")
End Sub
Private Function TimeofDay()
TimeofDay = Time
End Function
22
How to Pass Array in a
Private Sub Command1_Click() Function
Dim x(3) As Integer 'Declare a Static Integer Array of 4 elements
x(0) = 10
x(1) = 20
x(2) = 30
x(3) = 40
Call AcceptArray(x) 'Call the procedure and pass the Array
End Sub
Private Sub AcceptArray(intArray() As Integer)
Dim obj As Variant
For Each obj In intArray
Form1.Print obj
Next
End Sub
Private Function ReturnArray() As Variant
Dim x(3) As Integer 'Declare a Static Integer Array of 4 elements
x(0) = 1
x(1) = 2
x(2) = 3
x(3) = 4
ReturnArray = x 'Pass the array back as a return value
End Function
Private Sub Command2_Click()
Dim retval As Variant
Dim obj
retval = ReturnArray 'Assign the return value to a Variant Variable
For Each obj In retval
Form1.Print obj
Next
End Sub
23
How to use Message Box
Used for displaying
messages
Prompt
Symbol
Button
title
24
How to use Input Box?
Used for taking inputs
from users
Prompt
Title
Default
X-position
Y-position
25
Horizontal/ Vertical Scroll Bar
Private Sub Form_Load()
HScroll1.Value = 0
HScroll1.Min = 0
HScroll1.Max = 100
VScroll1.Value = 0
VScroll1.Max = 100
VScroll1.Min = 0
Label1.Caption = HScroll1.Value
End Sub
Private Sub HScroll1_Change()
Label1 = ""
Label1 = HScroll1.Value
End Sub
Private Sub VScroll1_Change()
Label1 = ""
Label1 = VScroll1.Value
End Sub
26
How to include add ins and
Libraries
27
How to use the Timer?
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Text1.Text = Time
End Sub
28
How to use Month Viewer
Private Sub Form_Load()
MonthView1.BackColor = vbGreen
MonthView1.Appearance = ccFlat
MonthView1.ForeColor = vbRed
End Sub
Private Sub MonthView1_DateClick(ByVal
DateClicked As Date)
MsgBox "you have clicked " &
Format(MonthView1.Value, "dd- mm-yyyy"),
vbInformation
End Sub
29
How to use Date Picker
30
How to Create Manus
31
How to use Manus
Private Sub mnDisplay_Click()
MsgBox "Hello, You have Clicked
Display"
End Sub
32
How to use Common Dialog
Control
Private Sub mnDisplay_Click()
On Error GoTo err:
CommonDialog1.Action = 3
'display color dialog box
Form2.BackColor =
CommonDialog1.Color
Exit Sub
err:
MsgBox "Dialog is canceled"
End Sub
33
How to Use MS-Flex Grid
Private Sub Command1_Click()
MSFlexGrid1.TextMatrix(9, 2) =
Val(MSFlexGrid1.TextMatrix(1, 2)) +
Val(MSFlexGrid1.TextMatrix(2, 2)) +
Val(MSFlexGrid1.TextMatrix(3, 2))
End Sub
Private Sub Form_Load()
MSFlexGrid1.TextMatrix(9, 2) = 0
MSFlexGrid1.TextMatrix(0, 0) = "S. No"
MSFlexGrid1.TextMatrix(0, 1) = "Name"
MSFlexGrid1.TextMatrix(0, 2) = "Age"
MSFlexGrid1.TextMatrix(1, 0) = "1"
MSFlexGrid1.TextMatrix(1, 1) = "Dwipen"
MSFlexGrid1.TextMatrix(1, 2) = 29
MSFlexGrid1.TextMatrix(2, 0) = "2"
MSFlexGrid1.TextMatrix(2, 1) = "S Adarsh"
MSFlexGrid1.TextMatrix(2, 2) = 27
MSFlexGrid1.TextMatrix(3, 0) = "3"
MSFlexGrid1.TextMatrix(3, 1) = "Sachin"
MSFlexGrid1.TextMatrix(3, 2) = 56
MSFlexGrid1.TextMatrix(9, 0) = "Total"
MSFlexGrid1.TextMatrix(9, 1) = "Age"
End Sub
34