Practical 1 : Listbox and Combo Box
Private Sub Command1_Click()
List1.AddItem Text1.Text
Text1.SetFocus
Command1.Enabled = False
Label1.Caption = List1.ListCount
End Sub
Private Sub Command2_Click()
Dim Remove As Integer
Remove = List1.ListIndex
List1.RemoveItem Remove
Label1.Caption = List1.ListCount
End Sub
Private Sub Command3_Click()
List1.Clear
Label1.Caption = List1.ListCount
End Sub
Private Sub Text1_Change()
If (Len(Text1.Text) > 0) Then
Command1.Enabled = True
End If
End Sub
Horizontal Scroll Bar
Private Sub Command1_Click()
Dim I As Integer
Dim J As Integer
I = 1
Do While I < HScroll1.Value
J = J + I
I = I + 1
Loop
Text1.Text = Str(J)
End Sub
Check Box and Option button Font and Color
Private Sub chkBold_Click()
If chkBold.Value = 1 Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
End Sub
Private Sub chkitalic_Click()
If chkitalic.Value = 1 Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
End If
End Sub
Private Sub chkunderline_Click()
If chkunderline.Value = 1 Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
End Sub
Private Sub optblue_Click()
Text1.ForeColor = vbBlue
End Sub
Private Sub optgreen_Click()
Text1.ForeColor = vbGreen
End Sub
Private Sub optred_Click()
Text1.ForeColor = vbRed
End Sub
Timer Control
Practical no. 5 : TIMER CONTROL
set timer at enable=false interval=250
Start a new VB project and put 2 command buttons and a timer control on the form. set the properties of the controls as follows:
Command button - property - Name: cmdStart Caption: start timer
Command button - name: cmdStop Caption: StopTimer
Timer: Name: tmr Test Enabled:False Interval:250
In General declaration:
Private mintcount as integer
Code the click event for the 2 command button and the timer event for the timer control as follows:
Private sub cmdstart_click()
mint count=0
timer1. enable =true
end sub
Private sub cmdstop_click()
timer1.enable=false
end sub
Private sub Timer1_timer()
mint count+1
Print "timer fine origin count =" & mint count
end sub
Shipment Cost: Checkbox and option button
Private Sub CmdCalcost_Click()
Dim unitorder As Integer
Dim cost As Currency
unitorder = Val(txtbox1.Text)
If optWholesaler.Value = 1 Then
Select Case unitorder
Case 1 To 15
cost = unitorder * 50
Case 16 To 20
cost = unitorder * 45
Case 21 To 30
cost = unitorder * 40
Case 31 To 50
cost = unitorder * 35
Case Is > 50
cost = unitorder * 30
End Select
Else
Select Case unitorder
Case 1 To 15
cost = unitorder * 60
Case 16 To 20
cost = unitorder * 55
Case 21 To 30
cost = unitorder * 50
Case 31 To 50
cost = unitorder * 45
Case Is > 50
cost = unitorder * 40
End Select
End If
If chkdisc1.Value = 1 Then
cost = cost - (cost * 0.1)
End If
TextBox2.Text = cost
End Sub
Traffic Signal
Design an application to display traffic lights. All lights for 9 secs and yellow for 5 secs.
1. start VB & create a new project
2. Save the form & project as frmtraffic.frm
3. set the caption of your form as 'Traffic Light'
4. Now create the controls & place them according to the screenshot shows above.
You can draw the desired shape by setting the shape property. set the shape property to 3-circle to obtain circles.
5. also name the controls as labelled in the screenshots.
6. now set the properties of 3 inner circles as follows:
Name Property Setting
shpRed Backcolor VbRed
Backstyle 1-opaque
shpYellow same as above VbWhite
shpGreen same as above vbWhite
tmrlights Enabled True
7. Go to the code view: type option explicit at the top of the code page. as declare the variable state as shown below.
Dim state as integer
8. Now double click the event for change(cmdchange) button :
Private sub cmdchange_click()
state=1
shpRed.backcolor=VbRed
shpyellow.backcolor=Vbwhite
shpgreen.backcolor=VbWhite
label1.caption="stop"
Timer1.enable=true
End Sub
Create the timer event for timer tmrlights
now double click the timer and write the following code:
Private Sub Timer1_Timer()
If state=3
state=1
Else
state=state+1
End If
Select state
Case1
ShpRed.back color=VbRed
ShpYellow.back color=VbWhite
ShpGreen.back color=VbWhite
Label1.caption="Stop"
Timer1.Interval=250
Case2
ShpRed.backcolor=Vbwhite
ShpYellow.backcolor=Vbyellow
ShpGreen.backcolor=Vbwhite
label1.caption="wait"
Timer1.Interval=250
Case3
ShpRed.backcolor=VbWhite
ShpYellow.backcolor=VbWhite
ShpGreen.backcolor=VbGreen
label1.caption="Go"
Timer.Interval=250
End Select
End Sub
ADODC through coding
Public conn As ADODB.Connection
Public rs As ADODB.Recordset
Private Sub Command1_Click()
rs.MoveFirst
End Sub
Private Sub Command2_Click()
rs.MovePrevious
If rs.BOF Then
rs.MoveLast
End If
End Sub
Private Sub Command3_Click()
rs.MoveNext
If rs.EOF Then
rs.MoveFirst
End If
End Sub
Private Sub Command5_Click()
rs.AddNew
End Sub
Private Sub Command6_Click()
rs.Update
End Sub
Private Sub Command7_Click()
rs.Delete
rs.Update
End Sub
Private Sub Form_Load()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\ADMIN\My Documents\employee.mdb;Persist Security Info=False"
conn.Open
Set rs = New ADODB.Recordset
rs.Open "emp", conn, adOpenStatic, adLockOptimistic, adCmdTable
Set Text1.DataSource = rs
Text1.DataField = "empno"
Set Text2.DataSource = rs
Text2.DataField = "job"
Set Text3.DataSource = rs
Text3.DataField = "salary"
Set Text4.DataSource = rs
Text4.DataField = "empname"
Set Text5.DataSource = rs
Text5.DataField = "deptno"
Set Text6.DataSource = rs
Text6.DataField = "manager"
Set MSHFlexGrid1.DataSource = rs
End Sub
DAO through coding
Private Sub Command1_Click()
Data1.Recordset.MoveFirst
End Sub
Private Sub Command2_Click()
Data1.Recordset.MovePrevious
If Data1.Recordset.BOF = True Then
Data1.Recordset.MoveLast
End If
End Sub
Private Sub Command3_Click()
Data1.Recordset.MoveNext
If Data1.Recordset.EOF = True Then
Data1.Recordset.MoveFirst
End If
End Sub
Private Sub Command4_Click()
Data1.Recordset.MoveLast
End Sub
Private Sub Command5_Click()
Data1.Recordset.AddNew
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = True
Command7.Enabled = False
End Sub
Private Sub Command6_Click()
Command1.Enabled = True
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command7.Enabled = True
If (MsgBox("do u want to save data", vbYesNo) = vbYes) Then
Data1.Recordset.Update
End If
End Sub
Private Sub Command7_Click()
Data1.Recordset.Delete
Data1.Recordset.MoveNext
End Sub
Private Sub Command1_Click()
List1.AddItem Text1.Text
Text1.SetFocus
Command1.Enabled = False
Label1.Caption = List1.ListCount
End Sub
Private Sub Command2_Click()
Dim Remove As Integer
Remove = List1.ListIndex
List1.RemoveItem Remove
Label1.Caption = List1.ListCount
End Sub
Private Sub Command3_Click()
List1.Clear
Label1.Caption = List1.ListCount
End Sub
Private Sub Text1_Change()
If (Len(Text1.Text) > 0) Then
Command1.Enabled = True
End If
End Sub
Horizontal Scroll Bar
Private Sub Command1_Click()
Dim I As Integer
Dim J As Integer
I = 1
Do While I < HScroll1.Value
J = J + I
I = I + 1
Loop
Text1.Text = Str(J)
End Sub
Check Box and Option button Font and Color
Private Sub chkBold_Click()
If chkBold.Value = 1 Then
Text1.FontBold = True
Else
Text1.FontBold = False
End If
End Sub
Private Sub chkitalic_Click()
If chkitalic.Value = 1 Then
Text1.FontItalic = True
Else
Text1.FontItalic = False
End If
End Sub
Private Sub chkunderline_Click()
If chkunderline.Value = 1 Then
Text1.FontUnderline = True
Else
Text1.FontUnderline = False
End If
End Sub
Private Sub optblue_Click()
Text1.ForeColor = vbBlue
End Sub
Private Sub optgreen_Click()
Text1.ForeColor = vbGreen
End Sub
Private Sub optred_Click()
Text1.ForeColor = vbRed
End Sub
Timer Control
Practical no. 5 : TIMER CONTROL
set timer at enable=false interval=250
Start a new VB project and put 2 command buttons and a timer control on the form. set the properties of the controls as follows:
Command button - property - Name: cmdStart Caption: start timer
Command button - name: cmdStop Caption: StopTimer
Timer: Name: tmr Test Enabled:False Interval:250
In General declaration:
Private mintcount as integer
Code the click event for the 2 command button and the timer event for the timer control as follows:
Private sub cmdstart_click()
mint count=0
timer1. enable =true
end sub
Private sub cmdstop_click()
timer1.enable=false
end sub
Private sub Timer1_timer()
mint count+1
Print "timer fine origin count =" & mint count
end sub
Shipment Cost: Checkbox and option button
Private Sub CmdCalcost_Click()
Dim unitorder As Integer
Dim cost As Currency
unitorder = Val(txtbox1.Text)
If optWholesaler.Value = 1 Then
Select Case unitorder
Case 1 To 15
cost = unitorder * 50
Case 16 To 20
cost = unitorder * 45
Case 21 To 30
cost = unitorder * 40
Case 31 To 50
cost = unitorder * 35
Case Is > 50
cost = unitorder * 30
End Select
Else
Select Case unitorder
Case 1 To 15
cost = unitorder * 60
Case 16 To 20
cost = unitorder * 55
Case 21 To 30
cost = unitorder * 50
Case 31 To 50
cost = unitorder * 45
Case Is > 50
cost = unitorder * 40
End Select
End If
If chkdisc1.Value = 1 Then
cost = cost - (cost * 0.1)
End If
TextBox2.Text = cost
End Sub
Traffic Signal
Design an application to display traffic lights. All lights for 9 secs and yellow for 5 secs.
1. start VB & create a new project
2. Save the form & project as frmtraffic.frm
3. set the caption of your form as 'Traffic Light'
4. Now create the controls & place them according to the screenshot shows above.
You can draw the desired shape by setting the shape property. set the shape property to 3-circle to obtain circles.
5. also name the controls as labelled in the screenshots.
6. now set the properties of 3 inner circles as follows:
Name Property Setting
shpRed Backcolor VbRed
Backstyle 1-opaque
shpYellow same as above VbWhite
shpGreen same as above vbWhite
tmrlights Enabled True
7. Go to the code view: type option explicit at the top of the code page. as declare the variable state as shown below.
Dim state as integer
8. Now double click the event for change(cmdchange) button :
Private sub cmdchange_click()
state=1
shpRed.backcolor=VbRed
shpyellow.backcolor=Vbwhite
shpgreen.backcolor=VbWhite
label1.caption="stop"
Timer1.enable=true
End Sub
Create the timer event for timer tmrlights
now double click the timer and write the following code:
Private Sub Timer1_Timer()
If state=3
state=1
Else
state=state+1
End If
Select state
Case1
ShpRed.back color=VbRed
ShpYellow.back color=VbWhite
ShpGreen.back color=VbWhite
Label1.caption="Stop"
Timer1.Interval=250
Case2
ShpRed.backcolor=Vbwhite
ShpYellow.backcolor=Vbyellow
ShpGreen.backcolor=Vbwhite
label1.caption="wait"
Timer1.Interval=250
Case3
ShpRed.backcolor=VbWhite
ShpYellow.backcolor=VbWhite
ShpGreen.backcolor=VbGreen
label1.caption="Go"
Timer.Interval=250
End Select
End Sub
ADODC through coding
Public conn As ADODB.Connection
Public rs As ADODB.Recordset
Private Sub Command1_Click()
rs.MoveFirst
End Sub
Private Sub Command2_Click()
rs.MovePrevious
If rs.BOF Then
rs.MoveLast
End If
End Sub
Private Sub Command3_Click()
rs.MoveNext
If rs.EOF Then
rs.MoveFirst
End If
End Sub
Private Sub Command5_Click()
rs.AddNew
End Sub
Private Sub Command6_Click()
rs.Update
End Sub
Private Sub Command7_Click()
rs.Delete
rs.Update
End Sub
Private Sub Form_Load()
Set conn = New ADODB.Connection
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\ADMIN\My Documents\employee.mdb;Persist Security Info=False"
conn.Open
Set rs = New ADODB.Recordset
rs.Open "emp", conn, adOpenStatic, adLockOptimistic, adCmdTable
Set Text1.DataSource = rs
Text1.DataField = "empno"
Set Text2.DataSource = rs
Text2.DataField = "job"
Set Text3.DataSource = rs
Text3.DataField = "salary"
Set Text4.DataSource = rs
Text4.DataField = "empname"
Set Text5.DataSource = rs
Text5.DataField = "deptno"
Set Text6.DataSource = rs
Text6.DataField = "manager"
Set MSHFlexGrid1.DataSource = rs
End Sub
DAO through coding
Private Sub Command1_Click()
Data1.Recordset.MoveFirst
End Sub
Private Sub Command2_Click()
Data1.Recordset.MovePrevious
If Data1.Recordset.BOF = True Then
Data1.Recordset.MoveLast
End If
End Sub
Private Sub Command3_Click()
Data1.Recordset.MoveNext
If Data1.Recordset.EOF = True Then
Data1.Recordset.MoveFirst
End If
End Sub
Private Sub Command4_Click()
Data1.Recordset.MoveLast
End Sub
Private Sub Command5_Click()
Data1.Recordset.AddNew
Command1.Enabled = False
Command2.Enabled = False
Command3.Enabled = False
Command4.Enabled = False
Command5.Enabled = False
Command6.Enabled = True
Command7.Enabled = False
End Sub
Private Sub Command6_Click()
Command1.Enabled = True
Command2.Enabled = True
Command3.Enabled = True
Command4.Enabled = True
Command5.Enabled = True
Command7.Enabled = True
If (MsgBox("do u want to save data", vbYesNo) = vbYes) Then
Data1.Recordset.Update
End If
End Sub
Private Sub Command7_Click()
Data1.Recordset.Delete
Data1.Recordset.MoveNext
End Sub
No comments:
Post a Comment
You can ask your queries here and we will try to get back to you as soon as we will be done working with that..
Note: Only a member of this blog may post a comment.