100% found this document useful (1 vote)
4K views67 pages

Program No-01 Aim: WAP To Perform Arithmetic Operation Using Command Button. Code

The document contains code for 5 programs written in Visual Basic. Program 1 allows arithmetic operations using command buttons for addition, subtraction, multiplication and division. Program 2 calculates simple and compound interest given principal, rate and time. Program 3 generates multiplication tables for a given number. Program 4 simulates a mark sheet to calculate grades based on marks input in 5 subjects. Program 5 creates a basic calculator application.

Uploaded by

manisha Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
4K views67 pages

Program No-01 Aim: WAP To Perform Arithmetic Operation Using Command Button. Code

The document contains code for 5 programs written in Visual Basic. Program 1 allows arithmetic operations using command buttons for addition, subtraction, multiplication and division. Program 2 calculates simple and compound interest given principal, rate and time. Program 3 generates multiplication tables for a given number. Program 4 simulates a mark sheet to calculate grades based on marks input in 5 subjects. Program 5 creates a basic calculator application.

Uploaded by

manisha Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Department of Computer Science

Rungta College of Science & Technology, Durg

Program no-01
Aim: WAP to perform arithmetic operation using command button.
Code :
Dim a, b, c As Integer
Dim d, e, f As Double

Private Sub Command1_Click()


a = Val(InputBox("Enter any number", "Addition", 0))
b = Val(InputBox("Enter any number", "Addition", 0))
c=a+b
MsgBox (c)
End Sub

Private Sub Command2_Click()


a = Val(InputBox("Enter any number", "Subtraction", 0))
b = Val(InputBox("Enter any number", "Subtraction", 0))
c=a-b
MsgBox (c)
End Sub

Private Sub Command3_Click()


a = Val(InputBox("Enter any number", "Multiplication", 0))
b = Val(InputBox("Enter any number", "Multiplication", 0))
c=a*b
MsgBox (c)
End Sub

Private Sub Command4_Click()


d = Val(InputBox("Enter any number", "Division", 0))
e = Val(InputBox("Enter any number", "Division", 0))
f = (d / e)
MsgBox (f)
End

Lab Manual: CS-(Visual Basic)


Department of Computer Science
Rungta College of Science & Technology, Durg

Output :

2
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-02
Aim: WAP to take of principal, rate & time and calculate simple interest & compound
interest

Code :

Dim p, r, t, si As Double
Dim a As String

Private Sub Command1_Click()


si = p * r * t / 100
[Link] = si
End Sub

Private Sub Text1_Change()


On Error Resume Next
a = [Link]
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
End If
p = Val(a)
End Sub

Private Sub Text2_Change()


On Error Resume Next
a = [Link]
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
End If
r = Val(a)
End Sub

Private Sub Text3_Change()


On Error Resume Next
a = [Link]
If a < 48 And a > 57 Then
3
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

MsgBox ("Invalid Entry,Insert only numbers")


[Link] = ""
End If
t = Val(a)
End Sub

Output :

4
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-03
Aim: Write a program to take input of /X and print table of X in the following format

Code :

Dim a, b, x, y As Integer
Private Sub Command1_Click()
[Link]
a = Val(InputBox("Enter the number whose table you want to print", "Table", 1))
y = Val(InputBox("Upto which value you want to print table", "Mathematical Table", 10))
b=a
For x = 1 To y
[Link] (b & " * " & x & " = " & a * x)
Next
End Sub

Output :

5
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-04
Aim: Design an interface, which will appear like mark sheet. It will take input of
marks, in five subject and calculate total marks and percentage than provide grade
according to following criteria.

CODE :

Dim a As String
Dim m, p, c, e, h, b As Integer
Dim per As Double

Private Sub Cmd1()


per = (m + p + c + e + h) / 5
[Link] = per
If per >= 90 Then [Link] = "A+"
If per < 90 And per >= 75 Then [Link] = "A"
If per < 75 And per >= 60 Then [Link] = "B"
If per < 60 And per >= 45 Then [Link] = "C"
If per < 45 Then [Link] = "F"
If m > 35 And p > 35 And c > 35 And e > 35 And h > 35 Then
[Link] = "Pass"
Else
[Link] = "fail"
[Link] = "Nil"
End If
End Sub

Private Sub Text1_Change()


On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
6
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

m = Val(a)
Ankur
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then [Link]
End Sub

Private Sub Text2_Change()


On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
p = Val(a)
Ankur
End Sub

Private Sub Text2_GotFocus()


If [Link] = "" Then [Link]
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then [Link]
End Sub

Private Sub Text3_Change()


On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then

7
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

MsgBox ("Invalid Entry,Insert only numbers")


[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
c = Val(a)
Ankur
End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then [Link]
End Sub

Private Sub Text4_Change()


On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
e = Val(a)
Ankur
End Sub

Private Sub Text4_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then [Link]
End Sub

Private Sub Text5_Change()


On Error Resume Next
a = [Link]

8
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
h = Val(a)
Ankur
End Sub

Private Sub view_Click()


End Sub

Private Sub Timer1_Timer()


[Link](1).Text = Time
End Sub

Output :

9
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-05
Aim: WAP to create a simpler calculator.

Code :
Dim num1, num2, result As Double
Dim ref, dec As Integer
Dim op As String

Private Sub Command1_Click(Index As Integer)


If dec = 0 And Val([Link]) = 0 Then [Link] = ""
If ref = 0 Then
[Link] = [Link] & Command1(Index).Caption
Else
[Link] = Command1(Index).Caption
ref = 0
End If
End Sub
Private Sub Command10_Click()
[Link] = 0
num1 = 0
num2 = 0
result = 0
ref = 0
dec = 0
End Sub

Private Sub Command2_Click()


If dec = 0 Then
[Link] = [Link] & [Link]
End If
dec = 1
End Sub

Private Sub Command3_Click()


On Error GoTo l1
num2 = Val([Link])
ref = 1

10
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Select Case op
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
End Select
[Link] = result
If [Link] = 11 Then
[Link] = "Division By Zero"
End If
End Sub
Private Sub Command4_Click()
If num1 <> 0 Then
Command3_Click
End If
op = "+"
num1 = Val([Link])
ref = 1
dec = 0
End Sub

Private Sub Command5_Click()


If num1 <> 0 Then
Command3_Click
End If
op = "-"
num1 = Val([Link])
ref = 1
dec = 0
End Sub
Private Sub Command6_Click()
If num1 <> 0 Then

11
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Command3_Click
End If
op = "*"
num1 = Val([Link])
ref = 1
dec = 0
End Sub

Private Sub Command7_Click()


If num1 <> 0 Then
Command3_Click
End If
op = "/"
num1 = Val([Link])
ref = 1
dec = 0
End Sub

Private Sub Command8_Click()


ref = 1
[Link] = Val([Link]) ^ 0.5
dec = 0
End Sub

Output :

12
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-06
Aim: Write a program to check whether an entered no. is prime or not.
Code :
Dim a, x As Integer
Dim b As String

Private Sub Command1_Click()


For x = 2 To a - 1
If a Mod x = 0 Then
MsgBox ("Entered Number is not Prime")
Exit For
End If
Next
If x = a Then
MsgBox ("Entered Number is Prime")
End If
End Sub
Private Sub Text1_Change()
On Error Resume Next
b = [Link]
If b < 48 And b > 57 Then
MsgBox ("Insert only numbers")
[Link] = ""
End If
a = Val(b)
End Sub

Output :

13
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-07
Aim: Write a program which will count all vowels, consonanants, digits, special
character and blank spaces in a sentence.

Code :

Dim ac, ASC1, vo, co, di, sc, bs, nl As Integer


Dim S As String

Private Sub Text1_KeyPress(KeyAscii As Integer)


ASC1 = ac
ac = KeyAscii
Select Case ac
Case 65, 69, 73, 79, 85, 97, 101, 105, 111, 117
vo = vo + 1
GoTo L1
Case 65 To 90, 97 To 122
co = co + 1
Case 48 To 57
di = di + 1
Case 32
bs = bs + 1
GoTo L1
Case 13
nl = nl + 1
GoTo L1
Case Else
sc = sc + 1
End Select
L1:
[Link] = vo
[Link] = co
[Link] = di
[Link] = sc
[Link] = bs
[Link] = nl
End Sub

14
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Output :

15
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-08
Aim: WAP to illustrate all functionalities of list box and combo box.

Code :

Private Sub Command1_Click()


Dim n As Integer
For n = 0 To [Link] - 1
If [Link](n) = True Then
[Link] [Link](n)
[Link](n) = False
End If
Next n
End Sub

Private Sub Command2_Click()


Dim I, j As Integer
For I = ([Link] - 1) To j Step -1
If [Link](I) = True Then
[Link] (I)
[Link]
End If
Next I
If [Link] < 1 Then
[Link] = False
[Link] = False
[Link]
End If
End Sub

Private Sub Command3_Click()


Dim I As Integer
For I = 0 To [Link] - 1 Step 1
If [Link](I) Then
MsgBox "You have selected " & [Link](I)
End If
Next I
16
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

End Sub

Private Sub Command5_Click()


Unload Me
End Sub

Private Sub Form_Load()


[Link] "Color"
[Link] "Red"
[Link] "Black"
[Link] "Green"
[Link] "Blue"
[Link] "White"
[Link] "Yellow"
End Sub

Private Sub List1_Click()


[Link] = True
End Sub

Private Sub List2_Click()


[Link] = True
End Sub

17
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Output :

Adding Items To List 2

18
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-09
Aim: WAP using check boxes for following font effects.
a) Bold
b) Italic
c) Underline
d) Increase font size
e) Decreases font size
f) Font color

Code :

Private Sub Check1_Click()


If [Link] = 1 Then
[Link] = True
Else
[Link] = False
End If
End Sub

Private Sub Check2_Click()


If [Link] = 1 Then
[Link] = True
Else
[Link] = False
End If
End Sub

Private Sub Check3_Click()


If [Link] = 1 Then
[Link] = True
Else
[Link] = False
End If
End Sub

Private Sub Command1_Click()


[Link] = ""
End Sub
19
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Private Sub Option1_Click()


[Link] = vbRed
End Sub

Private Sub Option2_Click()


[Link] = vbBlue
End Sub

Private Sub Option3_Click()


[Link] = vbGreen
End Sub

Private Sub Option4_Click()


[Link] = 8
End Sub

Private Sub Option5_Click()


[Link] = 10
End Sub

Private Sub Option6_Click()


[Link] = 12
End Sub

Private Sub Option7_Click()


[Link] = 14
End Sub

Private Sub Option8_Click()


[Link] = 16
End Sub

Private Sub Option9_Click()


[Link] = vbYellow
End Sub

20
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Output :

21
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no-10
Aim: WAP for temperature conversion using option button.

Code :
Dim X As String
Dim a As Double
Dim fr1, fr2 As Integer

Private Sub Convert()


If [Link] = True Then
fr1 = 1
ElseIf [Link] = True Then
fr1 = 2
ElseIf [Link] = True Then
fr1 = 3
End If
If [Link] = True Then
fr2 = 1
ElseIf [Link] = True Then
fr2 = 2
ElseIf [Link] = True Then
fr2 = 3
End If
Select Case fr1
Case 1
Select Case fr2
Case 1
[Link] = a
Case 2
[Link] = ((9 * a) + 160) / 5
Case 3
[Link] = a + 273
End Select
Case 2
Select Case fr2
Case 1
22
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link] = ((5 * a) - 160) / 9


Case 2
[Link] = a
Case 3
[Link] = ((5 * a) + 2297) / 9
End Select
Case 3
Select Case fr2
Case 1
[Link] = a - 273
Case 2
[Link] = (9 * (a - 273) / 5) + 32
Case 3
[Link] = a
End Select
End Select
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As


Single)
Convert
End Sub

Private Sub Frame1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As


Single)
Convert
End Sub

Private Sub Frame2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As


Single)
Convert
End Sub

Private Sub Option1_Click()


Convert
End Sub

23
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Private Sub Option2_Click()


Convert
End Sub

Private Sub Option3_Click()


Convert
End Sub

Private Sub Option4_Click()


Convert
End Sub

Private Sub Option5_Click()


Convert
End Sub

Private Sub Option6_Click()


Convert
End Sub

Private Sub Text1_Change()


On Error Resume Next
X = [Link]
If X < 46 And X > 57 Or X = 47 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
a = Val(X)
Convert
End Sub

24
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Output :

Program no11 : WAP to launch a rocket using picture box and timer control.

Code :

Private Sub Command1_Click()


On Error Resume Next
[Link] = Min
[Link] = 10680
[Link] = 0
End Sub

Private Sub Command2_Click()


[Link] = 10
End Sub

Private Sub Form_Load()


[Link] = [Link]
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As


Single)
[Link] = [Link]
End Sub

Private Sub Timer1_Timer()


[Link] = [Link] - [Link]
End Sub

Private Sub VScroll1_Change()


[Link] = [Link]
End Sub

Output :

25
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no12 : WAP to change back color of any control (label, textbox)using scroll
box.

Form :

Code :

Private Sub Command1_Click()


[Link] = 0
26
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link] = 0
[Link] = 0
End Sub

Private Sub HScroll1_Change()


[Link] = RGB([Link], [Link], [Link])
[Link] = [Link] / 255 * 100
End Sub

Private Sub HScroll3_Change()


[Link] = RGB([Link], [Link], [Link])
[Link] = [Link] / 255 * 100
End Sub

Private Sub HScroll4_Change()


[Link] = RGB([Link], [Link], [Link])
[Link] = [Link] / 255 * 100
End Sub

Private Sub Text1_Change()


[Link] = [Link]
End Sub

Output :

27
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no13 : WAP to search an element for a one dimension static array.

Form :

Code :

Dim a(10), b As Integer


Private Sub Command1_Click()
For i = 0 To 9
a(i) = InputBox("enter any number")
Next i
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then
b = Val([Link])
For i = 0 To 9
If b = a(i) Then
x = MsgBox("The position of entered number in entered array is" & i, vbOKOnly,
"Search Result")
Exit Sub
End If
Next i
x = MsgBox("Element not found", vbOKOnly, "Message")
End If
End Sub

28
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Output :

29
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no14 : WAP to sort a dynamic array of N numbers and N strings (input
array size at run time).

For m :

Code :

Dim A() As String


Dim B() As Integer
Dim I As Integer
Dim R As Integer
Dim J As Integer
Dim TEMP As Variant

Private Sub Command1_Click()


R = Val(InputBox("Enter The Range"))
ReDim A(R) As String
For I = 1 To R Step 1
A(I) = InputBox("Enter The Strings")
Next I
End Sub

Private Sub Command2_Click()


R = Val(InputBox("Enter The Range"))
ReDim B(R) As Integer
30
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

For I = 0 To R - 1
B(I) = Val(InputBox("Enter The Integers"))
Next I
End Sub

Private Sub Command3_Click()


For I = 1 To R Step 1
For J = 1 To R - I Step 1
If A(J + 1) < A(J) Then
TEMP = A(J)
A(J) = A(J + 1)
A(J + 1) = TEMP
End If
Next J
Next I
Print "The Sorted Strings Are : -"
Print
For I = 1 To R Step 1
Print A(I)
Next I
End Sub

Private Sub Command4_Click()


For I = 0 To R - 2
For J = 0 To (R - 2) - I
If B(J) < B(J + 1) Then
TEMP = B(J)
B(J) = B(J + 1)
B(J + 1) = TEMP
End If
Next J
Next I
For I = 0 To R - 1
Print B(I)
Next
End Sub

Private Sub Command5_Click()


Unload Me
End Sub

Output :

31
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no15 : WAP to take input of two matrices and perform their addition
subtraction and multiplication using menu editor.

For m :

32
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :

Dim a(3, 3), b(3, 3), c(3, 3), i, j, k, sum As Integer

Private Sub ankur()


x = MsgBox("Insert the elements of first Matrix", vbOKOnly, "Message")
For i = 0 To 2
For j = 0 To 2
a(i, j) = Val(InputBox("Enter any number"))
Next j
Next i
x = MsgBox("Insert the elements of second Matrix", vbOKOnly, "Message")
For i = 0 To 2
For j = 0 To 2
b(i, j) = Val(InputBox("Enter any number"))
Next j
Next i
End Sub

Private Sub add_Click()


On Error Resume Next
ankur
For i = 0 To 2
For j = 0 To 2
Print a(i, j),
Next j
Print:
33
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Next i
Print::
For i = 0 To 2
For j = 0 To 2
Print b(i, j),
Next j
Print:
Next i
Print::
For i = 0 To 2
For j = 0 To 2
c(i, j) = a(i, j) + b(i, j)
Next j
Next i
For i = 0 To 2
For j = 0 To 2
Print c(i, j),
Next j
Print:
Next i
End Sub

Private Sub mul_Click()


On Error Resume Next
ankur
For i = 0 To 2
For j = 0 To 2
Print a(i, j),
Next j
Print:
Next i
Print::
For i = 0 To 2
For j = 0 To 2
Print b(i, j),
Next j
Print:
Next i
Print::
For i = 0 To 2
For j = 0 To 2
For k = 0 To 2
sum = sum + a(i, k) * b(k, j)
c(i, j) = sum
Next k
sum = 0
Next j
Next i
For i = 0 To 2
For j = 0 To 2
Print c(i, j),
Next j
Print:
Next i
34
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

End Sub

Private Sub sub_Click()


On Error Resume Next
ankur
For i = 0 To 2
For j = 0 To 2
Print a(i, j),
Next j
Print:
Next i
Print::
For i = 0 To 2
For j = 0 To 2
Print b(i, j),
Next j
Print:
Next i
Print::
For i = 0 To 2
For j = 0 To 2
c(i, j) = a(i, j) - b(i, j)
Next j
Next i
For i = 0 To 2
For j = 0 To 2
Print c(i, j),
Next j
Print:
Next i
End Sub

Output :

35
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no17 : WAP to illustrate call by value and call by reference (to swap to
values).

For m :

36
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :

Dim a, b, temp As Integer


Private Sub Command1_Click()

a = InputBox("Enter value of a :- ")


b = InputBox("Enter value of b :- ")
' Before Swapping
[Link] = a
[Link] = b
' After Swapping
temp = a
a=b
b = temp
[Link] = a
[Link] = b
End Sub
Private Sub Command2_Click()
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
End Sub
Private Sub Command3_Click()
End
End Sub

Output :

37
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no18 : Write a program to calculate factorial of a number using user


defined function.
For m :

38
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :
Dim x, a As Integer

Private Function fact(a As Integer) As Integer


Dim i, factorial As Integer
factorial = 1
For i = 1 To a
factorial = factorial * i
Next i
fact = factorial
End Function

Private Sub Command1_Click()


[Link] = ""
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
a = Val([Link])
x = fact(a)
MsgBox ("The factorial of entred number is " & x)
End If
End Sub
Output :

Program no19 : Take input of a word and WAP to check whether it is a palindrome
or not.(without using structure fun).
For m :

39
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :
Dim a, b As String
Private Sub Command1_Click()
b = StrReverse(a)
If (StrComp(a, b) = 0) Then
MsgBox ("The Entered String is a pallindrome")
Else
MsgBox ("The Entered String is not a pallindrome")
End If
End Sub

Private Sub Command2_Click()


[Link] = ""
End Sub

Private Sub Text1_Change()


a = UCase([Link])
End Sub
Output :

Program no20 : WAP to find smallest among given three numbers using user defined
procedures.

For m :

40
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :
Private Sub Command1_Click()
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "First no. is Smaller"
Else
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "Second No. is Smaller"
Else
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "Third No. is Smaller"
End If
End If
End If
End Sub
Private Sub Command2_Click()
[Link] = ""
[Link] = ""
[Link] = ""
End Sub
Output :

Program no21 : WAP to generate. Print and find sum of first n elements of
Fibonacci series using recursion.

For m :

41
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :

Private Function fibo(n As Integer) As Integer


If n = 0 Then
fibo = 0
Else
If (n <= 2) Then
fibo = 1
Else
fibo = fibo(n - 1) + fibo(n - 2)
End If
End If
End Function
Private Sub Command1_Click()
Dim c, n As Integer
n = CInt(InputBox("Enter the term of series :- "))
a=0
b=1
For n = 0 To n - 1
c = fibo(n)
[Link] = [Link] & c & vbNewLine
Next
End Sub
Private Sub Form_Load()
End Sub
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "First no. is Smaller"
Else
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "Second No. is Smaller"
Else
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "Third No. is Smaller"
End If
End If
End If
End Sub

Private Sub Command2_Click()


[Link] = ""
[Link] = ""
42
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link] = ""
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "First no. is Smaller"
Else
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "Second No. is Smaller"
Else
If [Link] < [Link] And [Link] < [Link] Then
MsgBox "Third No. is Smaller"
End If
End If
End If
End Sub

Private Sub Command2_Click()


[Link] = ""
[Link] = ""
[Link] = ""
End Sub

Output :

Program no22 : WAP to perform read write operations in a sequential file.


For m :

43
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :
Option Explicit
Dim s1 As String, s2 As String
Private Sub Command1_Click()
Open "[Link]" For Output As #1
Write #1, Text1, Text2
Close #1
MsgBox "Writing to a file success"
End Sub
Private Sub Command2_Click()
Open "[Link]" For Input As #1
Input #1, s1, s2
[Link] = s1
[Link] = s2
Close #1
MsgBox "Reading From a File "
End Sub
Private Sub Command3_Click()
Unload Me
End Sub
Output :

Program no23:-
Create a user defined-data type having fields name (as string of length 20 bytes). Roll no.
(as integer), class (as string of 10 bytes). WAP to create a random access file to store above
data and perform following operations in this file.
Write new record (b) Read / display existing record (c) Delete any record
(d) search my record (f) list selected records (e) close the file

44
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no24 : WAP to display records of a table using DAO & bound control code
for buttons to move at first recor, next record, previous record, last record in the table.
For m :

45
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :
Dim db As [Link]
Dim rs As [Link]
Dim a As String
Dim m, p, c, e, h, b As Integer
Dim per As Double
Private Sub Ankur()
per = (m + p + c + e + h) / 5
[Link] = per
If per >= 90 Then [Link] = "A+"
If per < 90 And per >= 75 Then [Link] = "A"
If per < 75 And per >= 60 Then [Link] = "B"
If per < 60 And per >= 45 Then [Link] = "C"
If per < 45 Then [Link] = "F"
If m > 35 And p > 35 And c > 35 And e > 35 And h > 35 Then
[Link] = "Pass"
Else
[Link] = "fail"
[Link] = "Nil"
End If
End Sub
Private Sub add_Click()
On Error Resume Next
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link]
[Link]
46
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

rs(1) = [Link]
rs(2) = [Link]
rs(3) = [Link]
rs(4) = [Link]
rs(5) = [Link]
rs(6) = [Link]
End Sub
Private Sub edit_Click()
rs(1) = [Link]
rs(2) = [Link]
rs(3) = [Link]
rs(4) = [Link]
rs(5) = [Link]
rs(6) = [Link]
[Link]
End Sub
Private Sub Form_Load()
[Link] = dbUseODBC
Set db = OpenDatabase("Odbc;dsn=Result")
Set rs = [Link]("select * from Table1", dbOpenDynamic, dbExecDirect,
dbOptimistic)
MsgBox "Connected"
Ankit
End Sub
Private Sub Ankit()
[Link] = rs(1)
[Link] = rs(2)
[Link] = rs(3)
[Link] = rs(4)
[Link] = rs(5)
[Link] = rs(6)
End Sub
Private Sub gf_Click()
[Link]
Ankit
End Sub
Private Sub gl_Click()
[Link]
Ankit
End Sub

Private Sub save_Click()


On Error Resume Next
[Link]
End Sub
Private Sub Text1_Change()
On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
47
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
m = Val(a)
Ankur
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then [Link]
End Sub
Private Sub Text2_Change()
On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
p = Val(a)
Ankur
End Sub
Private Sub Text2_GotFocus()
If [Link] = "" Then [Link]
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then [Link]
End Sub
Private Sub Text3_Change()
On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
c = Val(a)
Ankur
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then [Link]
End Sub

48
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Private Sub Text4_Change()


On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
e = Val(a)
Ankur
End Sub
Private Sub Text4_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then [Link]
End Sub
Private Sub Text5_Change()
On Error Resume Next
a = [Link]
b = Val([Link])
If a < 48 And a > 57 Then
MsgBox ("Invalid Entry,Insert only numbers")
[Link] = ""
ElseIf b > 100 Then
MsgBox ("Invalid Input")
[Link] = ""
End If
h = Val(a)
Ankur
End Sub

Private Sub Timer1_Timer()


[Link](1).Text = Time
End Sub
Private Sub Command1_Click()
On Error GoTo l1
[Link]
Ankit
l1:
If [Link] = 3021 Then
[Link]
Ankit
MsgBox "Last Record"
End If
End Sub
Private Sub Command2_Click()
On Error GoTo l1
[Link]
Ankit
l1:
If [Link] = 3021 Then

49
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link]
Ankit
MsgBox "First Record"
End If
End Sub

Output :

Program no25 : Create a table using visual data manger and write a program using
RDO & advance bound control to add, delete, edit & navigate records.

VISUAL DATA MANAGER

‘Step 1:

Click on Add Ins Menu ->


Choose Visual Data Manager ->
The VisData Window will be appeared.

50
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Step 2:

In the File Menu ->


Choose New Option then Choose Ms Access Option Version 2.0/7.0 MDB
A save dialog box will appear, prompt the database name and click ok button.

Step 3:

Now In the Database window right click on the Properties command.->


Choose Command New Table

Step 4:
Choose Add Field following dialog will be appeared
Add the required fields.

51
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Step 5:
Close the Add Field and Click on the Build The Table command of Table Structure
Window.

Step 6:
Now add data by double clicking on the table name (recordsofstudent) which is created.

Step 7:
Data base is ready Close the Visual Data Manager.

RDO (Remote Data Access)

To create RDO first we need to create an DSN i.e. Data Source Name.

CREATING DSN (Data Source Name)

Steps:-> Click Start Button and Open Control Panel


-> Select Administrative Tools
-> Open Data Sources (ODBC) Icon following dialog will be appeared

->Select MS Access Data Base and Click Add command next dialog will be appeared

52
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Select Driver do Microsoft Access (*.mdb) option and click Finish Command.
ODBC Microsoft Access Setup Dialog box will be displayed.

-> Enter Data Source Name and Select Database by clicking Select Button.
Click Ok button to create Data Source Name.

CODING FOR RDO


Steps Before
Go to Project Menu
Click on Component command
In the component dialog box select control tab if not selected
1. Check the Microsoft Remote Data Control 6.0
2. Drag the RDO Control on the form.
3. Right Click on the and select property
4. In the property dialog box select
Data Source = dns_record (Data Source Name)
SQL = select * from studentrecord

‘Coding for Advance Bound Control RDO


Private Sub cmd_add_Click()
[Link]
End Sub
53
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Private Sub cmd_del_Click()


[Link]
[Link]
End Sub
Private Sub cmd_edit_Click()
[Link]
End Sub
Private Sub Update_Click()
[Link]
End Sub

Program no26 : WAP to access a database using ADO & display a key column in
the combo box list box when an item is selected in it, its corresponding records is shown
in MSH flex grid.

For m :

Code :

Dim con As [Link]


Dim rs As [Link]
Private Sub dis()
[Link] = False
[Link] = False
[Link] = False
[Link] = False
[Link] = False
[Link] = False
[Link] = False
End Sub

Private Sub enb()


[Link] = True
[Link] = True
54
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link] = True
[Link] = True
[Link] = True
[Link] = True
[Link] = True
End Sub

Private Sub Command1_Click()


Set [Link] = rs
[Link]
End Sub

Private Sub Command10_Click()


enb
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link]
End Sub

Private Sub Command3_Click()


If [Link] < 0 Then
MsgBox "No Records to delete"
Else
[Link]
End If

If [Link] = True Then


[Link]
Else
[Link]
End If
[Link] = rs!Name
[Link] = rs!maths
[Link] = rs!eng
[Link] = rs!science
[Link] = rs!hindi
[Link] = rs!sst
[Link]
End Sub

Private Sub Command4_Click()


Unload Me
End Sub

Private Sub Command5_Click()


dis
If [Link] < 0 Then
MsgBox "No records Found"
Else
55
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link]
End If
End Sub

Private Sub Command6_Click()


dis
If [Link] = True Then
MsgBox "Begning Of File"
Else
[Link]
[Link] = rs!Name
[Link] = rs!maths
[Link] = rs!eng
[Link] = rs!science
[Link] = rs!hindi
[Link] = rs!sst
End If
End Sub

Private Sub Command7_Click()


dis
If [Link] = True Then
MsgBox "End Of File"
Else
[Link]
[Link] = rs!Name
[Link] = rs!maths
[Link] = rs!eng
[Link] = rs!science
[Link] = rs!hindi
[Link] = rs!sst
End If
End Sub

Private Sub Command8_Click()


dis
If [Link] < 0 Then
MsgBox "No records Found"
Else
[Link]
End If
End Sub

Private Sub Command9_Click()


[Link]
rs!Name = [Link]
rs!maths = [Link]
rs!eng = [Link]
rs!science = [Link]
rs!hindi = [Link]
rs!sst = [Link]
[Link]
End Sub

56
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Private Sub Form_Load()


dis
Set con = New [Link]
[Link] = "[Link].3.51"
[Link] "e:\project vb\project [Link]"
Set rs = New [Link] "table1", con, adOpenDynamic,
adLockOptimistic, adCmdTable
[Link] = "^Flex|^Student Name|^Roll No.|^Maths|^English|
^Science|^Hindi|^Social Science"
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then
[Link]
End If
[Link] = 13
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then
[Link]
End If
End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then
[Link]
End If
End Sub

Private Sub Text4_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then
[Link]
End If
End Sub

Private Sub Text5_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then
[Link]
End If
End Sub

Private Sub Text8_KeyPress(KeyAscii As Integer)


If KeyAscii = 13 Then
[Link]
End If
End Sub

Private Sub Text9_KeyPress(KeyAscii As Integer)


[Link] = flexResizeBoth
If KeyAscii = 13 Then
[Link] = [Link]
[Link] = [Link] + 1
[Link] = [Link]
57
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link] = [Link] + 1
[Link] = [Link]
[Link] = [Link] + 1
[Link] = [Link]
[Link] = [Link] + 1
[Link] = [Link]
[Link] = [Link] + 1
[Link] = [Link]
[Link] = [Link] + 1
[Link] = [Link]
End If
End Sub

Output :

Program no27 : Using data Environment create a program to display records of any
table.

Steps to Create Data Environment

[Link] the New Project select Data Project


58
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

[Link] selecting this option a form, a Data environment and a data report is loaded
automatically.

[Link] Open the Data Environment Window


[Link] Click on the Connection1 choose property option
5. Select Microsoft Jet 4.0 OLE DB Provider
6. Go to Connection Tab

[Link] Database and test connection then


Click OK Button.
[Link] Right Click on the Connection and
Choose Add Command Button.
[Link] right click on the command property
and open property.
[Link] Database Object type Table and
Object what you have created (Table Name).
[Link] Drag and Drop the fields on the
form then start conding.

59
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Code :

Private Sub cmd_first_Click()


[Link]
End Sub

Private Sub cmd_last_Click()


[Link]
End Sub

Private Sub cmd_next_Click()


[Link]
If [Link] Then
[Link]
MsgBox ("Last Record")
End If
End Sub

Private Sub cmd_Prev_Click()


[Link]
If [Link] Then
[Link]
MsgBox ("First Record")
End If
End Sub

60
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Private Sub Command1_Click()


[Link]
End Sub

Program no28:-
WAP to generate mark sheet of students in a class through data report.

61
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no29:-WAP to illustrate various key board and mouse events.


Code—
Form MouseUp And MouseDown in [Link]
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As
Single)
Circle (X, Y), 75
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As
Single)
Circle (X, Y), 75
End Sub
For MouseMove Event in [Link]
62
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As


Single)
Circle (X, Y), 75
End Sub
For KeyPress Event [Link]
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < "65" Or KeyAscii > "91" Then
If KeyAscii < "97" Or KeyAscii > "122" Then
KeyAscii = 0 'set character to Null if Not
MsgBox "Enter Only Character"
End If
End If
End Sub
For KeyUp/Key Down Events
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF2 Then
MsgBox "You have pressed the F2 Key"
End If
End Sub

Outputs:-Mouse Move Event

Mouseup / Mouse Down Events:

KeyUp / KeyDown Event:-

KeyPress Event:-
63
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Program no30:-
Using drive, directive and file list box ( it will shown only .bmp files ). Let the user select
the bmb files, which will appear in picture box as user click on any item in list box.

Form View:-

Run Time Source Code:-


Private Sub Dir1_Change()
[Link] = [Link]
[Link] = [Link]
End Sub

Private Sub Drive1_Change()


[Link] = [Link]
End Sub

Private Sub File1_Click()


[Link] = LoadPicture([Link] & [Link]([Link]))
End Sub

Private Sub Text1_Change()


Dir1_Change
End SubOutput View:-

64
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Ass-:-31--

Using toolbar design an interface for string manipulation. Toolbar should have tabs to
Find length of string
No of blank spaces in string
Reverse the string
Also show current date & time in status bar.

Code:-
Dim I As Integer
Dim SUM As Integer
Dim btn As Button
Private Sub Command1_Click()
[Link] = Len([Link])
End Sub
Private Sub Command2_Click()
[Link] = StrReverse([Link])
End Sub
Private Sub Command3_Click()
SUM = 0
For I = 0 To (Len([Link])) Step 1
If InStr(1, [Link], " ") = 1 Then
SUM = SUM + 1
End If
Next I
[Link] = SUM
End Sub
Private Sub Form_Load()
Dim btn As Button
Set btn = [Link](1, , "Length", tbrDefault)
Set btn = [Link](2, , "Reverse", tbrDefault)
Set btn = [Link](3, , "Blank Space", tbrDefault)
65
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

Dim pn As Panel
Set pn = [Link]()
[Link] = 1
Set pn = [Link](, , , sbrDate)
Set pn = [Link](, , , sbrTime)
[Link] = sbrInset
[Link] = sbrRight
[Link](1).AutoSize = sbrContents
[Link](1).Text = "Date and Time"
End Sub
Private Sub Toolbar1_ButtonClick(ByVal Button As [Link])
If [Link] = 1 Then
Command1_Click
Else
If [Link] = 2 Then
Command2_Click
Else
If [Link] = 3 Then

Command3_Click
End If
End If
End If
End Sub
Output View:-

66
Lab Manual: CS-(Visual Basic)
Department of Computer Science
Rungta College of Science & Technology, Durg

67
Lab Manual: CS-(Visual Basic)

You might also like