Sei sulla pagina 1di 21

Assignments

? Assignments in Controls

? Assignment 1: Usage of TEXTBOX & COMMAND BUTTON

Objective: Creation of a form using Text box and Command button controls, in which
the text “Starting of Visual Basic” should appear in the Textbox, on clicking
the command but ton OK

TextBox

Command Button

Steps for designing the above form

? Open new form in your project by clicking Project>>Add form, then selecting the
form 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 Name txtString
Text <Empty>
Command Name cmdOK txtString.Text=”Starting of Visual
Button1 Basic”
Caption OK

? Save and execute the form.

Training Division, NIC, New Delhi


R 1
Assignments

? Assignment 2: Usage of Frame, Check box and Radio Buttons with


the Text box

Objective: Conversion of text in Upper-case or in a Lower-case, Bold, Italic and


Underline as desired.

Text Box

Radio Button Check Box


Frame
Steps for designing the above form

? Open new form in your project by clicking Project>>Add form, then selecting the
form option.
? Create the following objects in the blank form:

Object Property Value Code


Text Box Name txtInput

Text <Empty>
Radio Button1 Name Option1 Private Sub Option1_Click( )
If Option1.Value = True Then
Caption UpperCase txtInput.Text = UCase(txtInput.Text)
End If
End Sub
Radio Button2 Name Option2 Private Sub Option2_Click( )
If Option2.Value = True Then
Caption LowerCase txtInput.Text = LCase(txtInput .Text)
End If
End Sub

Training Division, NIC, New Delhi


R 2
Assignments

Click Box1 Name Checkbold Private Sub checkbold_Click( )


If checkbold.Value = 1Then
Caption Bold txtInput.FontBold = True
Else
txtInput.FontBold = False
End If
End Sub
Click Box2 Name Checkitalic Private Sub checkitalic_Click( )
If checkitalic.Value = 1 Then
Caption Italic txtInput.Fontitalic = True
Else
txtInput.Fontitalic = False
End If
End Sub
Click Box3 Name checkUnder Private Sub checkUnderline_Click( )
line If checkUnderline.Value = 1 Then
Caption txtInput.FontUnderline = True
Underline Else
txtInput.FontUnderline = False
End If
End Sub
Text Box Text <Empty>

? 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.

Training Division, NIC, New Delhi


R 3
Assignments

? 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 through
entry in the Text box

Combo Box

List Box

Steps for designing the above form

? Open new form in your project by clicking Project>>Add form, then selecting the
form option.
? Create the following objects in the blank form:
Object Property Value Code
Text Box Name txtBox

Text <Empty>
Command Name cmdAddItem Private Sub CmdAddItem_Click( )
Button1 Dim Data As String
Caption Add Item to Data = txtBox.Text
List lstBox.AddItem Data
cboBox.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

Training Division, NIC, New Delhi


R 4
Assignments

? Assignment 4: Usage of OLE Control (Object Linking and


Embedding)

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 the
form option.
? Create the following objects in the blank form:

Object Property Value Code


Label Caption Use of OLE

Font Bold, Size 14


Command 1 Caption Quit Private Sub Command1_Click( )
Button End
End Sub

Training Division, NIC, New Delhi


R 5
Assignments

OLE1 BorderStyle 0- None


Control Appearance 0- Flat
BackColor Light Grey
OLE2 BorderStyle 0- None
Control Appearance 0- Flat
BackColor Light Grey
OLE3 BorderStyle 0- None
Control Appearance 0- Flat
BackColor Light Grey

? When you select OLE object from tools box and drag it to the form, the following
screen appears.

? Scroll down the list of objects, and click on Microsoft Word


? Check Display as Icon
? Click OK 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.

Training Division, NIC, New Delhi


R 6
Assignments

? 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 the
form option.
? Create the following objects in the blank form:

Object Property Value Code


Timer1 Caption Timer Private Sub Timer1_Timer( )
MsgBox (“Sorry, Yo ur time is up.”)
Interval 1500 End Sub
Lable Caption Use of OLE
Font Bold, Size 14
Command 1 Caption Quit Private Sub Command1_Click( )
Button If Text1.Text = “secret” Then
Timer1.Enabled = False
MsgBox (“Welcome To the
System”)
Else
MsgBox (“Sorry, Friend, I don’t
know you.”)
End If
End Sub

? Now run the program.


? Enter the Password and see the output.

Training Division, NIC, New Delhi


R 7
Assignments

? 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 image
in the image Control Space.

Drive List Box

Directory ListBox Image Control

File ListBox

Fig. 1
Steps for designing the above form
? Open new form in your project by clicking Project>>Add form, and then
selecting the Form option.
? Bring the following objects in the blank form and set their respective properties:
Object Properties Value Code
Drive listbox Name Drive1 Private Sub Drive1_Change( )
Dir1.path = Drive1.Drive
End Sub
Directorylistbox Name Dir1 Private Sub Dir1_Click( )
File1.path = Dir1.Path
End Sub
Image Control Name Image1

Stretch True
Filelistbox Name File1 Private Sub File1_Click( )
SelectFile = File1.path & “\” & File1.filename
Image1.Picture= LoadPicture(SelectFile)
End Sub

Note: Ensure that `Click’ event procedure is selected

? Save and execute the form.

Training Division, NIC, New Delhi


R 8
Assignments

? Assignment 2 Use of If...Then…Else, decision structure.

Example 1: Lucky Seven

Fig. 2
Description: In this form, when Spin button is clicked, three numbers appear in the
labels. In case any of the numbers is 7, the picture of Coins appears, otherwise it
does 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) Then
Image1.Visible = True
Beep
End If
End Sub

Hint: Get the picture of Coins from the file:


C:\Program Files\Microsoft Visual Studio\Common\Graphics\Metafile\Business\Coins

Training Division, NIC, New Delhi


R 9
Assignments

? Assignment 3 Use of If...Then…Else, decision structure (contd.).

Example 2: Age factor

Fig. 3

Description: In this form, you are supposed to type your age in the text box and
then press the Submit button. Based on the age typed, some message will appear
in 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 Integer
Age = txtAge.Text
If Age >= 13 And Age < 20 Then
lblMessage.Caption = "You are a teenager"
ElseIf Age <= 12 Then
lblMessage.Caption = "That's a nice Age to be"
ElseIf Age >= 21 And Age < 58 Then
lblMessage.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

Training Division, NIC, New Delhi


R 10
Assignments

? Assignment 4 Use of Select Case Decision Structure

Fig. 4

Objective: The functionality of this form, as discussed before, can also be achieved
with the help of another decision structure called Select Case.

Description: Same as in Examp le 2, Assignment 3

Code for Submit button (using Select Case )


Private Sub cmdSubmit_Click()
Age = txtAge.Text
Select Case Age
Case Is < 13
lblMessage.Caption = "That's a nice age to be"
Case 13 To 19
lblMessage.Caption = "You are a teenager"
Case 20 To 58
lblMessage.Caption = "Need to be More responsible now"
Case Is > 58
lblMessage.Caption = "Enjoy retired but Fruitful life"
End Select
End Sub

Code for Type Again Button

txtAge.Text = ""
lblMessage.Caption = ""
txtAge.SetFocus

Training Division, NIC, New Delhi


R 11
Assignments

? Assignment 4 Use of For….Next Loop

Fig. 5

Description: When the Loop button is clicked, the text "NIC" appears in different
font sizes, line by line.

Code for Loop Button

Private Sub cmdLoop_Click()


Dim Count As Integer
For Count = 1 To 10
FontSize = 10 + Count
Print "NIC"; Count
Next Count
End Sub

Try this: For …Step…Next Loop

In the above example, replace

For Count = 1 To 10
FontSize = 10 + Count
Print "NIC"; Count
Next Count
with
For Count = 10 To 20 Step 2
FontSize = 10 + Count
Print "NIC"; Count
Next Count

and run the form to see the results

Training Division, NIC, New Delhi


R 12
Assignments

? Assignment 5 Use of Do…While Loop

Fig. 5

Description: When the Loop button is clicked, the text "NIC" appears in different
font sizes, line by line.

Code for Loop Button

Private Sub cmdLoop_Click()


Dim Count As Integer
For Count = 1 To 10
FontSize = 10 + Count
Print "NIC"; Count
Next Count
End Sub

Try this: For …Step…Next Loop

In the above exa mple, replace

For Count = 1 To 10
FontSize = 10 + Count
Print "NIC"; Count
Next Count
with
For Count = 10 To 20 Step 2
FontSize = 10 + Count
Print "NIC"; Count
Next Count

and run the form to see the results

Training Division, NIC, New Delhi


R 13
Assignments

? Assignment 6 Use of Do While…Loop

Fig. 6
Description : When the Do..While option is clicked under Loops menu , a dialog box
appears. 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 Do…While menu item under Loops menu

Private Sub doopt1_Click()


Dim x, p As Integer
x = InputBox("Enter the max limit")
Do While p < x
p=p+1
Print p
Loop
End Sub

In the above example replace


Dim x, p As Integer
x = InputBox("Enter the max limit")
Do While p < x
p=p+1
Print p
Loop
with
Dim x, p As Integer
x = InputBox("Enter the max limit")
Do Until p = x

Training Division, NIC, New Delhi


R 14
Assignments

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 Integer
x = InputBox("Enter the max limit")
While p < x
p=p+1
Print p
Wend

Training Division, NIC, New Delhi


R 15
Assignments

? Assignment 7 Use of Arrays

Fig. 7

Description : Create array of 10 text boxes and a button with caption RESET on the
form. 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 = False
For i = 0 To 9
Text1(i).Visible = False
Next
End Sub

Code Arrays menu item

Private Sub cmdarrays_Click()


Command1.Visible = True
For i = 1 To 10
Text1(i - 1).Visible = True
Text1(i - 1).Text = "2 * " & i & " = " & 2 * i
Next
End Sub

Training Division, NIC, New Delhi


R 16
Assignments

Code for RESET button on the main form

Private Sub Command1_Click()


Command1.Visible = False
For i = 0 To 9
Text1(i).Visible = False
Next
End Sub

Training Division, NIC, New Delhi


R 17
Assignments

? Assignments in MDI Forms

? Open the Project.


? Select Project>>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 products
is used.

Training Division, NIC, New Delhi


R 18
Assignments

Set the MDIChild property to TRUE for both the forms.

? Write the following piece of code on the respective forms


EMPLOYEE Form :

Public cnn As ADODB.Connection


Public rst As ADODB.Recordset

Private Sub Form_Load()


Set cnn = New ADODB.Connection
cnn.Open _
ConnectionString:="Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb"
Set rst = New ADODB.Recordset
rst.Open Source:="employees", ActiveConnection:=cnn, _
CursorType:=adOpenDyanamic, LockType:=adLockOptimistic, _
Options:=adCmdTable
Call movefield
End Sub

Sub movefield()
rst.MoveNext
Text1.Text = rst!employeeid
Text2.Text = rst!firstname
Text3.Text = rst!birthdate
End Sub

Sub moveprev()
rst.MovePrevious
Text1.Text = rst!employeeid
Text2.Text = rst!firstname
Text3.Text = rst!birthdate
End Sub

CUSTOMER Form :

Public cnn As ADODB.Connection


Public rst As ADODB.Recordset

Private Sub Form_Load()


Set cnn = New ADODB.Connection
cnn.Open _
ConnectionString:="Provider=Microsoft.Jet.OLEDB.3.51;" & _
"Data Source=C:\Program Files\Microsoft Visual Studio\VB98\Nwind.mdb"
Set rst = New ADODB.Recordset
rst.Open Source:="customers", ActiveConnection:=cnn, _
CursorType:=adOpenDyanamic, LockType:=adLockOptimistic, _
Options:=adCmdTable
Call movefield

Training Division, NIC, New Delhi


R 19
Assignments

End Sub

Sub movefield()
rst.MoveNext
Text1.Text = rst!customerid
Text2.Text = rst!CompanyName
End Sub

Sub moveprev()
rst.MovePrevious
Text1.Text = rst!customerid
Text2.Text = rst!CompanyName
End Sub

? Write the following code in the MDI Form


Private Sub mnucas_Click() (Option : Window>>Cascade)
MDIForm1.Arrange vbCascade
End Sub

Private Sub mnuForm1_Click() (Option : Data Editing >> Customer Details)


Form1.Show
End Sub

Private Sub mnuform2_Click() (Option : Data Editing >> Employee Details)


Form2.Show
End Sub

Private Sub mnunext_Click() (Option : Navigation>>Move Next)


ActiveForm.movefield
End Sub

Private Sub mnuprev_Click() (Option : Navigation>>Move Previous)


ActiveForm.moveprev
End Sub

Private Sub mnutile_Click() (Option : Window>>Tile)


MDIForm1.Arrange vbTileHorizontal
End Sub

? Make the MDI Form as startup object.


? Run the Project.

Training Division, NIC, New Delhi


R 20
Assignments

? 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 name
coursedata. 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 stated
specifications.
Step 3: Add Microsoft DataList Control 6.0 (OLEDB) from Project>>Components to the
Toolbox.
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.Connection


cnn.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 = rs
DataCombo1.ListField = "title"

Training Division, NIC, New Delhi


R 21

Potrebbero piacerti anche