You are on page 1of 65

VISUAL BASIC PRACTICAL FILE

Submitted by: P. Vikas 04490301910 BBA(CAM)

Institute of Innovation in Technology & Management, (Affiliated to GGSIPU, New Delhi)

INDEX
S. No. Construct Name 1 Starting Visual Basic and its environment Problem statement Ques 1. Write the steps to open Visual Basic. Ques2. Paste a snapshot of the VB IDEin the word document & list its components. Ques 1. Create a form which to print hello on the form. Ques2. Create an application to perform addition, subtraction, multiplication & division functions. Ques 3. Create a form to display the mark sheet of a student . Page No. 4 Teachers signature

Working with forms and controls

Working with forms and controls

Ques 1. Create a form that has 3 12 textbox controls which accepts Title, first name, last name & command button ok when clicked displays the name combined. Ques 2. Create a form that takes temperature in Fahrenheit/Celsius, converts it and shows the result. Ques 1. Create a movie ticket system. The form should have an image control, and list box control and depending on the no. of seats booked, shows the total amount. 16

Working with forms and controls

Programmin g constructs:

Ques 1. Create an application that 18 generates a pay slip for the employee.
2

if, then & else statement and select case

The form should have Empcode, Name, Basic Salary, Grade, HRA, Allowance & Net Salary. HRA=40% of Basic Salary Allowance is 250 for grade 1 n 500 for grade 2 Net Salary = Basic + HRA +Allowance Ques 1. Write a program to find the factorial. Ques 2. Write a program to accept a number from user and show whether it is odd or even. Ques 3. Write a program to accept a number from user and print its multiplication table. Ques1. WAP to print odd numbers between 1 to 20. Ques2. Make an interface to print 1 to 20 in reverse order. Ques3. Make an interface to find the Fibonacci series of the number entered by the user. Ques 4. WAP to print square of numbers from 1 to 10. 20

Loop constructs

Loop constructs

25

Arrays

Ques1. Create an array and find the 31 sum and average of its numbers. Ques2. Create an interface to take 10 numbers from the user and display its sum and average. Ques1. Create a calculator using MDI. Ques 2.create a MDI form having menu calculate and change. They should have submenus like
3

MDI form menu

35

10

Procedures & Functions

factorial, area,line,shapes etc as submenus and attach appropriate child forms to these submenus. Ques1. Create an interface to calculate the Simple Interest. Ques2. WAP to swap the values of two variables.

41

11

Procedures & Functions

Ques 1. WAP that find the sum of two 46 numbers using function. Ques2. Write a Program that checks whether a number entered by user is even or odd. Ques1. WAP to perform string functions. Ques2. Create an interface to show the math functions. Ques3. Write a program to perform date function. Ques 1. Write a program for the addition of two matrices. Ques1. Write a program to imply form validation Ques 1. Create an interface using DAO, RDO and ADO for employee details. Ques2. Create an interface using DAO, RDO and ADO for student details. Also make command buttons to view first, next, previous and last record. 50

12

Inbuilt Functions

13

matrices

56

14 15

Form Validation Connectivity through DAO, RDO & ADO

58 61

ASSIGNMENT 1
Ques 1. Write the steps to open Visual Basic. Ans. The steps are as follows :i. ii. iii. iv. Click on the Start Button. Click on Programs menu. From the program sub menu click on Microoft Visual Basic 6.0 From Microsoft Visual Basic 6.0 sub menu click on Microsoft Visual Basic 6.0

Ques2. Paste a snapshot of the VB IDEin the word document & list its components. Ans.

Menu bar Title bar

Form Tool box

Project window

Property window

ASSIGNMENT 2
Ques 1. Create a form which to print hello on the form. Ans.

Private Sub Command1_Click() Print "click me again" lbl1.Caption = "hello" End Sub

Private Sub Form_Load() 'Form1.Show 'Print "hello" End Sub

Ques2. Create an application to perform addition, subtraction, multiplication & division functions. Ans. Private Sub Command1_Click() Label4.Caption = CInt(Text1.Text) + CInt(Text2.Text) End Sub

Private Sub Command2_Click() Label4.Caption = CInt(Text1.Text) - CInt(Text2.Text) End Sub

Private Sub Command3_Click() Label4.Caption = CInt(Text1.Text) * (Text2.Text) End Sub

Private Sub Command4_Click() Label4.Caption = CInt(Text1.Text) / (Text2.Text) End Sub

Ques 3. Create a form to display the mark sheet of a student . Ans .

Private Sub Command1_Click() Text5.Text = (CInt(Text2.Text) + CInt(Text3.Text) + CInt(Text4.Text)) End Sub

Private Sub Command2_Click() Text6.Text = (CInt(Text2.Text) + CInt(Text3.Text) + CInt(Text4.Text)) / 3 End Sub

10

11

ASSIGNMENT 3
Ques 1. Create a form that has 3 textbox controls which accepts Title, first name, last name & command button ok when clicked displays the name combined.

Ans. Private Sub Command1_Click() Dim a, b, c As String a = Text1.Text b = Text2.Text Text3.Text = a & " " & b End Sub

12

13

Ques 2. Create a form that takes temperature in Fahrenheit/Celsius, converts it and shows the result. Ans. Private Sub Command1_Click() Dim a, b As Integer a = CInt(Text1.Text) b = (a - 32) * (5 / 9) Text2.Text = b End Sub

Private Sub Command2_Click() Dim a, b As Integer a = CInt(Text3.Text) b = (a * (9 / 5)) + 32 Text4.Text = b End Sub

14

15

ASSIGNMENT 4
Ques 1. Create a movie ticket system. The form should have an image control, and list box control and depending on the no. of seats booked, shows the total amount. Ans

Private Sub Command1_Click() Dim a As Integer a = Combo1.Text * Combo2.Text MsgBox "movie selected" & List1.Text & vbCrLf & "no. of tickets" & Combo1.Text & vbCrLf & "types of ticket" & Combo2.Text & vbCrLf & "total bill" & a

End Sub

Private Sub List1_Click() If List1.Text = "Agneepath" Then Image1.Picture = LoadPicture("C:\Documents and Settings\All Users.WINDOWS\Documents\My Pictures\Sample Pictures\Blue hills.jpg") ElseIf List1.Text = "don2" Then Image1.Picture = LoadPicture("C:\Documents and Settings\All Users.WINDOWS\Documents\My Pictures\Sample Pictures\sunset.jpg") ElseIf List1.Text = "Tintin" Then

16

Image1.Picture = LoadPicture("C:\Documents and Settings\All Users.WINDOWS\Documents\My Pictures\Sample Pictures\winter.jpg") Else Image1.Picture = LoadPicture("C:\Documents and Settings\All Users.WINDOWS\Documents\My Pictures\Sample Pictures\water lilies.jpg") End If End Sub

17

ASSIGNMENT 5
Ques 1. Create an application that generates a pay slip for the employee. The form should have Empcode, Name, Basic Salary, Grade, HRA, Allowance & Net Salary. HRA=40% of Basic Salary Allowance is 250 for grade 1 n 500 for grade 2 Net Salary = Basic + HRA +Allowance Ans. Private Sub Command1_Click() Dim a, b As Integer a = CInt(Text3.Text) b = a * (40 / 100) Text4.Text = b End Sub

Private Sub Command2_Click() If List1.Text = 1 Then Text5.Text = 250 Else Text5.Text = 500 End If End Sub
18

Private Sub Command3_Click() Text6.Text = (CInt(Text3.Text) + CInt(Text4.Text) + CInt(Text5.Text)) End Sub

19

ASSIGNMENT 6
Ques 1. Write a program to find the factorial. Ans.

Private Sub Command1_Click() Dim a, fact, i As Integer fact = 1 a = Text1.Text For i = 1 To a fact = fact * i Next Text2.Text = fact End Sub

20

Ques 2. Write a program to accept a number from user and show whether it is odd or even. Ans. Private Sub Command1_Click() Dim n, i As Integer n = CInt(Text1.Text) i=2 While a > 0 a=a-i i=2 Wend If a = 0 Then Text2.Text = "even" Else Text2.Text = "odd" End If End Sub

21

22

Ques 3. Write a program to accept a number from user and print its multiplication table. Ans.

Private Sub Command1_Click() Dim a, s, i As Integer a = CInt(Text1.Text) For i = 1 To 10 s=a*i Print s Next End Sub

23

24

ASSIGNMENT 7
Ques1. WAP to print odd numbers between 1 to 20. Ans. Private Sub Command1_Click() Dim i As Integer For i = 1 To 20 Print i i=i+1 Next End Sub

25

Ques2. Make an interface to print 1 to 20 in reverse order. Ans. Private Sub Command1_Click() Dim i As Integer i = 20 While i >= 1 Print i i=i-1 Wend End Sub

Private Sub Command2_Click() Dim i As Integer i = 20 Do While i >= 1 Print i i=i-1 Loop End Sub

26

27

Ques3. Make an interface to find the Fibonacci series of the number entered by the user. Ans. Private Sub Command1_Click() Dim m, x, y, z, i As Integer m = CInt(Text1.Text) x=0 y=1 Print x Print y While z < m z=x+y Print z x=y y=z Wend End Sub

28

29

Ques 4. WAP to print square of numbers from 1 to 10. Ans. Private Sub Command1_Click() Dim a, i As Integer For i = 1 To 10 a=i*i Print a Next End Sub

30

ASSIGNMENT 8
Ques1. Create an array and find the sum and average of its numbers. Ans. Private Sub Command1_Click() Dim a(10), sum, avg, i As Integer a(0) = 1 a(1) = 2 a(2) = 3 a(3) = 4 a(4) = 5 a(5) = 6 a(6) = 7 a(7) = 8 a(8) = 9 a(9) = 10 a(10) = 11 For i = 0 To 10 sum = sum + a(i) Next Text1.Text = sum avg = sum / 11

31

Text2.Text = avg End Sub

32

Ques2. Create an interface to take 10 numbers from the user and display its sum and average. Ans. Private Sub Command1_Click() Dim a(10), avg, sum, i As Integer sum = 0 For i = 0 To 10 a(i) = InputBox("Enter a number") sum = sum + a(i) Next Text1.Text = sum avg = sum / 11 Text2.Text = avg End Sub

33

34

ASSIGNMENT 9

Ques1. Create a calculator using MDI. Ans. Private Sub a1_Click() Form1.Show End Sub

Private Sub d1_Click() Form4.Show End Sub

Private Sub m1_Click() Form3.Show End Sub

Private Sub s1_Click() Form2.Show End Sub

(Form1 )
35

Private Sub Command1_Click() Text3.Text = (CInt(Text1.Text) + CInt(Text2.Text)) End Sub

(Form2) Private Sub Command1_Click() Text3.Text = (CInt(Text1.Text) - CInt(Text2.Text)) End Sub

(Form3) Private Sub Command1_Click() Text3.Text = (CInt(Text1.Text) * CInt(Text2.Text)) End Sub

(Form4) Private Sub Command1_Click() Text3.Text = (CInt(Text1.Text) / CInt(Text2.Text)) End Sub

36

37

Ques 2.create a MDI form having menu calculate and change. They should have submenus like factorial, area,line,shapes etc as submenus ans attach appropriate child forms to these submenus. Ans. (MDI form) Private Sub a1_Click() Form2.Show End Sub

Private Sub f1_Click() Form1.Show End Sub

Private Sub l1_Click() Form5.Show End Sub

Private Sub o1_Click() Form4.Show End Sub

Private Sub s1_Click()

38

Form3.Show End Sub

(Form1) Private Sub Command1_Click() Dim a, i, fact As Integer a = CInt(Text1.Text) fact = a Do While i <= a fact = fact * i i=i+1 Loop Text2.Text = fact End Sub

(Form2) Private Sub Command1_Click() Dim a, b As Single a = CInt(Text1.Text) b = (22 / 7) * a * a Text2.Text = b End Sub
39

40

ASSIGNMENT 10

Ques1. Create an interface to calculate the Simple Interest.

Ans. Private Sub SI(ByVal p, ByVal r, ByVal t) Text4.Text = (p * r * t) / 100 End Sub Private Sub Command1_Click() Dim p, r, t As Integer p = CInt(Text1.Text) r = CInt(Text2.Text) t = CInt(Text3.Text) Call SI(p, r, t) End Sub

41

42

Ques2. WAP to swap the values of two variables. Ans. Private Sub swap(ByVal a, ByVal b) Dim c As Integer c=a a=b b=c End Sub

Private Sub switch(ByRef a, ByRef b) Dim c As Integer c=a a=b b=c End Sub

Private Sub Command1_Click() Dim x, y As Integer x = CInt(Text1.Text) y = CInt(Text2.Text) Call swap(x, y) Text3.Text = x
43

Text4.Text = y End Sub

Private Sub Command2_Click() Dim x, y As Integer x = CInt(Text1.Text) y = CInt(Text2.Text) Call switch(x, y) Text5.Text = x Text6.Text = y End Sub

44

45

ASSIGNMENT 11
Ques 1. WAP that find the sum of two numbers using function. Ans. Private Sub add(ByVal a, ByVal b) Dim q As Integer Text3.Text = a + b End Sub Private Sub Command1_Click() Dim s, t As Integer s = CInt(Text1.Text) t = CInt(Text2.Text) Call add(s, t) End Sub

46

47

Ques2. Write a Program that checks whether a number entered by user is even or odd. Ans.

Private Sub even(ByRef a) Dim i As Integer i=2 While a > 0 a=a-i i=2 Wend If a = 0 Then Text2.Text = "even" Else Text2.Text = "odd" End If End Sub Private Sub Command1_Click() Dim n As Integer n = CInt(Text1.Text) Call even(n) End Sub
48

49

ASSIGNMENT 12
Ques1. WAP to perform string functions. Ans.

Private Sub Command1_Click() Dim a As String a = (Text1.Text) Text2.Text = Len(a) Text3.Text = Left(a, 7) Text4.Text = Right(a, 12) Text5.Text = LTrim(a) Text5.Text = RTrim(a) Text6.Text = LTrim(a) End Sub

50

51

Ques2. Create an interface to show the math functions. Ans. Private Sub Command1_Click() Dim a, b As Integer Dim u As Double u = 81 a = -50 Text1.Text = Abs(a) b = 90 Text2.Text = Sin(b) Text3.Text = Cos(b) Text4.Text = Tan(b) Text5.Text = Sqr(u) Text6.Text = Round(90.86) End Sub

52

53

Ques3. Write a program to perform date function. Ans.

Private Sub Command1_Click() Text1.Text = Now() Text2.Text = DateValue(Now) Text3.Text = TimeValue(Now) Text4.Text = Month(Now) Text5.Text = Day(Now) Text6.Text = Year(Now) Text7.Text = Hour(Now) Text8.Text = Minute(Now) Text9.Text = Second(Now) End Sub

54

55

ASSIGNMENT 13
Ques 1. Write a program for the addition of two matrices. Ans. Private Sub Command1_Click() Dim mat1(1, 1) As Integer Dim mat2(1, 1) As Integer Dim result(1, 1) As Integer Dim i, j As Integer For i = 0 To 1 For j = 0 To 1 mat1(i, j) = InputBox("Enter The Numbers") Next Next For i = 0 To 1 For j = 0 To 1 mat2(i, j) = InputBox("Enter the Numbers") Next Next For i = 0 To 1 For j = 0 To 1 result(i, j) = mat1(i, j) + mat2(i, j)

56

Next Next For i = 0 To 1 For j = 0 To 1 Print result(i, j) Next Next End Sub

57

ASSINGNMENT 14
Ques1. Write a program to imply form validation. Ans. Private Sub Command1_Click() If (Text1.Text = "") Then MsgBox "the id field cannot be left blank" Text1.SetFocus End If If Not IsNumeric(Text1.Text) Then MsgBox "invalid id" End If If (Text2.Text = "") Then MsgBox "the name field cannot be left blank" Text2.SetFocus End If If IsNumeric(Text2.Text) Then MsgBox "invalid name" End If If (Text3.Text < 5000) Or (Text3.Text > 100000) Then MsgBox "salary is not within the range" End If

58

If Not IsDate(Text4.Text) Then MsgBox "invalid date" End If If Not IsDate(Text5.Text) Then MsgBox "invalid date" End If

Dim id, sal As Integer Dim name As String Dim dob, doj As Date id = CInt(Text1.Text) sal = CInt(Text3.Text) name = Text2.Text dob = CDate(Text4.Text) doj = CDate(Text5.Text) Print "your id is"; id Print "your name is"; name Print "your department is"; List1.Text Print "your salary is"; sal Print "your date of birth is"; dob Print "your date of joining is"; doj End Sub
59

Private Sub Form_Load() List1.AddItem "HR" List1.AddItem "MARKETING" List1.AddItem "FINANCE" List1.AddItem "FIELD" End Sub

60

ASSIGNMENT 15
Ques 1. Create an interface using DAO, RDO and ADO for employee details. Ans.

Private Sub Command1_Click() Data1.Recordset.AddNew Text1.Text = "" Text2.Text = "" Text3.Text = "" 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.Update End Sub

61

62

Ques2. Create an interface using DAO, RDO and ADO for student details. Also make command buttons to view first, next, previous and last record. Ans. Private Sub Command1_Click() Data1.Recordset.AddNew Text1.Text = "" Text2.Text = "" Text3.Text = "" 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.Update End Sub

Private Sub Command3_Click() Data1.Recordset.MoveFirst End Sub

Private Sub Command4_Click()


63

Data1.Recordset.MoveNext End Sub

Private Sub Command5_Click() Data1.Recordset.MoveLast End Sub

Private Sub Command6_Click() Data1.Recordset.MovePrevious End Sub

64

65

You might also like