0% found this document useful (0 votes)
66 views

Khushi VB

This document provides code snippets in Visual Basic to create various applications. Some key examples include: 1. Creating an application that displays an image when a command button is clicked, and resizes the image over time using a timer control. 2. Checking if a number is even or odd, calculating factorials, and determining if a number is prime. 3. Calculating book discounts based on number of books purchased. 4. Creating forms to display student names in list boxes and allow shifting between lists, as well as displaying state capitals and cities based on combo box selection. 5. Creating arrays, finding frequencies of elements, and displaying positions. Also counting characters and words in a text

Uploaded by

Khushi Aglawe
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
0% found this document useful (0 votes)
66 views

Khushi VB

This document provides code snippets in Visual Basic to create various applications. Some key examples include: 1. Creating an application that displays an image when a command button is clicked, and resizes the image over time using a timer control. 2. Checking if a number is even or odd, calculating factorials, and determining if a number is prime. 3. Calculating book discounts based on number of books purchased. 4. Creating forms to display student names in list boxes and allow shifting between lists, as well as displaying state capitals and cities based on combo box selection. 5. Creating arrays, finding frequencies of elements, and displaying positions. Also counting characters and words in a text

Uploaded by

Khushi Aglawe
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
You are on page 1/ 26

1.

A] Make an application in Visual Basic, which display a picture when,

i) Command button with caption “click here” is clicked.

INPUT

Private Sub Command1_Click()


Timer1.Interval = 100
End Sub

Private Sub Timer1_Timer()


Picture1.Picture = LoadPicture("C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg")
End Sub

OUTPUT
ii) After delay time 100ms(by using Timer control)

INPUT

Private Sub Command1_Click()


Timer1.Interval = 100
Picture1.Picture=LoadPictures(“C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg")
End Sub

Private Sub Timer1_Timer()


Picture1.Height = Picture1.Height + 150
Picture1.Width = Picture1.Width + 120
If(Picture1.Height >=5500) then
Timer1.Interval = 0
End If
End Sub

OUTPUT
1] B)
V. Find a even/Odd number.

INPUT

Option Explicit
Dim no As Integer

Private Sub Command1_Click()


no = Text1.Text
If no Mod 2 = 0 Then
Label2.Caption = "Given number is Even"
Else
Label2.Caption = "Given number is Odd"
End If
End Sub

OUTPUT
VI. Find Factorial of number.

INPUT

Option Explicit
Dim a As Integer
Dim b As Integer
Dim i As Integer

Private Sub Command1_Click()


a = Val(Text1.Text)
b=1
For i = 1 To a
b=b*i
Next
Text2.Text = b
End Sub
OUTPUT
vii. Find if the number is Prime or Not Prime.

INPUT

Option Explicit
Dim check As Integer
Dim num As Integer
Dim i As Integer

Private Sub Command1_Click()


check = 1
num = Text1.Text
For i = 2 To (num - 1)
If num Mod i = 0 Then
check = 0
Exit For
End If
Next
If check = 0 Then
MsgBox"not prime"
Else
MsgBox"prime"
End If
End Sub
OUTPUT
2]A) A book stall gives discount on the books as per following condition,

No. of books purchased Discount


<=5 Nil
>5 and <=10 10%
>10 and <=1512%
>15 20%

Create a for, as follows to calculate the discount.

INPUT

Option Explicit
Dim n As Integer
Dim dis As Single
Dim tp As Single

Private Sub Command1_Click()


If Text2.Text = "" Or Text2.Text = "" Then
MsgBox "you must entered the price and no. of books", vbExclamation, "alert"
Else
Label6.Caption = Val(Text1.Text) * Val(Text2.Text)
End If
End Sub

Private Sub Command2_Click()


n = Val(Text2.Text)
tp = Val(Label6.Caption)
If n < 5 Then
dis = 0
ElseIf n > 5 And n <= 10 Then
dis = tp * 10 / 100
ElseIf n > 10 And n <= 15 Then
dis = tp * 12 / 100
ElseIf n > 15 Then
dis = tp * 20 / 100
End If
Label7.Caption = dis
Command2.Enabled = True
End Sub

Private Sub Command3_Click()


Label8.Caption = Val(Label6.Caption) - Val(Label7.Caption)
End Sub

OUTPUT
B] Design a form for speed control program using scrollbars.

INPUT
Private Sub Command1_Click()
HScroll1.Enabled = True
HScroll1.Value = 1
End Sub

Private Sub HScroll1_Change()


If Hscroll1 > 80 Then
MsgBox "Speed cannot be increased further", vbCritical, "ERROR"
HScroll1.Enabled = False
Else
Label1.Caption = HScroll1.Value
End If
End Sub
OUTPUT
3]A] Design an application, which display a text and given choice to change text to bold, italic,
underline(any one or in combination) using checkbox controls.

INPUT

Private Sub Check1_Click()


If Check1.Value = 1 Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
End Sub

Private Sub Check2_Click()


If Check2.Value = 1 Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
End If
End Sub

Private Sub Check3_Click()


If Check3.Value = 1 Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
End Sub
OUTPUT
B] Write a VB code to give five choices of colors. Design an application to choose any one color
using option button and change the forecolor of textbox caption.

INPUT
Private Sub Option1_Click()
If Option1.Value = True Then
Text1.ForeColor = vbRed
Else
Text1.ForeColor = vbBlack
End If
End Sub
Private Sub Option2_Click()
If Option2.Value = True Then
Text1.ForeColor = vbBlue
Else
Text1.ForeColor = vbBlack
End If
End Sub
Private Sub Option3_Click()
If Option3.Value = True Then
Text1.ForeColor = vbMagenta
Else
Text1.ForeColor = vbBlack
End If
End Sub
Private Sub Option4_Click()
If Option4.Value = True Then
Text1.ForeColor = vbYellow
Else
Text1.ForeColor = vbBlack
End If
End Sub
Private Sub Option5_Click()
If Option5.Value = True Then
Text1.ForeColor = vbGreen
Else
Text1.ForeColor = vbBlack
End If
End Sub
OUTPUT
4]A] Write a program in VB to build application to display sorted list of group A & group B
student in two list boxes. Make provision to shift name(s) on either side using ListBox control.
INPUT
Option Explicit
Dim i As Integer

Private Sub Command1_Click()


For i = 0 To List1.ListCount
List2.AddItem List1.List(i)
Next
List1.Clear
End Sub

Private Sub Command2_Click()


If List1.ListCount = 0 Then
MsgBox "list is empty ..."
ElseIf List1.ListIndex = -1 Then
MsgBox "You must select item ..."
Else
List2.AddItem List1.Text
List1.RemoveItem List1.ListIndex
End If
End Sub

Private Sub Command3_Click()


If List2.ListCount = 0 Then
MsgBox "list is empty ..."
ElseIf List2.ListIndex = -1 Then
MsgBox "You must select item ..."
Else
List1.AddItem List2.Text
List2.RemoveItem List2.ListIndex
End If
End Sub

Private Sub Command4_Click()


For i = 0 To List2.ListCount
List1.AddItem List2.List(i)
Next
List2.Clear
End Sub
OUTPUT
B] Write a program in VB to build application which display list of states. Make provision to
display Capital of the State and Cities when a particular State is selected using combo Box.
INPUT
Private Sub Command1_Click()
If Combo1.ListIndex = 0 Then
Text1.Text = ""
Text1.Text = "Mumbai"
ElseIf Combo1.ListIndex = 1 Then
Text1.Text = ""
Text1.Text = "Bhopal"
ElseIf Combo1.ListIndex = 2 Then
Text1.Text = ""
Text1.Text = "Gandhinagar"
ElseIf Combo1.ListIndex = 3 Then
Text1.Text = ""
Text1.Text = "Jiapur"
ElseIf Combo1.ListIndex = 4 Then
Text1.Text = ""
Text1.Text = "Hyderabad"
End If

If Combo2.ListIndex = 0 Then
Text2.Text = ""
Text2.Text = "Maharashtra"
ElseIf Combo2.ListIndex = 1 Then
Text2.Text = ""
Text2.Text = "Madhya Pradesh"
ElseIf Combo2.ListIndex = 2 Then
Text2.Text = ""
Text2.Text = "Gujrat"
ElseIf Combo2.ListIndex = 3 Then
Text2.Text = ""
Text2.Text = "Rajasthan"
ElseIf Combo2.ListIndex = 4 Then
Text2.Text = ""
Text2.Text = "Andhra Pradesh"
End If
End Sub

Private Sub Form_Load()


Combo1.AddItem "Maharashtra"
Combo1.AddItem "Madhya Pradesh"
Combo1.AddItem "Gujrat"
Combo1.AddItem "Rajasthan"
Combo1.AddItem "Andhra Pradesh"

Combo2.AddItem "Nagpur"
Combo2.AddItem "Bhopal"
Combo2.AddItem "Ahmedabad"
Combo2.AddItem "Jaipur"
Combo2.AddItem "Hyderabad"
End Sub
OUTPUT
5]A] Write a VB Program to create an array of N elements. Find the frequency of element and
display the positions in an array.

INPUT
Option Explicit
Dim a(5) As Integer
Dim i As Integer
Dim n As Integer
Dim cnt As Integer

Private Sub Command1_Click()


If i> 5 Then
MsgBox "overflow"
Else
a(i) = Text1.Text
i=i+1
Text1.Text = ""
Text1.SetFocus
End If
End Sub

Private Sub Command2_Click()


n = Val(Text2.Text)
For i = 0 To 5
If n = a(i) Then
cnt = cnt + 1
End If
Next
Label3.Caption = cnt
End Sub

Private Sub Command3_Click()


Text1.Text = ""
Text2.Text = ""
Label3.Caption = ""
End Sub
OUTPUT
B] Create an application, which counts no. of characters and no. of words entered in TextBox.
INPUT

Private Sub Command1_Click()


Dim countchar, countword As Integer
Dim duptext As String
countchar = Len(Text1.Text)
duptext = Replace(Trim(Text1.Text), vbNewLine, "")
If duptext = "" Then
countword = 0
Else
countword = 1

For i = 1 To Len(duptext)
If Mid(duptext, i, 1) = "" Then
If Mid(duptext, i - 1, 1) <> "" Then
countword = countword + 1
End If
End If
Next
End If
MsgBox "Number of characters:" &countchar&vbNewLine& "Number of words:" &countword
End Sub

OUTPUT
6] Create Menus using editor as follows.

And Write appropriate code on click event of New & Edit Menu Options.
INPUT
Private Sub menuexit_Click()
Unload Me
End Sub

Private Sub menunew_Click()


frmNew.Visible = True
End Sub

Private Sub menuopen_Click()


FrmOpen.Show
End Sub

OUTPUT
7] Create a database pay.mdb using Visual Database Manager with fields code number pay(n),
Name of Emplyoee(T), Designation(T), Address(T), Joining date(T), Basics pay(n), Status(T)
[Permanent / Temporary], Department(T), [purchase, sales, account]. Achieve connectivity
using Data control. Develop an application in V.B to display the data of employee using
MSFlexgrid control as shown below with company’s logo.

OUTPUT
8] Design a Menu as shown below.
Create table pay.mdb using Visual Data Manager and connect it to V.B application using Data
Control. Display/view records using DbGridControl.
Create an application in V.B to open database of employee pay.mdb by using menu option
“OPEN”. Use APPEND and SAVE options to ADD & SAVE records in Database.

INPUT
Private Sub menuappend_Click()
Adodc1.Recordset.AddNew
Text1.Text
End Sub

Private Sub menuexit_Click()


Unload Me
End Sub

Private Sub menuopen_Click()


Form2.Show
End Sub

Private Sub menusave_Click()


Adodc1.Recordset.Update
Adodc1b.Refresh
End Sub
OUTPUT
9] Open DataBase fees.mdb with fields:-
Name, Class, fees using Visual Data Manager use Data control for connectivity.
Develop a menu driven application in V.B. to:-
1) Compute total fees class wise.
2) Search a record of a particular student belonging to a particular class.

INPUT
Private Sub menuexit_Click()
Unload Me
End Sub

Private Sub menuopen_Click()


Form2.Show
End Sub

Private Sub menusearch_Click()


Dim s_name As String
s_name = InputBox("Enter the name of student to search")
Data1.Recordset.FindFirst "Name" & s_name & ""
If Data1.Recordset.NoMatch = True Then
MsgBox "Record not Found", vbInformation, "Result"
End If
End Sub

Private Sub menutotalfees_Click()


Data1.Recordset.MoveFirst
Do While Not Data1.Recordset.EOF
Class = Data1.Recordset.Fields(1).Value
If StrComp(Class, "BSC-1") = 0 Then
sum1 = Val(sum1 + Data1.Recordset.Fields(2).Value)
ElseIf StrComp(Class, "BSC-2") = 0 Then
sum2 = Val(sum2 + Data1.Recordset.Fields(2).Value)
ElseIf StrComp(Class, "BSC-3") = 0 Then
sum3 = Val(sum3 + Data1.Recordset.Fields(2).Value)
End If
Data1.Recordset.MoveNext
Loop
MsgBox "Total Fees of BSC-1=" & sum1, vbInformation, "Result"
MsgBox "Total Fees of BSC-2=" & sum2, vbInformation, "Result"
MsgBox "Total Fees of BSC-3=" & sum3, vbInformation, "Result"
End Sub

Private Sub menucompute_Click()


sum1 = 0
sum2 = 0
sum3 = 0

Data1.Recordset.MoveFirst
Do While Not Data1.Recordset.EOF
Class = Data1.Recordset.Fields(1).Value
If StrComp(Class, "BSC-1") = 0 Then
sum1 = Val(sum1 + Data1.Recordset.Fields(2).Value)
ElseIf StrComp(Class, "BSC-2") = 0 Then
sum2 = Val(sum2 + Data1.Recordset.Fields(2).Value)
ElseIf StrComp(Class, "BSC-3") = 0 Then
sum3 = Val(sum3 + Data1.Recordset.Fields(2).Value)
End If

Data1.Recordset.MoveNext
Loop
MsgBox "Total Fees of BSC-1=" & sum1, vbInformation, "Result"
MsgBox "Total Fees of BSC-2=" & sum2, vbInformation, "Result"
MsgBox "Total Fees of BSC-3=" & sum3, vbInformation, "Result"
End Sub

Private Sub menuexit_Click()


Unload Me
End Sub
OUTPUT
10] Create a DataBase College.mdb containing fields:-
Name, Class, Marks in Phy, Comp. Sc., Math’s and percentage using Visual Data Manager.
Connect database to vb application using Data Control.
Develop a Menu driven application in V.B. to
1) Edit a records class wise.
2) Calculate class wise passing percentage where a student is declared “pass”, if he/she
gets percentage >= 45.
Display Class wise list of students who are placed in 1 st Division.(Percentage >=80)

INPUT
Private Sub Command1_Click()
Data1.Recordset.Edit
End Sub

Private Sub Command10_Click()


Data1.Recordset.FindLast "class=' & Text2.Text & '"
If Data1.Recordset.NoMatch Then
MsgBox "Not Found"
End If
End Sub

Private Sub Command11_Click()


Data1.Recordset.FindNext "class=' & Text2.Text & '"
If Data1.Recordset.NoMatch Then
MsgBox "Not Found"
End If
End Sub

Private Sub Command12_Click()


Data1.Recordset.FindPrevious "class=' & Text2.Text& '"
If Data1.Recordset.NoMatch Then
MsgBox "Not Found"
End If
End Sub

Private Sub Command2_Click()


Data1.Recordset.Fields(0) = Text1.Text
Data1.Recordset.Fields(1) = Text2.Text
Data1.Recordset.Fields(2) = Text3.Text
Data1.Recordset.Fields(3) = Text4.Text
Data1.Recordset.Fields(4) = Text5.Text
Data1.Recordset.Fields(5) = Text6.Text
MsgBox "Record saved"
Data1.Recordset.Update
MsgBox "Successfully saved", vbInformation, "Information"
Command1.Visible = True
Command2.Visible = True
Command3.Visible = True
Command4.Visible = True
Command5.Visible = True
End Sub

Private Sub Command3_Click()


Data1.Recordset.AddNew
End Sub
Private Sub Command4_Click()
Dim a As Integer
Text6.Text = ((Val(Text3.Text) + Val(Text4.Text) + Val(Text3.Text)) / 300) * 100
If Val(Text6.Text) >= 90 And Val(Text6.Text) <= 100 Then
MsgBox "Grade A"
ElseIf Val(Text6.Text) >= 80 And Val(Text6.Text) <= 90 Then
MsgBox "Grade B"
ElseIf Val(Text6.Text) >= 70 And Val(Text6.Text) <= 80 Then
MsgBox "Grade C"
Else
MsgBox "Grade D"
End If
End Sub

Private Sub Command5_Click()


Data1.Recordset.MoveFirst
End Sub

Private Sub Command7_Click()


If Data1.Recordset.EOF Then
MsgBox "End of file", vbInformation, "ERROR"
Else
Data1.Recordset.MoveNext
End If
End Sub

Private Sub Command8_Click()


If Data1.Recordset.BOF Then
MsgBox "Begining of file", vbInformation, "ERROR"
Else
Data1.Recordset.MovePrevious
End If
End Sub

Private Sub Command9_Click()


Data1.Recordset.FindFirst "Class=' & Text2.Text& '"
If Data1.Recordset.NoMatch Then
MsgBox "NotFound"
End If
End Sub

Private Sub menubsc1_Click()


Data1.Recordset.MoveFirst
Data1.Recordset.FindFirst "class=BSC-1"
If Data1.Recordset.NoMatch Then
MsgBox "Not Found"
End If
End Sub

Private Sub menubsc2_Click()


Data1.Recordset.MoveFirst
Data1.Recordset.FindFirst "class='BSC-2'"
If Data1.Recordset.NoMatch Then
MsgBox "Not Found"
End If
End Sub
Private Sub menubsc3_Click()
Data1.Recordset.MoveFirst "class='BSC-3'"
If Data1.Recordset.NoMatch Then
MsgBox "Not Found"
End If
End Sub

Private Sub menucreate_Click()


Command1.Visible = True
Command3.Visible = True
Command9.Visible = True
Command10.Visible = True
Command12.Visible = True
Command2.Visible = True
End Sub

Private Sub menudisplay_Click()


formcombo.Show
End Sub
OUTPUT
11] Create a table book.mdb using VDM containing fields:-
Name of Books, subject, price, date of purchase, Name of publication.
Develop a menu driven application in V.B. to:-
1) Open Database.
2) Delete a record of book.
3) Display subject wise list of book in proper format.

INPUT
Private Sub menudelete_Click()
Form2.Show
End Sub

Private Sub menuexit_Click()


Unload Me
End Sub

Private Sub menuopen_Click()


Frame1.Visible = True
End Sub

Private Sub Combo1_Change()


Data1.Recordset.MoveFirst
Call fillCombo
End Sub

Private Sub Command1_Click()


If MsgBox(" Are you sure you want to delete?", vbQuestion + vbYesNo, "Confirm") = vbYes Then
Data1.Recordset.Delete
MsgBox "Successfully Deleted", vbInformation, "Result"
End If
End Sub

Public Sub fillCombo()


Combo1.Clear
Data1.Recordset.MoveFirst
Do While Not Data1.Recordset.EOF
Combo1.AddItem Data1.Recordset.Fields(0).Value
Data1.Recordset.MoveNext
Loop
End Sub
OUTPUT
12] Create a table using VDM to store required information for computing Electricity Bill.
Charges are as follows.
1) 1.25/units for 1st 30 units.
2) 2.90/units from 31 to 300 units.
3) 4.0/units from 301 to onwards.
Develop a menu driven application to:-
1) Open Database
2) Display Electricity Bill in proper format.
3) Delete a record of book.
4) Append a record.
5) Edit record.
INPUT
Private Sub Form_Load()
DataGrid1.Visible = False
End Sub

Private Sub menuappend_Click()


Adodc1.Recordset.AddNew
End Sub

Private Sub menudelete_Click()


Adodc1.Recordset.Delete
End Sub

Private Sub menudisplay_Click()


n = Val(Text1.Text)
If n > 1 And n < 30 Then
Adodc1.Recordset.Fields(2) = 1.25 * n
Adodc1.Recordset.Update
ElseIf n > 31 And n > 300 Then
Adodc1.Recordset.Fields(2) = 2.9 * n
Adodc1.Recordset.Update
Else
Adodc1.Recordset.Fields(2) = 4 * n
Adodc1.Recordset.Update
End If
End Sub

Private Sub menuedit_Click()


DataGrid1.AllowUpdate = True
End Sub

Private Sub menuexit_Click()


Unload Me
End Sub

Private Sub menuopen_Click()


DataGrid1.Visible = True
End Sub
OUTPUT

You might also like