Professional Documents
Culture Documents
PESPT
1
Dept of CSE,
VB LAB by vm
Fourth Semester
Subject: CS407 VISUAL BASIC Lab
Contact Hrs/Week:6
Graded Exercises
2
Dept of CSE,
VB LAB by vm
3
Dept of CSE,
VB LAB by vm
4
Dept of CSE,
VB LAB by vm
PART – B
LIST OF PROGRAMS :
1 Write a program for the following
1.1 To draw text with the method Print
1.2 To draw line and rectangle with method Line
1.3 To draw circles, ellipses, sector arcs with method Circle
1.4 To demonstrate method Pset to draw shapes
PART C
1 1.1 Write a program to detect and handle a divide- by- zero error
1.2 Write a program to demonstrate nested OnError statement
1.3 Write a program that uses the Err object
1.4 Write a program that demonstrates the use of Resume statement
1.5 Write a program that demonstrates error handling in the call stack
1.6 Write a program to demonstrate rethrowing an error.
5
Dept of CSE,
VB LAB by vm
` 4 4.1 Write a program to open a random access file, define the record
format as given below :
ACCOUNT_NO NAME BALANCE
4.2 Write a program to write data to a random access file.
4.3 Write a program that reads through the file sequentially from a
random access file and displays only those records containing
balance > 50000
4.4 Write a program to accept ACCOUNT_NO from the user and read
his / her details from the random access file and display the same
on the screen.
4.5 Write a program for transaction processing of a bank's account
information. The program should update existing accounts,
adds
new accounts, deletes accounts and stores a listing of all current
accounts in a text for printing.
6
Dept of CSE,
VB LAB by vm
Shape
Point Circle
Shape Hierarchy
Create a shape, point and Circle Hierarchies.
Develop & interface a class called Ishape.Interface Ishape
contains three public methods - area,name,tosting******
Develop classes Cpoint & Ccircle each of which implement
Interfaces Ishapes.
5.5 Write a program to demonstrate the use of interfaces &
polymorphism to perform payroll calculation based on the type
of employee (refer 5.3)
5.6 Write a program to demonstrate a Class that raises an Event.
6 6.1 Create a MS-Word document using VB automation Active X
control :- Label Scrollbar.
1. Create an ActiveX control that is composed of two constituent
controls: a label & a scrollbar. This ActiveX control is called as
LabelScrollbar.
LabelScrollbar is a control that allows the user to scroll
through.
a range of values. The scrollbar value is diplayed in the label
portion of the
control.
6.2 ActiveX control:- Clock control
Build a digital clock ActiveX control, which contains two
constituent controls: A label & a timer. The control automatically
changes its background colour to match the background colour of
the container. The developer can set the clock's foreground colour
Resizing the control result in the clock's digit growing or shrinking.
Create an ActiveX DLL that provides one function procedure
6.3 call
Spellcheck word that uses MS-Word's Spellchecker.
Create a standard EXE application that uses the ActiveX DLL
to verify that it is working properly..
6.4 ActiveX EXEs
1. Create an Active EXE & Execute as a stand alone EXE.
2. Create an Active EXE and create another standard EXE to
control an Active EXE.
6.5 Friend Access
Write a program to demonstrate Friend access with in an
application .
7 7.1 ADO Data control & Data Grid control
1.Create a program that allows the user to enter the SQL queries
process the queries and display the results.
2. Demonstrate the hierarchical Flex grid control.
7.2 Data list and Data Combo controls.
1. Write a program to demonstrate some of the basic capabilities
of the data combo & data list controls.
2.lUse bound column property of the data combo and data list
controls update the field value of a record in another record
set.
7.3 Using Data Environment designer
1. Create a connection to a database.
2.Create command object.
7
Dept of CSE,
VB LAB by vm
hints :- prefix for the names of objects lbl for label object,cmd for command
button,txt for text box,frm for form,lst for list box,cbo for combobox,chk for
checkbox,
guidelines :-
do not forget to change the name of objects,make sure same name in code and design.
If private or sub procedure not defined error.then delete that objects do fresh one.
edited and prepared by
V MANJUNATH
NAME CAPTION
FORM1 A Simple
Program
Label1 Welcome
to visual
basic
8
Dept of CSE,
VB LAB by vm
NAME CAPTION
Form1 Printing
text on the
line
cmdDisplay Print
cmdExit Exit
Private Sub cmdDisplay_Click()
End Sub
End Sub
5 Design a GUI which accepts two integer number, compute their sum and
display the result. Use a appropriate labels, text boxes
and buttons. NAME CAPTION
Form1 Addition
Program
Label1 Enter an
integer
Label2 The sum is
cmdAdd Add
cmdExit Exit
txtInput Text field
empty
txtSum Text field
empty
9
Dept of CSE,
VB LAB by vm
6 Write a program that inputs two different integers using the function Input
Box
a Compare the three numbers using the if-then -else statement
b Use label for displaying the number1 = Number2 or number1#number2
c Use label for displaying the number1>number2 or number1<number2
d Provide Enter numbers and Exit button
10
Dept of CSE,
VB LAB by vm
Name Caption
Form1 Testing the
comparison
operators
lblDisplay1
lblDisplay2
lblDisplay3
lblDisplay4
cmdEnterNumbers Enter
Numbers
cmdExit Exit
11
Dept of CSE,
VB LAB by vm
End Sub
End Sub
12
Dept of CSE,
VB LAB by vm
Form Displaying
Private Sub cmdPrint_Click()
characters in a
Dim counter As Integer ' Declaration
label
Label1 Enter an
counter = txtInput.Text ' Get number of characters
integer(1-9) and
lblDisplay.Caption = "" ' Clear Label
press print
txtInput
' Repeat the statements between Do While and Loop
' until counter has a value of 0.
Do While counter > 0 lblDisplay
lblDisplay.Caption = lblDisplay.Caption & "#"
counter = counter - 1 ' Decrement number cmdPrint Print
Loop
End Sub
13
Dept of CSE,
VB LAB by vm
Dim counter As Integer ' Number of grades input Form Class Average
Dim grade As Integer ' Current grade Program
Dim average As Single ' Floating-point average Label1 Press Enter
Dim message As String ' Text displayed in Label Grades To Start
End Sub
14
Dept of CSE,
VB LAB by vm
15
Dept of CSE,
VB LAB by vm
End Sub
Form Do/loop
Private Sub cmdPrint_Click()
while
Dim counter As Integer
cmdPrint Print
counter = 1
Do
Print counter & Space$(2);
counter = counter + 1
Loop While counter <= 10
Print
End Sub
16
Dept of CSE,
VB LAB by vm
Do
Print counter & Space$(2);
counter = counter + 1
Loop Until counter = 10
Print
End Sub
x=1
Do
17
Dept of CSE,
VB LAB by vm
If x = 5 Then
If x = 5 Then
Exit For ' Exit loop only if x = 5
End If
18
Dept of CSE,
VB LAB by vm
Do Until x = 10
bool = x ' Implicit conversion
Print x & vbTab & bool ' Print True or False
x=x+1 ' Increment x
Loop
End Sub
19
Dept of CSE,
VB LAB by vm
20
Dept of CSE,
VB LAB by vm
OR
21
Dept of CSE,
VB LAB by vm
22
Dept of CSE,
VB LAB by vm
23
Dept of CSE,
VB LAB by vm
24
Dept of CSE,
VB LAB by vm
For x = 5 To -1 Step -1
Form Demonstrating
Call PrintNumbers(x)
Exit Sub
Next x
cmdBegin Print
cmdBegin.Enabled = False
End Sub
End Sub
25
Dept of CSE,
VB LAB by vm
If d = 0 Then
Exit Function ' Exit the function returns ""
Print "After Exit Function Line" ' Never executed
Else
Divide = "Division yields " & n / d
End If
End Function
26
Dept of CSE,
VB LAB by vm
Frmscope Scoping
Example
' Scoping example
Option Explicit ' General declaration
Dim x As Integer ' Module variable in general declaration
cmdGo Go
Private Sub cmdGo_Click()
Dim x As Integer ' Local variable
x=6
Print "Local x is " & x & " on entering cmdGo_click()"
Print
27
Dept of CSE,
VB LAB by vm
Call Two
Call Three
x = 26
Print "Local x is " & x & " on entering One"
x=x+1
Print "Local x is " & x & " on exiting One"
Print
End Sub
28
Dept of CSE,
VB LAB by vm
Sub four()
frmScope.Print "global x is " & x% & " on entering four"
x% = x% + 1
frmScope.Print "global x is " & x% & " on exiting four"
frmScope.Print
End Sub
29
Dept of CSE,
VB LAB by vm
For x = 0 To n
lstValues.AddItem Format$(x, "@@") & "! = " & _
Form Factorial Program
Factorial((x)) ' Call-by-value
Next x
Label1 Enter an integer
End Sub
txtInput
Private Function Factorial(ByVal y As Double) As Double
End Function
Form Fibonacci
Numbers
Private Sub cmdCalculate_Click()
Dim fibonacciValue As Variant Label1 Enter an integer
30
Dept of CSE,
VB LAB by vm
Screen.MousePointer = vbDefault
End Sub
If (n = 0 Or n = 1) Then
Fibonacci = n ' Base Case
Else
' Recursive step
Fibonacci = Fibonacci(n - 1) + Fibonacci(n - 2)
End If
End Function
Form Demonstrating
Optical
Private Sub cmdPrint_Click() Arguments
Call Cls cmdPrint Print
31
Dept of CSE,
VB LAB by vm
Print "Dimensions: " & x & ", " & y & ", " & z & Space$(5) & _
"Volume: " & (x * y * z)
End Sub
cmdPrint Print
32
Dept of CSE,
VB LAB by vm
‘Another module
End Sub
33
Dept of CSE,
VB LAB by vm
End Sub
34
Dept of CSE,
VB LAB by vm
End Sub
Call Cls
Call Randomize
35
Dept of CSE,
VB LAB by vm
cmdPrint.Enabled = False
End Sub
36
Dept of CSE,
VB LAB by vm
cmdElement Element
Private Sub cmdElement_Click()
Dim x As Integer cmdExit Exit
Call PrintModified
End Sub
End Sub
37
Dept of CSE,
VB LAB by vm
End Sub
End Sub
38
Dept of CSE,
VB LAB by vm
Option Explicit
Option Base 1
Dim mArray(10) As Integer
For x = 1 To UBound(mArray)
Call lstSorted.AddItem(mArray(x))
Next x
cmdSort.Enabled = False
End Sub
‘IN MODULE 1
' Module modBubble.bas
Option Explicit
39
Dept of CSE,
VB LAB by vm
Next compare
Next pass
End Sub
A Linear search
Write a program to demonstrate Linear search technique.
40
Dept of CSE,
VB LAB by vm
lblResult.Caption = ""
searchKey = txtKey.Text
End Sub
Call Randomize
Call lstData.Clear
lblResult.Caption = ""
‘IN MODULE 1
' Code module modLinear.bas
Option Explicit
41
Dept of CSE,
VB LAB by vm
Next x
42
Dept of CSE,
VB LAB by vm
Option Explicit
Option Base 1
Dim mArray(15) As Integer
mLowBound = LBound(mArray)
mUpperBound = UBound(mArray)
End Sub
Call Cls
Call BinarySearch
End Sub
low = mLowBound
high = mUpperBound
Call PrintHeader
searchKey = txtKey.Text
43
Dept of CSE,
VB LAB by vm
Loop
Print "Indexes:"
For x = mLowBound To mUpperBound
Print Format$(x, "!@@@@");
Next x
Print
For x = mLowBound To 4 * mUpperBound
Print "-";
Next x
Print
End Sub
Next x
Print
End Sub
44
Dept of CSE,
VB LAB by vm
End Sub
45
Dept of CSE,
VB LAB by vm
cmdDisplay.Enabled = False
Call PrintArray(mArray)
End Sub
Call lstDisplay.AddItem(temp)
Next row
End Sub
46
Dept of CSE,
VB LAB by vm
Call lstValues.Clear
arrayLength = txtInput.Text
cmdErase 47 Erase
Dept of CSE,
chkPreserve preserve
(checkbox)
VB LAB by vm
End Sub
End Sub
48
Dept of CSE,
VB LAB by vm
End Sub
49
Dept of CSE,
VB LAB by vm
Print
v = Array("hello", "bye", "hi") ' Return Variant array
Print "Variant array values are: ";
For x = LBound(v) To UBound(v)
Print v(x) & Space$(2);
Next x
Print
v = Array(1.1, 2.2, 3.3, 4.4) ' Return Variant array
Print "Variant array values are: ";
For x = LBound(v) To UBound(v)
Print v(x) & Space$(2);
Next x
cmdPrint.Enabled = False
End Sub
50
Dept of CSE,
VB LAB by vm
Call lstOutput.Clear
51
Dept of CSE,
VB LAB by vm
End Sub
Label1 String
Label2 Pattern
Label3 Pattern
Descriptions
lblResult
txtString
' Demonstrating operator Like
Option Explicit txtPattern
Lstpattern
(listbox) 52
Dept of CSE,
VB LAB by vm
End Sub
53
Dept of CSE,
VB LAB by vm
End Sub
54
Dept of CSE,
VB LAB by vm
empty
cmdconvert Convert to pig latin
txtOutput.Text = ""
phrase = txtInput.Text
blankPosition = InStr(1, phrase, " ")
Call DisplayLatinWord(nextWord)
phrase = Right$(phrase, Len(phrase) - blankPosition)
blankPosition = InStr(1, phrase, " ")
Wend
nextWord = phrase
Call DisplayLatinWord(nextWord)
End Sub
55
Dept of CSE,
VB LAB by vm
56
Dept of CSE,
VB LAB by vm
Form
Label1 Enter string with
leading and
trailing spaces
Txtinput
Private Sub cmdTrim_Click() cmdTrim Trim space
Call lstOutput.Clear characters
Call lstOutput.AddItem("***" & LTrim$
(txtInput.Text) & _ LstOutput
"***")
Call lstOutput.AddItem("***" & RTrim$
(txtInput.Text) & _
"***")
Call lstOutput.AddItem("***" & Trim$(txtInput.Text) & _
"***")
End Sub
Form Using
string and
space
lstOutput
57
Dept of CSE,
VB LAB by vm
Form Replacing
substrings
in a string
Label1 Enter
phrase
txtInput
Form Reversing
a string
Private Sub cmdReverse_Click() with
txtOutput.Text = StrReverse(txtInput.Text) strReverse
End Sub Label1 The
reversed
string is
h. write a program to convert string to all txtInput
upper case letters using Ucase $ and all
Cmdreverse Reverse
lowercase letters using Lcase $
the string
txtOutput
58
Dept of CSE,
VB LAB by vm
Form Cinverting
strings to
upper and
lower
case
Label1 Enter
phrase
txtInput
Private Sub cmdConvert_Click() cmdConvert Convert
txtUpperCase.Text = UCase$(txtInput.Text)
txtLowerCase.Text = LCase$(txtInput.Text) Label2 The
End Sub phrase in
uppercase
is
Label3 The
phrase in
lower
case is
34 a Write a program which txtUppercase
demonstrate the use of conversion txtLowercase
function Asc and Chr $
Form Demonstrating
Asc and Chr
Label1 Enter an
integer to
convert to a
character
txtInputInt
cmdConvertChar Show
corresponding
character
txtOutputChar
Private Sub cmdConvertInt_Click()
txtOutputChar.Text = _ Label2 Enter a
"The character value of " & txtInputInt.Text & character to
_ convert to an
" is " & Chr$(txtInputInt.Text) integer
End Sub cmdConvertInt Show
corredponding
59 integer
Dept of CSE,
txtOutputInt
txtInputChar
VB LAB by vm
Form Using
IsNumeric,V
al and Str$
Label1 Enter a
string
cmdIsNum IsNumeric?
eric
Private Sub cmdIsNumeric_Click()
If IsNumeric(txtInput.Text) Then
lblOutput.Caption = txtInput.Text & " + 10 is " & _
Str$(Val(txtInput.Text) + 10)
Else
lblOutput.Caption = txtInput.Text & " is not a number"
End If
End Sub
60
Dept of CSE,
VB LAB by vm
Form Demostra_
ting Hex
and Oct
Label1 Enter a
decimal
values to
convert
txtInput
cmdConvert Convert to
Hex and
Oct
Label2 Hex value
Label3 octValue
txtHex
txtOct
61
Dept of CSE,
VB LAB by vm
62
Dept of CSE,
VB LAB by vm
Format$(123456.789, "Currency"))
Call lstOutput.AddItem("Fixed: " & _
Format$(123456.789, "Fixed"))
Call lstOutput.AddItem("Standard: " & _ Form Demonstrationg
Format$(123456.789, "Standard")) named numeric
Call lstOutput.AddItem("Scientific: " & _ formats
Format$(123456.789, "Scientific"))
Call lstOutput.AddItem("Percent: " & _
Format$(0.05475, "Percent"))
lstOuput
Call lstOutput.AddItem("Yes/No: " & _
Format$(0, "Yes/No"))
Call lstOutput.AddItem("Yes/No: " & _
Format$(1, "Yes/No"))
Call lstOutput.AddItem("True/False: " & _
Format$(0, "True/False"))
Call lstOutput.AddItem("True/False: " & _
Format$(1, "True/False"))
Call lstOutput.AddItem("On/Off: " & _
Format$(0, "On/Off"))
Call lstOutput.AddItem("On/Off: " & _
Format$(1, "On/Off"))
End Sub
63
Dept of CSE,
VB LAB by vm
64
Dept of CSE,
VB LAB by vm
Format$(-1123456.789, "#,##0.00"))
Call lstOutput.AddItem("#,##0.00: " & _
Format$(0.789, "#,##0.00"))
Call lstOutput.AddItem("$#,##0.00: " & _
Format$(1123456.789, "$#,##0.00"))
Call lstOutput.AddItem("$#,##0.00: " & _
Format$(-1123456.789, "$#,##0.00"))
Call lstOutput.AddItem("$#,##0.00: " & _
Format$(0.789, "$#,##0.00"))
Call lstOutput.AddItem("0%: " & Format$(2.789, "0%"))
Call lstOutput.AddItem("0%: " & Format$(-2.789, "0%"))
Call lstOutput.AddItem("0%: " & Format$(0.789, "0%"))
Call lstOutput.AddItem("0.00%: " & _
Format$(2.789, "0.00%"))
Call lstOutput.AddItem("0.00%: " & _
Format$(-2.789, "0.00%"))
Call lstOutput.AddItem("0.00%: " & _
Format$(0.789, "0.00%"))
Call lstOutput.AddItem("0.00E+00: " & _
Format$(1123456.789, "0.00E+00"))
Call lstOutput.AddItem("0.00E+00: " & _
Format$(-1123456.789, "0.00E+00"))
Call lstOutput.AddItem("0.00E+00: " & _
Format$(0.789, "0.00E+00"))
Call lstOutput.AddItem("0.00E-00: " & _
Format$(1123456.789, "0.00E-00"))
Call lstOutput.AddItem("0.00E-00: " & _
Format$(-1123456.789, "0.00E-00"))
Call lstOutput.AddItem("0.00E-00: " & _
Format$(0.789, "0.00E-00"))
Call lstOutput.AddItem("$#,##0.00;($#,##0.00): " & _
Format$(1123456.789, "$#,##0.00;($#,##0.00)"))
Call lstOutput.AddItem("$#,##0.00;($#,##0.00): " & _
Format$(-1123456.789, "$#,##0.00;($#,##0.00)"))
End Sub
65
Dept of CSE,
VB LAB by vm
Form Formatting
numbers
Label1 Enter
Private Sub cmdFormat_Click() value to
Dim numDigits As Integer, leadDigit As Boolean, _ format
parens As Boolean, group As Boolean txtInput
Form Formatting
numbers
Label1 Enter value
to format
txtInput
txtOutput
cmdFormat
Label2 Number of
digits after
Private Sub cmdFormat_Click() decimal
point
cboNumdigits
chkLeaddigits 66
Dept of CSE,
chkParenthesese
chkGroupdigits
VB LAB by vm
numDigits = cboNumDigits.Text
leadDigit = chkLeadDigit.Value
parens = chkParentheses.Value
group = chkGroupDigits.Value
txtOutput.Text = FormatCurrency(txtInput.Text, _
numDigits, leadDigit, parens, group)
End Sub
Form Formatting
numbers
Label1 Enter
value to
format
txtInput
txtOutput
cmdFormat
Label2 Number of
digits after
decimal
point
Private Sub cmdFormat_Click() cboNumdigits
Dim numDigits As Integer, leadDigit As chkLeaddigits
Boolean, _
parens As Boolean, group As Boolean chkParenthesese
chkGroupdigits
numDigits = cboNumDigits.Text
leadDigit = chkLeadDigit.Value
parens = chkParentheses.Value
group = chkGroupDigits.Value
txtOutput.Text = FormatPercent(txtInput.Text, _
numDigits, leadDigit, parens, group)
End Sub
67
Dept of CSE,
VB LAB by vm
68
Dept of CSE,
VB LAB by vm
Format(IsDate(123456), "True/False"))
Call lstOutput.AddItem("IsDate(2/15/73): " & _
Format(IsDate("2/15/73"), "True/False"))
Call lstOutput.AddItem("DateValue(2-15-73): " & _
DateValue("2-15-73")) Form Demonstrating
Call lstOutput.AddItem("DateValue(February 15, IsDate
1973): " & _ ,DateValue
DateValue("February 15, 1973")) and dateserial
Call lstOutput.AddItem("DateValue(15-Feb-73): " & lstOutput
_
DateValue("15-Feb-73"))
Call lstOutput.AddItem("DateSerial(1998, 8, 2): " &
_
DateSerial(1998, 8, 2))
End Sub
69
Dept of CSE,
VB LAB by vm
Form Demonstrating
date functions
lstOutput
70
Dept of CSE,
VB LAB by vm
71
Dept of CSE,
VB LAB by vm
72
Dept of CSE,
VB LAB by vm
73
Dept of CSE,
VB LAB by vm
a(0) = "Java"
Print
Erase b
Print "Filtering for strings other than: " & Chr$(34) & _
"Visual Basic 6" & Chr$(34)
b = Filter(a, "Visual Basic 6", False)
Call PrintStrings(b)
End Sub
For z = 0 To UBound(c)
Print c(z)
Next z
74
Dept of CSE,
VB LAB by vm
End Sub
End Sub
75
Dept of CSE,
VB LAB by vm
form Compound
interest
cmdcalculate calculate
cmdexit exit
Txtamount
Txtinterest
lstdisplay
Label1 Interest rate %
Label2 Enter principal
76
Dept of CSE,
VB LAB by vm
For years = 1 To 10
amount = principal * (1 + interestRate) ^ years
lstDisplay.AddItem Format$(years, "@@@@") & vbTab & _
Format$(Format$(amount, "Currency"), _
String$(17, "@"))
Next years
End Sub
77
Dept of CSE,
VB LAB by vm
78
Dept of CSE,
VB LAB by vm
End Select
80
Dept of CSE,
VB LAB by vm
PART-B
1 Write a program for the following
Form Printing to
Private Sub cmdPrint_Click()
Call Randomize the form
cmdPrint print
' Randomly pick values to draw at
CurrentX = Rnd() * Width
CurrentY = Rnd() * Height
81
Dept of CSE,
VB LAB by vm
Call Randomize
End Sub
82
Dept of CSE,
VB LAB by vm
Call Randomize
End Sub
83
Dept of CSE,
VB LAB by vm
End Sub
Form Demonstrating
method pset
Private Sub DrawShape()
Dim x As Single, y As Single
Dim totalRadians As Single, r As Single
Dim a As Single, theta As Single
Call Randomize
Scale (3, -3)-(-3, 3) ' Change scale
totalRadians = 8 * Atn(1) ' Circle in Radians
End Sub
2
2 .1Write a program to draw circle of random sizes in picture box.
The user specifies the Drawmode , Drawstyle and Fillstyle values.
84
Dept of CSE,
VB LAB by vm
v = txtDrawStyle.Text
85
Dept of CSE,
VB LAB by vm
txtDrawStyle.Text = vbSolid
End If
v = txtFillStyle.Text
2.2 Write a program which allows the user to change the width of a
v = txtStyle.Text
86
Dept of CSE,
VB LAB by vm
v = txtWidth.Text
End Sub
Form Shape
control
Shape In shape
(shape make oval,
object) Rectangle.
Circle.
Change borderwidth
ans :
put shape object from object window
87
Dept of CSE,
VB LAB by vm
Call Randomize
Scale (3, -3)-(-3, 3) ' Change scale
totalRadians = 8 * Atn(1) ' Circle in Radians
End Sub
88
Dept of CSE,
VB LAB by vm
End Sub
Form Textboxes
txtInput Enter
3 some text
here…
txtOutput
89
Dept of CSE,
VB LAB by vm
Form Combobox
cboCombo
Label1 comboBox
item
cmdRemove Remove
cmdClear Clear
CmdAdd Add
Private Sub cmdInput_Click()
lblOutput
txtinput
' Add item to cboCombo drop-down list
Call cboCombo.AddItem(txtInput.Text)
90
Dept of CSE,
VB LAB by vm
Form listBox
lstList
txtInput
cmdAdd Add
cmdRemove Remove
cmdClear Clear
cmdExit Exit
91
Dept of CSE,
VB LAB by vm
End Sub
92
Dept of CSE,
VB LAB by vm
Form Slider
3.7 Write a program to create a pop - up menu using the MenuEditor. The form
contains a Label displaying the word Text. Right clicking the form should display
a pop-up menu from which the user can select either Right, Center or left
to justify Text.
93
Dept of CSE,
VB LAB by vm
End Sub
Form Function
MsgBox
Frame1 Button type
Frame2 Icon
Frame3 Modal
Opttype(0) vbOKOnly
Opttype(1) vbOKCancel
Opttype(2) vbAbortRetry
Ignore
Opttype( vbYesNoCancel
3)
vbYesNo
Opttype(4
)
Opttype(5 vbRetryCancel
)
Opticon(0) vbCritical
94
Dept of CSE,
VB LAB by vm
optType(mButtonType).Value = True
optIcon(mButtonIcon).Value = True
optModal(mModal).Value = True
End Sub
End Sub
PART-C
95
Dept of CSE,
VB LAB by vm
1
1.1 Write a program to detect and handle a divide- by- zero error
numerator = txtInput(0).Text
denominator = txtInput(1).Text
End Sub
inputHandler:
lblResult.Caption = "Attempted non-numeric input!"
End Sub
97
Dept of CSE,
VB LAB by vm
errorHandler:
Print "Source: " & Err.Source
Print "Error: ";
form Demonstrating
resume
Frame1 Error handling
response
optResume Enter
denominator
optResumeNext
optResumeLabel
cmdgo go
98
Dept of CSE,
VB LAB by vm
resumeLabel:
s = "Visual Basic How to Program!"
Print s
handler:
If optResume.Value Then
Print "Resume: ";
Resume ' Repeat line that raised error
ElseIf optResumeNext.Value Then
Print "Resume Next: ";
Resume Next ' Resume at next line after error raising line
Else ' optResumeLabel
Print "Label: ";
Resume resumeLabel ' Resume at label resumeLabel
End If
End Sub
99
Dept of CSE,
VB LAB by vm
1.5 Write a program that demonstrates error handling in the call stack
Call initializeFlags
mFlags(n) = True
End Sub
If mFlags(0) Then
On Error GoTo handler
Else
On Error GoTo 0 ' Disable error handler
End If
handler:
lblLocation.Caption = "Error handled in cmdBegin_Click"
imgImage(0).Picture = LoadPicture("images" & _
"\resume3.gif")
Resume Next ' Exit Sub statement
End Sub
If mFlags(1) Then
On Error GoTo handlerOne
Else
On Error GoTo 0 ' Disable error handler
End If
handlerOne:
lblLocation.Caption = "Error handled in one"
imgImage(1).Picture = LoadPicture("images" & _
"\resume3.gif")
Resume Next ' Exit Sub statement
End Sub
If mFlags(2) Then
On Error GoTo handlerTwo
Else
On Error GoTo 0 ' Disable error handler
End If
handlerTwo:
lblLocation.Caption = "Error handled in two"
imgImage(2).Picture = LoadPicture("images" & _
"\resume3.gif")
Resume Next ' Exit Sub statement
End Sub
101
Dept of CSE,
VB LAB by vm
If mFlags(3) Then
On Error GoTo handlerThree
Else
On Error GoTo 0 ' Disable error handler
End If
handlerThree:
lblLocation.Caption = "Error handled in three"
imgImage(3).Picture = LoadPicture("images" & _
"\resume3.gif")
Resume Next ' Exit Sub statement
End Sub
If mFlags(4) Then
On Error GoTo handlerFour
Else
On Error GoTo 0 ' Disable error handler
End If
102
Dept of CSE,
VB LAB by vm
form Rethrowing an
error
Cmdinput input
handler:
Print "cmdRethrow_Click's error handler executed!"
Resume ' Resume at line that raised error
End Sub
localHandler:
103
Dept of CSE,
VB LAB by vm
End Sub
104
Dept of CSE,
VB LAB by vm
errorhandler:
Dim message As String
Else
Call MsgBox(Err.Description, vbOKOnly + vbExclamation)
Resume Next
End If
End Sub
105
Dept of CSE,
VB LAB by vm
errorhandler:
Dim message As String
Else
Call MsgBox(Err.Description, vbOKOnly + vbExclamation)
Resume Next
End If
End Sub
106
Dept of CSE,
VB LAB by vm
107
Dept of CSE,
VB LAB by vm
108
Dept of CSE,
VB LAB by vm
109
Dept of CSE,
VB LAB by vm
errhandler:
Call MsgBox(Err.Description, vbCritical, "ERROR")
drvBox.Drive = drvBox.List(1)
Exit Sub
End Sub
110
Dept of CSE,
VB LAB by vm
111
Dept of CSE,
VB LAB by vm
112
Dept of CSE,
VB LAB by vm
handler:
End Sub
txtDisplay.Text = "Accounts:"
113
Dept of CSE,
VB LAB by vm
4
4.1 Write a program to open a random access file, define the record
format as given below :
ACCOUNT_NO NAME BALANCE
Sub cmdInitialize_Click()
Dim recordLength As Long, x As Integer
Dim udtBlankClient As ClientRecord ' user defined type
Dim filename As String
dlgOpen.ShowOpen
filename = dlgOpen.filename
114
Dept of CSE,
VB LAB by vm
Len = recordLength
For x = 1 To 100
Put #1, x, udtBlankClient ' Write empty records
Next
Sub cmdExit_Click()
End
End Sub
Sub cmdOpenFile_Click()
Dim recordLength As Long
Dim filename As String
115
Dept of CSE,
VB LAB by vm
dlgOpen.ShowOpen
filename = dlgOpen.filename
Call ClearFields
End Sub
Sub cmdDone_Click()
Close #1
cmdOpenFile.Enabled = True
cmdEnter.Enabled = False
cmdDone.Enabled = False
End Sub
4.3 Write a program that reads through the file sequentially from a
random access file and displays only those records containing
balance > 50000
116
Dept of CSE,
VB LAB by vm
Sub cmdOpenFile_Click()
Dim recordLength As Long
Dim filename As String
dlgOpen.ShowOpen
filename = dlgOpen.filename
If EOF(1) Then
cmdNext.Enabled = False
117
Dept of CSE,
VB LAB by vm
Exit Sub
End If
Sub cmdDone_Click()
Close #1
cmdOpenFile.Enabled = True
cmdNext.Enabled = False
cmdDone.Enabled = False
txtAccount.Text = ""
txtFirstName.Text = ""
txtLastName.Text = ""
txtBalance.Text = ""
End Sub
4.4 Write a program to accept ACCOUNT_NO from the user and read
his / her details from the random access file and display the same
on the screen.
118
Dept of CSE,
VB LAB by vm
Sub cmdOpenFile_Click()
Dim recordLength As Long
Dim filename As String
dlgOpen.ShowOpen
filename = dlgOpen.filename
Sub cmdDone_Click()
Close #1
cmdOpenFile.Enabled = True
cmdGet.Enabled = False
cmdDone.Enabled = False
txtAccount.Text = ""
txtFirstName.Text = ""
txtLastName.Text = ""
txtBalance.Text = ""
End Sub
119
Dept of CSE,
VB LAB by vm
Sub cmdOpenFile_Click()
Dim recordLength As Long
Dim filename As String
dlgOpen.ShowOpen
filename = dlgOpen.filename
120
Dept of CSE,
VB LAB by vm
End Sub
End If
Close #2
Else
MsgBox ("You must specify a file name")
End If
End Sub
If mUdtClient.accountNumber = 0 Then
mUdtClient.accountNumber = Val(txtNewAccount)
mUdtClient.firstName = txtNewFirstName.Text
mUdtClient.lastName = txtNewLastName.Text
mUdtClient.balance = txtNewBalance.Text
Put #1, mUdtClient.accountNumber, mUdtClient
MsgBox ("Account " & mUdtClient.accountNumber & _
" has been added to the file")
Else
MsgBox ("Account already exists")
End If
Else
MsgBox ("You must enter an account number")
121
Dept of CSE,
VB LAB by vm
End If
End Sub
End Sub
Sub cmdCloseFile_Click()
Close #1
cmdOpenFile.Enabled = True
cmdCloseFile.Enabled = False
End Sub
122
Dept of CSE,
VB LAB by vm
IN THE MODULE
' Class definition for CTime1
123
Dept of CSE,
VB LAB by vm
Option Explicit
124
Dept of CSE,
VB LAB by vm
If mTime.Second = 0 Then
mTime.Minute = (mTime.Minute + 1) Mod 60
If mTime.Minute = 0 Then
mTime.Hour = (mTime.Hour + 1) Mod 24
End If
End If
Call UpdateDisplay
End Sub
IN MODULE
' Class definition for CTime2
' This class enhances CTime1 by providing Property procedures
' for Hour, Minute and Second properties
Option Explicit
mHour = ValidateHour(h)
mMinute = ValidateMinute(m)
mSecond = ValidateSecond(s)
End Sub
125
Dept of CSE,
VB LAB by vm
Format$(mSecond, "00")
End Function
126
Dept of CSE,
VB LAB by vm
With employee1
.Firstname = "Bob"
.LastName = "Jones"
Call .BirthDate.SetDate(9, 8, 1965)
Call .HireDate.SetDate(2, 17, 1992)
End With
employee2.Firstname = "Susan"
employee2.LastName = "Baker"
Print employee1.ToString()
Print employee2.ToString()
End Sub
‘IN MODULE1
' Class CDate1 definition
Option Explicit
127
Dept of CSE,
VB LAB by vm
daysPerMonth = Array(0, 31, 28, 31, 30, 31, 30, 31, 31, _
30, 31, 30, 31)
‘IN MODULE2
' Class CEmployee definition
Option Explicit
128
Dept of CSE,
VB LAB by vm
129
Dept of CSE,
VB LAB by vm
‘IN MODULE1
' CCircle class definition
Option Explicit
Implements IShape
130
Dept of CSE,
VB LAB by vm
End Property
‘IN MODULE 2
Private mX As Integer
Private mY As Integer
‘IN MODULE 3
131
Dept of CSE,
VB LAB by vm
For x = 1 To 5
Call mEventRaiser.RaisesAnEvent
Next x
End Sub
‘IN MODULE 1
' Class that raises an event
Option Explicit
132
Dept of CSE,
VB LAB by vm
6
6.1 Create a MS-Word document using VB automation Active X
control :- Label Scrollbar.
133
Dept of CSE,
VB LAB by vm
''''''''''''''''''''''''''''''''''''''
' Event procedures for UserControl '
''''''''''''''''''''''''''''''''''''''
Private Sub UserControl_InitProperties()
With hsbScroll
.Min = 0
.Max = 32767
.Value = (Min + Max) / 2
.SmallChange = 1
.LargeChange = 5
End With
lblDisplay.Caption = hsbScroll.Value
End Sub
With hsbScroll
.Value = PropBag.ReadProperty("Value", (Min + Max) \ 2)
.Min = PropBag.ReadProperty("Min", 0)
.Max = PropBag.ReadProperty("Max", 32767)
.LargeChange = PropBag.ReadProperty("LargeChange", 5)
.SmallChange = PropBag.ReadProperty("SmallChange", 1)
End With
lblDisplay.Caption = PropBag.ReadProperty("Caption", _
(Min + Max) \ 2)
End Sub
With hsbScroll
.Left = lblDisplay.Width
134
Dept of CSE,
VB LAB by vm
lblDisplay.Height = ScaleHeight
End Sub
''''''''''''''''''''''''''''''''''''''
' Event procedures for hsbScroll '
''''''''''''''''''''''''''''''''''''''
Private Sub hsbScroll_Change()
lblDisplay.Caption = hsbScroll.Value
RaiseEvent Change
End Sub
''''''''''''''''''''''''''''''''''''''
' Property methods for hsbScroll '
''''''''''''''''''''''''''''''''''''''
Public Property Get Value() As Long
Value = hsbScroll.Value
End Property
135
Dept of CSE,
VB LAB by vm
Call UserControl.PropertyChanged("SmallChange")
End Property
'''''''''''''''''''''''''''''''''''
' UserControl event procedures '
'''''''''''''''''''''''''''''''''''
Private Sub UserControl_Show()
BackColor = Ambient.BackColor
End Sub
End Sub
'''''''''''''''''''''''''''''''''''
' UserControl property methods '
'''''''''''''''''''''''''''''''''''
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End Property
'''''''''''''''''''''''''''''''''''
' Timer event procedure '
'''''''''''''''''''''''''''''''''''
Private Sub tmrTimer_Timer()
Call Cls
Print Abs(Hour(Now) - 12) & ":";
Print Format$(Minute(Now), "00") & ":";
136
Dept of CSE,
VB LAB by vm
6.3 Create an ActiveX DLL that provides one function procedure call
Spellcheck word that uses MS-Word's Spellchecker.
Create a standard EXE application that uses the ActiveX DLL
to verify that it is working properly..
137
Dept of CSE,
VB LAB by vm
If mChecker.SpellCheckWord(txtInput.Text) Then
lblDisplay.Caption = txtInput.Text & _
" is spelled correctly."
Else
lblDisplay.Caption = txtInput.Text & _
" is spelled incorrectly."
End If
End Sub
138
Dept of CSE,
VB LAB by vm
End Sub
If mChecker.SpellCheckWord(txtInput.Text) Then
lblDisplay.Caption = txtInput.Text & _
" is spelled correctly."
Else
lblDisplay.Caption = txtInput.Text & _
" is spelled incorrectly."
End If
End Sub
139
Dept of CSE,
VB LAB by vm
Option Explicit
Dim bookQuery, allInfoQuery As String
140
Dept of CSE,
VB LAB by vm
2.lUse bound column property of the data combo and data list
controls update the field value of a record in another record set.
141
Dept of CSE,
VB LAB by vm
cmdPrevious.Enabled = False
End If
End Sub
“Best Wishes”
Part-c
142
Dept of CSE,
VB LAB by vm
1.4,3.1,3.5,3.7,4. 1.5,3.2,3.6,4.2,4.3,
1
4.4,4.5,5.1,5.2,5.3
5.4,all 6.1 to 6.6 ,
143
Dept of CSE,