Model Questions for Practical - VB

Answers are given at the end

   
         
   
  1. Create a text box with Multi-select property and a menu with edit features to act upon  the text typed in the text box.
   
   

 

   

Answer to Question-1

 First set the multiselect property true.

 

 

Private Sub mnuCopy_Click()
     Clipboard.Clear

      Clipboard.SetText

 Screen.ActiveControl.SelText

End Sub

 

Private Sub mnuDelete_Click()

Screen.ActiveControl.SelText = ""

End Sub

 

Private Sub mnuExit_Click()

Unload Me

End Sub

 

Private Sub mnuPaste_Click()

Screen.ActiveControl.SelText = Clipboard.GetText

End Sub

 

Private Sub munCut_Click()

Clipboard.Clear

Clipboard.SetText Screen.ActiveControl.SelText

Screen.ActiveControl.SelText = ""

End Sub

 

     
  2. Place together a Drive list, Directory list, File list box  and an Image box. Browse picture files. Display the selected picture file in the image box. 
     
   
 

Code for Question.2

 Private Sub Form_Load()

Image1.Stretch = True

File1.Pattern = "*.jpg"

End Sub

 

Private Sub Dir1_Change()

File1.Path = Dir1.Path

End Sub

 

Private Sub Drive1_Change()

Dir1.Path = Drive1.Drive

End Sub

 

Private Sub File1_Click()

Image1.Picture = LoadPicture(File1.Path & "/" & File1.FileName)

End Sub

 

  3. Create a multipurpose calculator as illustrated below.
     
   
 

Code for Question-3

 Private Sub CmdFind_Click()

If Option1.Value = True Then

Text3 = Val(Text1) + Val(Text2)

ElseIf Option2.Value = True Then

Text3 = Val(Text1) - Val(Text2)

ElseIf Option3.Value = True Then

Text3 = Val(Text1) * Val(Text2)

ElseIf Option4.Value = True Then

Text3 = Val(Text1) / Val(Text2)

End If

End Sub

 

Private Sub CmdClear_Click()

Text1 = ""

Text2 = ""

Text3 = ""

Text1.SetFocus

End Sub

 

Private Sub CmdExit_Click()

End

End Sub

 

   4. Create a conventional multipurpose calculator as given below.
   

Answer to Question - 4

Option Explicit

Dim s As Integer

Dim sign As String

 

Private Sub CmdNumber_Click(Index As Integer)

Text1 = Text1 + Mid("0123456789", Index + 1, 1)

End Sub

 

Private Sub CmdSign_Click(Index As Integer)

s = Val(Text1)

Text1 = ""

Text1.SetFocus

Select Case Index

Case 0

sign = "+"

Case 1

sign = "-"

Case 2

sign = "*"

Case 3

sign = "/"

End Select

End Sub

 

Private Sub CmdEqual_Click()

Select Case sign

Case "+"

Text1 = Val(Text1) + s

Case "-"

Text1 = Val(Text1) - s

Case "*"

Text1 = Val(Text1) * s

Case "/"

Text1 = s / Val(Text1)

End Select

End Sub

 

Private Sub CmdClear_Click()

s = 0

Text1 = ""

Text1.SetFocus

End Sub

 

 
   5. A sample program illustrating the mouse enter event.
     
   

Answer to QUESTION-5

Private Sub Command1_Click()

MsgBox ("You clicked 'NO' button")

End Sub

 

Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As _

Single, Y As Single)

Command2.Caption = "YES"

Command1.Caption = "NO"

End Sub

 

Private Sub Command2_Click()

MsgBox ("You clicked 'NO' button")

End Sub

 

Private Sub Command2_MouseMove(Button As Integer, Shift As Integer, X As _                                                                   Single, Y As Single)

Command2.Caption = "NO"

Command1.Caption = "YES"

End Sub

 
     
   6. Program to find largest among three numbers using InputBox and MsgBox 
   

Answer to QUESTION-6

Private Sub Form_Load()

Dim a As Integer

Dim b As Integer

Dim c As Integer

Dim d As Integer

back:

a = InputBox("Enter the First Number ")

b = InputBox("Enter the Second Number ")

c = InputBox("Enter the Third Number ")

If a > b And a > c Then

d = MsgBox("The Largest Number is " & a & vbCrLf & "Do You wish _                                             to Continue?", vbYesNo)

ElseIf b > c Then

d = MsgBox("The Largest Number is " & b & vbCrLf & "Do You wish_                                    to Continue?", vbYesNo)

Else

d = MsgBox("The Largest Number is " & c & vbCrLf & "Do You wish_

to Continue?", vbYesNo)

End If

If d = vbYes Then

GoTo back

Else

End

End If

End Sub

 

 7. Place a ComboBox ( with run-time added items, set in the sorted order) and a ListBox on a form. Add  selected items from the ComboBox to the ListBox and remove selected items from the ListBox  with the appropriate command buttons. Provide the provision for multiple selection and deletion in the ListBox.

Answer to QUESTION-7

 Option Explicit

Dim i As Integer

Private Sub Combo1_Click()

List1.AddItem Combo1.List(Combo1.ListIndex)

End Sub

 

Private Sub Command1_Click()

Combo1.AddItem Text1.Text

Text1 = ""

Text1.SetFocus

End Sub

 

Private Sub Command2_Click()

For i = List1.ListCount - 1 To 0 Step -1

If (List1.Selected(i) = True) Then

List1.RemoveItem (i)

End If

Next i

End Sub

 
     
  8. Design a billing system for a tea shop. Place a group-box  titled “Beverages” and add to it 3 checkbox items labeled “Tea”, “Coffee” and “Drinks” along with adjacently placed textboxes that inputs the corresponding item quantity. Place another group-box titled “Eatables” and add to it 3 checkbox items labeled ”Puffs”, “Vada” and “Cutlet” along with adjacently placed textboxes that inputs respective item quantity. Display total bill amount for individual entries.

Answer for QUESTION-8

 

Dim a As Integer

 

Private Sub Check1_Click()

Text1.SetFocus

End Sub

Private Sub Check2_Click()

Text2.SetFocus

End Sub

Private Sub Check3_Click()

Text3.SetFocus

End Sub

Private Sub Check4_Click()

Text4.SetFocus

End Sub

Private Sub Check5_Click()

Text5.SetFocus

End Sub

Private Sub Check6_Click()

Text6.SetFocus

End Sub

 

Private Sub Command1_Click()

a = 0

If Check1.Value = 1 And Text1 <> "" Then a = a + 3 * Val(Text1)

If Check2.Value = 1 And Text2 <> "" Then a = a + 5 * Val(Text2)

If Check3.Value = 1 And Text3 <> "" Then a = a + 10 * Val(Text3)

If Check4.Value = 1 And Text4 <> "" Then a = a + 5 * Val(Text4)

If Check5.Value = 1 And Text5 <> "" Then a = a + 4 * Val(Text5)

If Check6.Value = 1 And Text6 <> "" Then a = a + 8 * Val(Text6)

If a > 1 Then

Label1.Caption = "Total Price= " & a

Else

Label1.Caption = "Select Any Item"

End If

End Sub

 

Private Sub Command2_Click()

Check1.Value = False

Check2.Value = False

Check3.Value = False

Check4.Value = False

Check5.Value = False

Check6.Value = False

Text1 = ""

Text2 = ""

Text3 = ""

Text4 = ""

Text5 = ""

Text6 = ""

End Sub

 

     
 9. Write the procedure for creating an Access Database named Student_Data with table Student_Rec having the fields Reg_no, Name, Course, Fees  and Design an interface for inserting records to that table throw ADO with procedure.

Answer for QUESTION-9

 

Private Sub Command1_Click()

Adodc1.Recordset.AddNew

Text1 = ""

Text2 = ""

Text3 = ""

Text4 = ""

Text1.SetFocus

Command2.Enabled = True

Command1.Enabled = False

End Sub

 

Private Sub Command2_Click()

If Text1 = "" Or Text2 = "" Or Text3 = "" Or Text4 = "" Then

MsgBox ("Empty field cannot be entered!!!")

Else

Adodc1.Recordset.Update

End If

Command1.Enabled = True

Command2.Enabled = False

End Sub

 

Private Sub Form_Load()

Command2.Enabled = False

End Sub

 

     
 10. Write a Menu driven program for Inserting, Deleting and Editing the records for the table Person which contains the fields Name, Tel_Phone_no, Address, and Email. 

Answer for QUESTION-10

 Private Sub mnuadd_Click()

Adodc1.Recordset.AddNew

End Sub

 

Private Sub mnudelete_Click()

If Text1 <> "" Then

Adodc1.Recordset.Delete

Adodc1.Recordset.MoveFirst

End If

End Sub

 

Private Sub mnuedit_Click()

If Text1 <> "" Then

Adodc1.Recordset.Update

End If

End Sub

 

Private Sub mnuexit_Click()

End

End Sub

 

Private Sub mnusave_Click()

If Text1 = "" Or Text2 = "" Or Text3 = "" Or Text4 = "" Then

MsgBox ("Empty field cannot be entered!!!")

Else

Adodc1.Recordset.Update

End If

End Sub