vb assignments
TRANSCRIPT
-
8/8/2019 VB Assignments
1/21
Assignments
Training Division, NIC, New Delhi
R 1
?Assignments in Controls
? Assignment 1: Usage of TEXTBOX & COMMAND BUTTON
Objective : Creation of a form using Text box and Command button controls, in whichthe text Starting of Visual Basic should appear in the Textbox, on clickingthe command button OK
Steps for designing the above form
? Open new form in your project by clicking Project>>Add form, then selecting theform option.
? Click on object text box in tool and drag it to blank form.? Adjust the size of text box.? Click on object command button and drag it to blank form.? Adjust the size of command button.? Set the properties of two objects as follows:
Object Property Value Code
Textbox NameText
txtString
Command
Button1
Name
Caption
cmdOK
OK
txtString.Text=Starting of Visual
Basic
? Save and execute the form.
TextBox
Command Button
-
8/8/2019 VB Assignments
2/21
Assignments
Training Division, NIC, New Delhi
R 2
? Assignment 2: Usage of Frame, Check box and Radio Buttons withthe Text box
Objective : Conversion of text in Upper-case or in a Lower-case, Bold, Italic and
Underline as desired.
Steps for designing the above form
? Open new form in your project by clicking Project>>Add form, then selecting theform option.
? Create the following objects in the blank form:
Object Property Value Code
Text Box Name
Text
txtInput
Radio Button1 Name
Caption
Option1
UpperCase
Private Sub Option1_Click( )
If Option1.Value = True Then
txtInput.Text = UCase(txtInput.Text)
End IfEnd Sub
Radio Button2 Name
Caption
Option2
LowerCase
Private Sub Option2_Click( )
If Option2.Value = True Then
txtInput.Text = LCase(txtInput .Text)
End IfEnd Sub
Text Box
Radio Button Check BoxFrame
-
8/8/2019 VB Assignments
3/21
Assignments
Training Division, NIC, New Delhi
R 3
Click Box1 Name
Caption
Checkbold
Bold
Private Sub checkbold_Click( )
If checkbold.Value = 1Then
txtInput.FontBold = True
Else
txtInput.FontBold = False
End IfEnd Sub
Click Box2 Name
Caption
Checkitalic
Italic
Private Sub checkitalic_Click( )
If checkitalic.Value = 1 ThentxtInput.Fontitalic = True
Else
txtInput.Fontitalic = FalseEnd If
End Sub
Click Box3 Name
Caption
checkUnderline
Underline
Private Sub checkUnderline_Click( )
If checkUnderline.Value = 1 Then
txtInput.FontUnderline = TrueElse
txtInput.FontUnderline = False
End IfEnd Sub
Text Box Text
? Click on Run button or run the project from Run>> Start.? Type desired text in a Text Box then click the buttons on the form and see the output.
-
8/8/2019 VB Assignments
4/21
Assignments
Training Division, NIC, New Delhi
R 4
? Assignment 3: Usage of List Box and Combo box
Objective : Creation of a form to add list of items to Combo box and list box throughentry in the Text box
Steps for designing the above form
? Open new form in your project by clicking Project>>Add form, then selecting theform option.
? Create the following objects in the blank form:
Object Property Value Code
Text Box Name
Text
txtBox
Command
Button1
Name
Caption
cmdAddItem
Add Item to
List
Private Sub CmdAddItem_Click( )
Dim Data As String
Data = txtBox.Text
lstBox.AddItem DatacboBox.AddItem Data
End Sub
List Box Name lstBox
ComboBox Name cboBox
? Click on Run button or Run project from RUN menu then click Start.? Type Apple in Text Box and click the button Add Item to List.? Similarly, continue adding items to the List Box and Combo Box
Now Both item will be add to list box and as well as in the combo Boxes
Combo Box
List Box
-
8/8/2019 VB Assignments
5/21
Assignments
Training Division, NIC, New Delhi
R 5
? Assignment 4: Usage of OLE Control (Object Linking andEmbedding)
Objective : To create a form to make a two way link with an object in another Windows
application.
Steps for designing the above form
? Open new form in your project by clicking Project>>Add form, then selecting theform option.
? Create the following objects in the blank form:
Object Property Value Code
Label Caption
Font
Use of OLE
Bold, Size 14
Command 1Button
Caption Quit Private Sub Command1_Click( )End
End Sub
-
8/8/2019 VB Assignments
6/21
Assignments
Training Division, NIC, New Delhi
R 6
OLE1Control
BorderStyleAppearance
BackColor
0- None0- FlatLight Grey
OLE2Control
BorderStyleAppearance
BackColor
0- None0- Flat
Light GreyOLE3Control
BorderStyleAppearance
BackColor
0- None0- Flat
Light Grey
? When you select OLE object from tools box and drag it to the form, the followingscreen appears.
? Scroll down the list of objects, and click on Microsoft Word? CheckDisplay as Icon? ClickOK to close the Insert Object dialog box and to open MS Word.? Type in some matter in MS Word and exit.? Resize object label if desired.? Repeat same steps for MS Excel etc..
You can select a file also by clicking on Create from a file button.
-
8/8/2019 VB Assignments
7/21
Assignments
Training Division, NIC, New Delhi
R 7
? Assignment 5: Usage of TIMER Control (Setting of Time)
Objective : To set the time limit for the execution of your program
Steps for designing the above form
? Open new form in your project by clicking Project>>Add form, then selecting theform option.
? Create the following objects in the blank form:
Object Property Value Code
Timer1 Caption
Interval
Timer
1500
Private Sub Timer1_Timer( )
MsgBox (Sorry, Your time is up.)End Sub
Lable CaptionFont
Use of OLEBold, Size 14
Command 1
Button
Caption Quit Private Sub Command1_Click( )
If Text1.Text = secret ThenTimer1.Enabled = False
MsgBox (Welcome To the
System)Else
MsgBox (Sorry, Friend, I dont
know you.)End If
End Sub
? Now run the program.? Enter the Password and see the output.
-
8/8/2019 VB Assignments
8/21
Assignments
Training Division, NIC, New Delhi
R 8
?Assignments in Programming
? Assignment 1. Use of Assigning Values to Variables
Objective: Selection of a Drive from Drive List box, a folder from Directory Listbox,
Image file from the filelistbox of the folder and viewing of the selected imagein the image Control Space.
Fig. 1Steps for designing the above form
? Open new form in your project by clicking Project>>Add form, and thenselecting the Form option.
? Bring the following objects in the blank form and set their respective properties:
Object Properties Value CodeDrive listbox Name Drive1 Private Sub Drive1_Change( )
Dir1.path = Drive1.DriveEnd Sub
Directorylistbox Name Dir1 Private Sub Dir1_Click( )File1.path = Dir1.Path
End Sub
Image Control Name
Stretch
Image1
True
Filelistbox Name File1 Private Sub File1_Click( )
SelectFile = File1.path & \ & File1.filenameImage1.Picture= LoadPicture(SelectFile)
End Sub
Note: Ensure that `Click event procedure is selected
? Save and execute the form.
Drive List Box
Directory ListBox
File ListBox
Image Control
-
8/8/2019 VB Assignments
9/21
Assignments
Training Division, NIC, New Delhi
R 9
? Assignment 2 Use of If...ThenElse, decision structure.
Example 1: Lucky Seven
Fig. 2
Description: In this form, when Spin button is clicked, three numbers appear in thelabels. In case any of the numbers is 7, the picture of Coins appears, otherwise itdoes not.
Code for Spin button:
Private Sub cmdSpin_Click()Image1.Visible = False
Label1.Caption = Int(Rnd * 10)
Label2.Caption = Int(Rnd * 10)Label3.Caption = Int(Rnd * 10)
If (Label1.Caption = 7) Or (Label2.Caption = 7) Or (Label3.Caption = 7) ThenImage1.Visible = TrueBeep
End IfEnd Sub
Hint: Get the picture of Coins from the file:C:\Program Files\Microsoft Visual Studio\Common\Graphics\Metafile\Business\Coins
-
8/8/2019 VB Assignments
10/21
Assignments
Training Division, NIC, New Delhi
R 10
? Assignment 3 Use of If...ThenElse, decision structure (contd.).
Example 2: Age factor
Fig. 3
Description: In this form, you are supposed to type your age in the text box andthen press the Submit button. Based on the age typed, some message will appearin the label below. Next you may click on the Type Again button and the cursor
flashes in the text box. Try putting different ages and see the results.
Code for Submit Button
Private Sub cmdSubmit_Click()
Dim Age As IntegerAge = txtAge.TextIf Age >= 13 And Age < 20 Then
lblMessage.Caption = "You are a teenager"ElseIf Age = 21 And Age < 58 ThenlblMessage.Caption = "Need to be More Responsible"
ElseIf Age >= 59 Then
lblMessage.Caption = "Now is the time to retire and relax "End If
End Sub
Code for Type Again Button
txtAge.Text = ""lblMessage.Caption = ""
txtAge.SetFocus
-
8/8/2019 VB Assignments
11/21
Assignments
Training Division, NIC, New Delhi
R 11
? Assignment 4 Use of Select Case Decision Structure
Fig. 4
Objective: The functionality of this form, as discussed before, can also be achievedwith the help of another decision structure called Select Case.
Description: Same as in Example 2, Assignment 3
Code for Submit button (using Select Case )
Private Sub cmdSubmit_Click()
Age = txtAge.TextSelect Case AgeCase Is < 13
lblMessage.Caption = "That's a nice age to be"Case 13 To 19
lblMessage.Caption = "You are a teenager"
Case 20 To 58lblMessage.Caption = "Need to be More responsible now"
Case Is > 58
lblMessage.Caption = "Enjoy retired but Fruitful life"End SelectEnd Sub
Code for Type Again Button
txtAge.Text = ""lblMessage.Caption = ""
txtAge.SetFocus
-
8/8/2019 VB Assignments
12/21
Assignments
Training Division, NIC, New Delhi
R 12
? Assignment 4 Use of For.Next Loop
Fig. 5
Description: When the Loop button is clicked, the text "NIC" appears in differentfont sizes, line by line.
Code forLoop Button
Private Sub cmdLoop_Click()Dim Count As Integer
For Count = 1 To 10FontSize = 10 + CountPrint "NIC"; Count
Next CountEnd Sub
Try this: For StepNext Loop
In the above example, replace
For Count = 1 To 10FontSize = 10 + CountPrint "NIC"; Count
Next Count
withFor Count = 10 To 20 Step 2
FontSize = 10 + CountPrint "NIC"; CountNext Count
and run the form to see the results
-
8/8/2019 VB Assignments
13/21
Assignments
Training Division, NIC, New Delhi
R 13
? Assignment 5 Use of DoWhile Loop
Fig. 5
Description: When the Loop button is clicked, the text "NIC" appears in differentfont sizes, line by line.
Code forLoop Button
Private Sub cmdLoop_Click()Dim Count As Integer
For Count = 1 To 10FontSize = 10 + CountPrint "NIC"; Count
Next CountEnd Sub
Try this: For StepNext Loop
In the above example, replace
For Count = 1 To 10FontSize = 10 + CountPrint "NIC"; Count
Next Count
withFor Count = 10 To 20 Step 2
FontSize = 10 + CountPrint "NIC"; CountNext Count
and run the form to see the results
-
8/8/2019 VB Assignments
14/21
Assignments
Training Division, NIC, New Delhi
R 14
? Assignment 6 Use of Do WhileLoop
Fig. 6
Description : When the Do..While option is clicked under Loops menu , a dialog boxappears. Here upper limit of the numbers has to be entered and OK button is clicked.As a result, list of the numbers starting from 1 to the upper limit will be printed on the
main form.
Code for DoWhile menu item under Loops menu
Private Sub doopt1_Click()
Dim x, p As Integerx = InputBox("Enter the max limit")
Do While p < xp = p + 1
Print p
LoopEnd Sub
In the above example replaceDim x, p As Integer
x = InputBox("Enter the max limit")
Do While p < xp = p + 1
Print pLoop
withDim x, p As Integer
x = InputBox("Enter the max limit")Do Until p = x
-
8/8/2019 VB Assignments
15/21
Assignments
Training Division, NIC, New Delhi
R 15
p = p + 1
Print p
Loop
For trying for Do until..Loop and then replace the same code with the following code for
While..Wend loop
Dim x, p As Integerx = InputBox("Enter the max limit")
While p < x
p = p + 1Print p
Wend
-
8/8/2019 VB Assignments
16/21
Assignments
Training Division, NIC, New Delhi
R 16
? Assignment 7 Use of Arrays
Fig. 7
Description : Create array of 10 text boxes and a button with caption RESET on theform. When Arrays menu item is clicked table of 2 will be printed in the text boxex
created.
Code in the Form load event
Private Sub Form_Load()
Command1.Visible = FalseFor i = 0 To 9
Text1(i).Visible = False
NextEnd Sub
Code Arrays menu item
Private Sub cmdarrays_Click()
Command1.Visible = True
For i = 1 To 10Text1(i - 1).Visible = True
Text1(i - 1).Text = "2 * " & i & " = " & 2 * i
NextEnd Sub
-
8/8/2019 VB Assignments
17/21
Assignments
Training Division, NIC, New Delhi
R 17
Code for RESET button on the main form
Private Sub Command1_Click()Command1.Visible = False
For i = 0 To 9
Text1(i).Visible = FalseNext
End Sub
-
8/8/2019 VB Assignments
18/21
Assignments
Training Division, NIC, New Delhi
R 18
?Assignments in MDI Forms
? Open the Project.? SelectProject>>Add MDI Form. MDI Form will appear as shown below :
? Create the menu on the above form as shown in tree given below
? Create two normal forms whose interfaces are as shown below
In this example NorthWind database already available as sample with Microsoft productsis used.
-
8/8/2019 VB Assignments
19/21
Assignments
Training Division, NIC, New Delhi
R 19
Set theMDIChildproperty to TRUE for both the forms.
? Write the following piece of code on the respective formsEMPLOYEE Form :
Public cnn As ADODB.ConnectionPublic rst As ADODB.Recordset
Private Sub Form_Load()Set cnn = New ADODB.Connectioncnn.Open _ConnectionString:="Provider=Microsoft.Jet.OLEDB.3.51;" & _"Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb"Set rst = New ADODB.Recordsetrst.Open Source:="employees", ActiveConnection:=cnn, _CursorType:=adOpenDyanamic, LockType:=adLockOptimistic, _Options:=adCmdTable
Call movefieldEnd Sub
Sub movefield()rst.MoveNextText1.Text = rst!employeeidText2.Text = rst!firstnameText3.Text = rst!birthdateEnd Sub
Sub moveprev()rst.MovePreviousText1.Text = rst!employeeidText2.Text = rst!firstnameText3.Text = rst!birthdateEnd Sub
CUSTOMER Form :
Public cnn As ADODB.ConnectionPublic rst As ADODB.Recordset
Private Sub Form_Load()
Set cnn = New ADODB.Connectioncnn.Open _ConnectionString:="Provider=Microsoft.Jet.OLEDB.3.51;" & _"Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb"Set rst = New ADODB.Recordsetrst.Open Source:="customers", ActiveConnection:=cnn, _CursorType:=adOpenDyanamic, LockType:=adLockOptimistic, _Options:=adCmdTableCall movefield
-
8/8/2019 VB Assignments
20/21
Assignments
Training Division, NIC, New Delhi
R 20
End Sub
Sub movefield()rst.MoveNextText1.Text = rst!customeridText2.Text = rst!CompanyName
End Sub
Sub moveprev()rst.MovePreviousText1.Text = rst!customeridText2.Text = rst!CompanyNameEnd Sub
? Write the following code in the MDI Form
Private Sub mnucas_Click() (Option : Window>>Cascade)MDIForm1.Arrange vbCascadeEnd Sub
Private Sub mnuForm1_Click() (Option : Data Editing >> Customer Details)Form1.ShowEnd Sub
Private Sub mnuform2_Click() (Option : Data Editing >> Employee Details)Form2.ShowEnd Sub
Private Sub mnunext_Click() (Option : Navigation>>Move Next)ActiveForm.movefieldEnd Sub
Private Sub mnuprev_Click() (Option : Navigation>>Move Previous)ActiveForm.moveprevEnd Sub
Private Sub mnutile_Click() (Option : Window>>Tile)MDIForm1.Arrange vbTileHorizontalEnd Sub
?
Make the MDI Form as startup object.
? Run the Project.
-
8/8/2019 VB Assignments
21/21
Assignments
Training Division, NIC, New Delhi
?Sample Code to List a field in the Data Combo using the
component Microsoft DataList Control 6.0 (OLEDB)
In this sample database is lying at SQL server named cabin785a, with the namecoursedata. Table name is course, field name is title.
Way 1: By setting the properties
Step 1: Add ADO Data Control from Project>>Components to the Toolbox.Step 2: Create ADO on the form and set its properties to connect it to the above statedspecifications.
Step 3: Add Microsoft DataList Control 6.0 (OLEDB) from Project>>Components to theToolbox.
Step 4: Create DataCombo on the form and set its following properties:
Property Value
RowSource Adodc1(Name of ADO control)
ListField Title
Way 2: Through ADO Programming
Dim cnn As New ADODB.Connection
Dim rs As ADODB.Recordset
Set cnn = New ADODB.Connectioncnn.ConnectionString = _"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial
Catalog=coursedata;Data Source=cabin785a"
cnn.Open
Set rs = New ADODB.Recordset
rs.Open Source:="course", ActiveConnection:=cnn, CursorType:=adOpenKeyset,LockType:=adLockOptimistic, Options:=adCmdTable
Set DataCombo1.RowSource = rsDataCombo1.ListField = "title"