Vous êtes sur la page 1sur 48

Visual Basic 6

Practical Solutions

for

T.Y.B.Sc.(I.T.) 2009 - 2010

Guided by – Hon. Prof. Mr. Vahid Kapadia

Compiled by –
Sibtain Masih
Student of I.T. Department,
AP College,
Grant Road,
Mumbai – 400 008
Q.1. Create a project for Coffee Shop?

Form Design-
Code-

Option Explicit
Dim mcurSubTotal As Currency
Dim mcurTotal As Currency
Dim mcurGrandTotal As Currency
Dim mintCustomerCount As Integer

Private Sub cmdCalculate_Click()


Dim curPrice As Currency
Dim intQuantity As Integer
Dim curTax As Currency
Dim curItemAmount As Currency

Const curTaxRate = 0.08


Const curCappuccinoPrice As Currency = 2
Const curExpressoPrice As Currency = 2.25
Const curLattePrice As Currency = 1.75
Const curIcedPrice As Currency = 2.5

If optCappuccino.Value = True Then


curPrice = curCappuccinoPrice
ElseIf optEspresso.Value = True Then
curPrice = curExpressoPrice
ElseIf optLatte.Value = True Then
curPrice = curLattePrice
ElseIf optIcedCappuccino.Value = True Or optIcedLatte.Value = True Then
curPrice = curIcedPrice
End If

If IsNumeric(txtQuantity) Then
intQuantity = Val(txtQuantity)
curItemAmount = curPrice * intQuantity
mcurSubTotal = mcurSubTotal + curItemAmount

If chkTax.Value = vbChecked Then


curTax = mcurSubTotal * curTaxRate
End If

mcurTotal = mcurSubTotal + curTax

lblItemAmount = FormatCurrency(curItemAmount)
lblSubTotal = FormatNumber(mcurSubTotal)
lblTax = FormatNumber(curTax)
lblTotal = FormatCurrency(mcurTotal)
Else
MsgBox "Quantity must be numeric.", vbExclamation, "Numeric Test Failed"
txtQuantity = Empty
txtQuantity.SetFocus
End If
End Sub

Private Sub cmdClear_Click()


If mcurSubTotal <> 0 Then 'Clear only if calculation made
optCappuccino.Value = True
optEspresso.Value = False
optLatte.Value = False
optIcedCappuccino.Value = False
optIcedLatte.Value = False
lblItemAmount = Empty
chkTax.Enabled = False 'Allow change for new order only
With txtQuantity
.Text = Empty
.SetFocus
End With
Else
MsgBox "No New Order to Clear.", vbExclamation, "Customer Order"
End If
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdNewOrder_Click()


'Clear the current order and add to totals
cmdClear_Click
lblSubTotal = Empty
lblTax = Empty
lblTotal = Empty

'Add to totals
If mcurSubTotal <> 0 Then 'Should not be able add to counts if no new order/customer
'Preven accidential clicking

mcurGrandTotal = mcurGrandTotal + mcurTotal


mcurSubTotal = 0
mcurTotal = 0 'Reset for next customer

mintCustomerCount = mintCustomerCount + 1
End If
'Display appropriate display item and enable check box
With chkTax
.Enabled = True
.Value = vbChecked
End With
End Sub

Private Sub cmdSummary_Click()


'Calculate the avrage and display total

Dim curAvg As Currency


Dim strMsg As String
Dim strFormattedAvg As String

If mintCustomerCount > 0 Then


If mcurTotal <> 0 Then
cmdNewOrder_Click 'Make sure last order is counted
curAvg = mcurGrandTotal / mintCustomerCount
'Format the numbers
strFormattedAvg = FormatCurrency(curAvg)
'Concatenate the message string
strMsg = "Orders: " & mintCustomerCount & vbCrLf & "Average Sale: " &
strFormattedAvg
MsgBox strMsg, vbInformation, "Coffee Sale Summary"
End If
Else
MsgBox "No data to summarize.", vbExclamation, "Coffee Sale Summary"
End If
End Sub
Screen Shot-
Q.2. Create a project to read the income and calculate Income Tax, Surcharge and Net
income. Where Income Tax is
Income Tax
First 1,00,000 Nil
Next 1,00,000 10%
Next 50,000 20%
Next 50,000 30%
Excess 35%
Surcharge=5% of Income Tax if income is greater than 500000?

Form Design-
Code-

Private Sub cmdCalculate_Click()


Dim mIncome As Currency, mTax As Currency, mSurcharge As Currency

mIncome = Val(txtIncome)

'Income Tax Calculation


If mIncome <= 100000 Then
mTax = 0
ElseIf mIncome <= 200000 Then
mTax = (mIncome - 100000) * 0.1
ElseIf mIncome <= 250000 Then
mTax = 100000 * 0.1 + (mIncome - 200000) * 0.2
ElseIf mIncome <= 300000 Then
mTax = 100000 * 0.1 + 50000 * 0.2 + (mIncome - 250000) * 0.3
Else
mTax = 100000 * 0.1 + 50000 * 0.2 + 50000 * 0.3 + (mIncome - 300000) * 0.35
End If

'Surcharge Calculation
If mIncome > 500000 Then
mSurcharge = mTax * 0.05
Else
mSurcharge = 0
End If

txtTax = mTax
txtSurcharge = mSurcharge
txtNetIncome = mIncome - (mTax + mSurcharge)

End Sub

Private Sub cmdClear_Click()


txtIncome = Empty
txtTax = Empty
txtSurcharge = Empty
txtNetIncome = Empty
txtIncome.SetFocus
End Sub

Private Sub cmdExit_Click()


End
End Sub
Private Sub txtIncome_KeyPress(KeyAscii As Integer)
If (KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 8 Then
'Do Nothing
Else
KeyAscii = 0
End If

End Sub
Screen Shot-
Q.3. Create a project to read the marks of I, II, III and IV semester of B.Sc.(I.T.) students
and calculate total and average?

Form Design-
Code-

Private Sub cmdOK_Click()


frmSemMarks.Hide
frmResult.Show

frmResult.txtTotal = Val(frmSemMarks.txt1) + Val(frmSemMarks.txt2) +


Val(frmSemMarks.txt3) + Val(frmSemMarks.txt4) + Val(frmSemMarks.txt5)
frmResult.txtAverage = Val(frmResult.txtTotal) / 5

End Sub

Private Sub Form_Activate()


txt1.SetFocus
End Sub

Private Sub txt1_KeyPress(KeyAscii As Integer)


KeyAscii = checkVal(KeyAscii)
End Sub

Private Sub txt2_KeyPress(KeyAscii As Integer)


KeyAscii = checkVal(KeyAscii)
End Sub

Private Sub txt3_KeyPress(KeyAscii As Integer)


KeyAscii = checkVal(KeyAscii)
End Sub

Private Sub txt4_KeyPress(KeyAscii As Integer)


KeyAscii = checkVal(KeyAscii)
End Sub

Private Sub txt5_KeyPress(KeyAscii As Integer)


KeyAscii = checkVal(KeyAscii)
End Sub
Public Function checkVal(Ascii As Integer) As Integer
If (Ascii >= 48 And Ascii <= 57) Or (Ascii = 8) Then
checkVal = Ascii
Else
checkVal = 0
End If
End Function
Screen Shots-
Q.4. Create a project to cover different properties of combo box and list box?

Form Design-
Code-

Private Sub cmdAddToCombo_Click()


If (cbo1.Text = Empty) Then
MsgBox "Enter Text in the Combo box text field !", vbOKOnly, "Error"
Else
If (MsgBox("Do you want to add " + cbo1.Text + " to the combo box?", vbYesNo,
"Add to Combo") = vbYes) Then cbo1.AddItem (cbo1.Text)
End If
End Sub

Private Sub cmdAddToList_Click()


If (MsgBox("Do you want to add " + cbo1.Text + " to the list box?", vbYesNo, "Add to
List") = vbYes) Then lst1.AddItem (cbo1.List(cbo1.ListIndex))
End Sub

Private Sub cmdClearList_Click()


If lst1.ListCount = 0 Then
MsgBox "No item in the list", vbOKOnly, "Clear List"
Else
lst1.Clear
MsgBox "List cleared!", vbOKOnly, "Clear List"
End If
End Sub

Private Sub cmdCount_Click()


MsgBox "Total Items in the list are : " & Str(lst1.ListCount)
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdRemoveItem_Click()


If (MsgBox("Do you want to remove " + lst1.Text + " from the list box?", vbYesNo,
"Remove Item") = vbYes) Then lst1.RemoveItem (lst1.ListIndex)
End Sub

Private Sub Form_Load()


lst1.Clear

cbo1.AddItem "Apple"
cbo1.AddItem "Mango"
cbo1.AddItem "Grapes"
cbo1.AddItem "Banana"
End Sub
Screen Shot-
Q.5. Create a project to maintain form no, name, percentage, maths marks and category
of students in a random file. Create a user defined type for maintaining the details. Allow
the user to ADD, MOODIFY and DELETE details. Use a list box to display the name of
the student to the user. Provide a SHOW button to display all the details in text boxes
after selecting the name from the list?

Form Design-

Code-

Option Explicit
Private Type Student
curFormNo As Currency
strName As String * 10
curPercentage As Currency
curMathsMarks As Currency
strCategory As String * 5
End Type
Dim mudtStudent As Student

Private Sub cmdAdd_Click()


With mudtStudent
.curFormNo = Val(txtFormNo)
.strName = lstName.List(lstName.ListIndex)
.curPercentage = Val(txtPercentage)
.curMathsMarks = Val(txtMathsMarks)
.strCategory = txtCategory
End With

Put #1, lstName.ListIndex + 1, mudtStudent


End Sub

Private Sub cmdDelete_Click()


Dim i As Integer
i = lstName.ListIndex + 1
Get #1, i, mudtStudent

For i = 1 To 5
If Trim(mudtStudent.strName) = Trim(lstName.List(lstName.ListIndex)) Then
txtFormNo = mudtStudent.curFormNo
txtPercentage = mudtStudent.curPercentage
txtMathsMarks = mudtStudent.curMathsMarks
txtCategory = mudtStudent.strCategory

If (MsgBox("Do you want to DELETE the record?", vbYesNo, "Reply") = vbYes)


Then
With mudtStudent
.curFormNo = 0
.strName = Empty
.curPercentage = 0
.curMathsMarks = 0
.strCategory = Empty
End With

Put #1, lstName.ListIndex + 1, mudtStudent


Else
Exit For
End If
End If
Next i
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdModify_Click()


With mudtStudent
.curFormNo = Val(txtFormNo)
.strName = lstName.List(lstName.ListIndex)
.curPercentage = Val(txtPercentage)
.curMathsMarks = Val(txtMathsMarks)
.strCategory = txtCategory
End With

Put #1, lstName.ListIndex + 1, mudtStudent


End Sub

Private Sub cmdShow_Click()


Dim i As Integer

For i = 1 To 5
Get #1, i, mudtStudent

If Trim(mudtStudent.strName) = Trim(lstName.List(lstName.ListIndex)) Then


txtFormNo = mudtStudent.curFormNo
txtPercentage = mudtStudent.curPercentage
txtMathsMarks = mudtStudent.curMathsMarks
txtCategory = mudtStudent.strCategory
End If
Next i

End Sub

Private Sub Form_Load()


lstName.AddItem ("Raj")
lstName.AddItem ("Vikas")
lstName.AddItem ("Ali")
lstName.AddItem ("Ravi")
lstName.AddItem ("John")

Open "f:\student.txt" For Random As #1 Len = Len(mudtStudent)


End Sub

Private Sub Form_Terminate()


Close #1
End Sub
Screen Shots-
Q.6. Create a project to read 20 numbers and print its median?

Form Design-
Code-

Dim x(20) As Integer, cnt As Integer

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdNext_Click()


x(cnt) = Val(txtNumber)
txtNumber = Empty
txtNumber.SetFocus
cnt = cnt + 1
If cnt = 20 Then
Dim temp, i, j As Integer
For i = 0 To 18
For j = i + 1 To 19
If x(j) < x(i) Then
temp = x(i)
x(i) = x(j)
x(j) = temp
End If
Next j
Next i
MsgBox "Median is - " & (x(9) + x(10)) / 2, vbOKOnly, "Median"
cnt = 0
lblNumber.Caption = "Enter Number " & cnt + 1
Else
lblNumber.Caption = "Enter Number " & cnt + 1
End If
End Sub

Private Sub Form_Load()


lblNumber.Caption = "Enter Number " & cnt + 1
End Sub
Screen Shot-
Q.7. Create a project for KBC game show through database?

Form Design-

Code-

Option Explicit
Dim mintCounter As Integer
Dim mcurAmount As Currency
Dim mcurPrizeAmoutn As Currency
Dim intQuestionNumber As Integer, intRandom As Integer, i As Integer
Dim strName As String, strNum As String
Dim mintarrQuestionNumber(15) As Integer

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmd5050_Click()


If MsgBox("Do you want to use LifeLine 50-50 ?", vbYesNo + vbQuestion, "KBC III") =
vbYes Then
If (lblOptA = lblAnswer) Or (lblOptB = lblAnswer) Then
optC.Visible = False
optD.Visible = False
lblOptC.Visible = False
lblOptD.Visible = False
Else
optA.Visible = False
optB.Visible = False
lblOptA.Visible = False
lblOptB.Visible = False
End If
cmd5050.Visible = False
End If
End Sub

Private Sub cmdEndGame_Click()


End
End Sub

Private Sub cmdJantaKiRai_Click()


'This is for not coded yet.
End Sub

Private Sub cmdLock_Click()


If (MsgBox("Are you sure ?", vbYesNo + vbQuestion, "KBC III") = vbYes) Then
If optA.Value = True Then
AnswerCheck (lblOptA)
ElseIf optB.Value = True Then
AnswerCheck (lblOptB)
ElseIf optC.Value = True Then
AnswerCheck (lblOptC)
ElseIf optD.Value = True Then
AnswerCheck (lblOptD)
Else
MsgBox "Select an option !", vbOKOnly + vbCritical, "Error"
End If

End If
End Sub

Private Sub cmdNext_Click()


With optA
.Value = False
.Visible = True
.Enabled = True
End With

With optB
.Value = False
.Visible = True
.Enabled = True
End With

With optC
.Value = False
.Visible = True
.Enabled = True
End With

With optD
.Value = False
.Visible = True
.Enabled = True
End With

lblOptA.Visible = True
lblOptB.Visible = True
lblOptC.Visible = True
lblOptD.Visible = True

cmdLock.Enabled = True
mintCounter = mintCounter + 1
If mintCounter = 14 Then
MsgBox "Final Question for 1 Crore - Are You Ready !!!", vbOKOnly +
vbInformation, "KBC III"
End If

SetQuestion
SetCurrentQuestionColor
SetPreviousQuestionColor

cmdNext.Enabled = False
End Sub

Public Sub AnswerCheck(SelectedValue As String)


If lblAnswer = SelectedValue Then
MsgBox "Right Answer.", vbOKOnly + vbInformation, "KBC III"
SetPrize
optA.Enabled = False
optB.Enabled = False
optC.Enabled = False
optD.Enabled = False
cmdLock.Enabled = False
cmdNext.Enabled = True
cmdWithDraw.Enabled = True
If mintCounter = 14 Then
MsgBox "You have hit the Jackpot, Congratulations!", vbOKOnly + vbExclamation,
"KBC III"
cmd5050.Enabled = False
cmdJantaKiRai.Enabled = False
cmdLock.Enabled = False
cmdNext.Enabled = False
cmdPhoneAFriend.Enabled = False
cmdWithDraw.Enabled = False
End If
Else
MsgBox "Ah! Wrong Answer.", vbOKOnly + vbCritical, "Wrong Answer !"
MsgBox "Right Answer is - " + lblAnswer
cmdWithDraw_Click
End If
End Sub

Private Sub cmdPhoneAFriend_Click()


If (MsgBox("Do you want to use Life Line Phone a friend?", vbYesNo + vbQuestion,
"KBCIII") = vbYes) Then
strName = InputBox("Enter Your Frieds Name - ", "KBC III", "Sibtain Masih", 500,
300)
strNum = InputBox("Enter " + strName + "'s phone number - ", "KBC III",
"9773516883", 500, 300)
MsgBox "Dialing " + strNum, vbOKOnly, "KBC III"
MsgBox "Your friend " + strName + " has suggested " + lblAnswer + ".", vbOKOnly +
vbExclamation, "KBCIII"
cmdPhoneAFriend.Visible = False
End If
End Sub

Private Sub cmdWithDraw_Click()


MsgBox "You Have Won - " + lblAssuredPrizeMoney
cmdExit_Click
End Sub

Private Sub Form_Load()


mintCounter = 0
MsgBox "Welcome to the KBC Game Show.", vbOKOnly, "KBC III"
SetCurrentQuestionColor
End Sub

Public Sub SetCurrentQuestionColor()


Select Case mintCounter + 1
Case 1
lbl1.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl1 & "
/-"
Case 2
lbl2.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl2 & "
/-"
Case 3
lbl3.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl3 & "
/-"
Case 4
lbl4.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl4 & "
/-"
Case 5
lbl5.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl5 & "
/-"
Case 6
lbl6.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl6 & "
/-"
Case 7
lbl7.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl7 & "
/-"
Case 8
lbl8.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl8 & "
/-"
Case 9
lbl9.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl9 & "
/-"
Case 10
lbl10.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl10 &
" /-"
Case 11
lbl11.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl11 & "
/-"
Case 12
lbl12.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl12 &
" /-"
Case 13
lbl13.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl13 &
" /-"
Case 14
lbl14.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl14 &
" /-"
Case 15
lbl15.ForeColor = vbRed
lblQuestionStatus = "This is Question " & mintCounter + 1 & " for Rs. " & lbl15 &
" /-"
End Select
End Sub

Public Sub SetPrize()


Select Case mintCounter + 1
Case 1
lblTotalPrizeMoney = "Rs. " + lbl1 + " /-"
Case 2
lblTotalPrizeMoney = "Rs. " + lbl2 + " /-"
Case 3
lblTotalPrizeMoney = "Rs. " + lbl3 + " /-"
Case 4
lblTotalPrizeMoney = "Rs. " + lbl4 + " /-"
Case 5
lblTotalPrizeMoney = "Rs. " + lbl5 + " /-"
lblAssuredPrizeMoney = lblTotalPrizeMoney
Case 6
lblTotalPrizeMoney = "Rs. " + lbl6 + " /-"
Case 7
lblTotalPrizeMoney = "Rs. " + lbl7 + " /-"
Case 8
lblTotalPrizeMoney = "Rs. " + lbl8 + " /-"
Case 9
lblTotalPrizeMoney = "Rs. " + lbl9 + " /-"
Case 10
lblTotalPrizeMoney = "Rs. " + lbl10 + " /-"
lblAssuredPrizeMoney = lblTotalPrizeMoney
Case 11
lblTotalPrizeMoney = "Rs. " + lbl11 + " /-"
Case 12
lblTotalPrizeMoney = "Rs. " + lbl12 + " /-"
Case 13
lblTotalPrizeMoney = "Rs. " + lbl13 + " /-"
Case 14
lblTotalPrizeMoney = "Rs. " + lbl14 + " /-"
Case 15
lblTotalPrizeMoney = "Rs. " + lbl15 + " /-"
lblAssuredPrizeMoney = lblTotalPrizeMoney
End Select
End Sub

Public Sub SetPreviousQuestionColor()


Select Case mintCounter
Case 1
lbl1.ForeColor = vbBlack
Case 2
lbl2.ForeColor = vbBlack
Case 3
lbl3.ForeColor = vbBlack
Case 4
lbl4.ForeColor = vbBlack
Case 5
lbl5.ForeColor = vbBlack
Case 6
lbl6.ForeColor = vbBlack
Case 7
lbl7.ForeColor = vbBlack
Case 8
lbl8.ForeColor = vbBlack
Case 9
lbl9.ForeColor = vbBlack
Case 10
lbl10.ForeColor = vbBlack
Case 11
lbl11.ForeColor = vbBlack
Case 12
lbl12.ForeColor = vbBlack
Case 13
lbl13.ForeColor = vbBlack
Case 14
lbl14.ForeColor = vbBlack
End Select
End Sub

Private Sub optA_Click()


cmdWithDraw.Enabled = False
End Sub
Private Sub optB_Click()
cmdWithDraw.Enabled = False
End Sub

Private Sub optC_Click()


cmdWithDraw.Enabled = False
End Sub

Private Sub optD_Click()


cmdWithDraw.Enabled = False
End Sub

Public Sub SetQuestion()


mintarrQuestionNumber(0) = 0

fraQuestion.Caption = "Question " & mintCounter + 1 & " - "

If mintCounter <> 0 Then


intRandom = Rnd * 34
i=0
While i < mintCounter
If (mintarrQuestionNumber(i) = intRandom) Then
intRandom = Rnd * 15
i=0
Else
i=i+1
End If
Wend
mintarrQuestionNumber(mintCounter) = intRandom

While (Val(lblQNo) <> intRandom)


If (Data1.Recordset.EOF = True) Then
Data1.Recordset.MoveFirst
Else
Data1.Recordset.MoveNext
End If
Wend
End If
End Sub
Screen Shot-
Q.8. Create a project to process electricity bill by inputting consumer number, consumer
name, address, current meter reading, previous meter reading, bill number and bill date. It
should calculate total units consumed, gross amount, fuel charges, electricity duty, service
tax and net amount. A deposit of Rs.500/- for commercial and Rs.200 for residential is to
be added to bill amount. The rate applicable is

Units Consumed Residential Commercial


Up to 200 units Rs.0.5/unit Rs.3/unit
Next 300 Rs.2/unit Rs.5/unit
Next 300 Rs.5/unit Rs.10/unit
Excess Rs.10/unit Rs.25/unit

Fuel charge is 10.5% of gross amount, electricity duty is 5% of gross amount plus fuel
charges, service tax is 2.6% of gross amount plus fuel charges plus electricity duty and
net amount is gross amount + fuel charges + electricity duty + service tax.
Display first due date after one month from bill date.
Display second due date after two months from bill date with interest 12% added.
Display third due date after three months from bill date with interest 24%.
Provide facility for bill printing?

Form Design-
Code-

Option Explicit
Dim mCurMeterReading As Integer, mPrevMeterReading As Integer, mUnitsConsumed
As Integer, mDeposit As Integer
Dim mElectricityCharge As Double, mGrossAmt As Double, mFuelCharges As Double,
mElectricityDuty As Double, mServiceCharge As Double, mNetAmt As Double,
mNetAmtAfter1mMonth As Double, mNetAmtAfter2mMonths As Double,
mNetAmtAfter3mMonths As Double
Dim mDate As Date
Private Sub cmdCalculate_Click()
mCurMeterReading = Val(txtCurrentMeterReading)
mPrevMeterReading = Val(txtPreviousMeterReading)
mUnitsConsumed = mCurMeterReading - mPrevMeterReading

If cboConsumerType.ListIndex = 0 Then
mDeposit = 100

If mUnitsConsumed <= 200 Then


mElectricityCharge = mUnitsConsumed * 0.5
ElseIf mUnitsConsumed <= 500 Then
mElectricityCharge = 200 * 0.5 + (mUnitsConsumed - 200) * 2
ElseIf mUnitsConsumed <= 800 Then
mElectricityCharge = 200 * 0.5 + 300 * 2 + (mUnitsConsumed - 500) * 5
Else
mElectricityCharge = 200 * 0.5 + 300 * 2 + 300 * 5 + (mUnitsConsumed - 800) *
10
End If

ElseIf cboConsumerType.ListIndex = 1 Then


mDeposit = 500

If mUnitsConsumed <= 200 Then


mElectricityCharge = mUnitsConsumed * 3
ElseIf mUnitsConsumed <= 500 Then
mElectricityCharge = 200 * 3 + (mUnitsConsumed - 200) * 5
ElseIf mUnitsConsumed <= 800 Then
mElectricityCharge = 200 * 3 + 300 * 5 + (mUnitsConsumed - 500) * 10
Else
mElectricityCharge = 200 * 3 + 300 * 5 + 300 * 10 + (mUnitsConsumed - 800) *
25
End If

End If

mGrossAmt = mDeposit + mElectricityCharge


mFuelCharges = mGrossAmt * 0.105
mElectricityDuty = (mGrossAmt + mFuelCharges) * 0.05
mServiceCharge = (mGrossAmt + mFuelCharges + mElectricityCharge) * 0.026
mNetAmt = mGrossAmt + mFuelCharges + mElectricityDuty + mServiceCharge

mNetAmtAfter1mMonth = mNetAmt
mNetAmtAfter2mMonths = mNetAmt + mNetAmt * 0.12
mNetAmtAfter3mMonths = mNetAmt + mNetAmt * 0.24

mDate = txtDate

lbl1DueDate = DateAdd("m", 1, mDate)


lbl2DueDate = DateAdd("m", 2, mDate)
lbl3DueDate = DateAdd("m", 3, mDate)

txtUnitsConsumed = FormatNumber(mUnitsConsumed, 2)
txtDeposit = FormatNumber(mDeposit, 2)
txtElectricityCharge = FormatNumber(mElectricityCharge, 2)
txtGrossAmt = FormatNumber(mGrossAmt, 2)
txtFuelCharges = FormatNumber(mFuelCharges, 2)
txtElectricityDuty = FormatNumber(mElectricityDuty, 2)
txtServiceCharge = FormatNumber(mServiceCharge, 2)
txtNetAmt = FormatNumber(mNetAmt, 2)
txt1DueDateAmt = FormatNumber(mNetAmtAfter1mMonth, 2)
txt2DueDateAmt = FormatNumber(mNetAmtAfter2mMonths, 2)
txt3DueDateAmt = FormatNumber(mNetAmtAfter3mMonths, 2)
End Sub

Private Sub cmdClear_Click()


txtConsumerNo = Empty
txtBillNumber = Empty
txtDate = Empty
cboConsumerType.ListIndex = -1
txtName = Empty
txtAddress = Empty
txtCurrentMeterReading = Empty
txtPreviousMeterReading = Empty
txtUnitsConsumed = Empty
txtDeposit = Empty
txtElectricityCharge = Empty
txtGrossAmt = Empty
txtFuelCharges = Empty
txtElectricityDuty = Empty
txtServiceCharge = Empty
txtNetAmt = Empty
lbl1DueDate = Empty
lbl2DueDate = Empty
lbl3DueDate = Empty
txt1DueDateAmt = Empty
txt2DueDateAmt = Empty
txt3DueDateAmt = Empty
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Function NumCheck(Asc As Integer) As Integer


If (Asc >= 48 And Asc <= 57) Or Asc = 8 Then
NumCheck = Asc
Else
NumCheck = 0
End If
End Function

Private Function CharCheck(Asc As Integer) As Integer


If (Asc >= 65 And Asc <= 90) Or (Asc >= 97 And Asc <= 122) Or Asc = 8 Or Asc =
32 Then
CharCheck = Asc
Else
CharCheck = 0
End If
End Function

Private Sub cmdPrint_Click()


PrintForm
End Sub

Private Sub txtBillNumber_KeyPress(KeyAscii As Integer)


KeyAscii = NumCheck(KeyAscii)
End Sub

Private Sub txtConsumerNo_KeyPress(KeyAscii As Integer)


KeyAscii = NumCheck(KeyAscii)
End Sub

Private Sub txtCurrentMeterReading_KeyPress(KeyAscii As Integer)


KeyAscii = NumCheck(KeyAscii)
End Sub

Private Sub txtDate_KeyPress(KeyAscii As Integer)


KeyAscii = DateCheck(KeyAscii)
End Sub

Private Sub txtName_KeyPress(KeyAscii As Integer)


KeyAscii = CharCheck(KeyAscii)

End Sub

Private Sub txtPreviousMeterReading_KeyPress(KeyAscii As Integer)


KeyAscii = NumCheck(KeyAscii)
End Sub

Private Function DateCheck(Asc As Integer) As Integer


If (Asc >= 47 And Asc <= 57) Or Asc = 8 Then
DateCheck = Asc
Else
DateCheck = 0
End If
End Function

Screen Shot-
Q.9. Create a project to read matrix A(x,y) and matrix B(p,q). Calculate and print matrix
Result. Display matrix subscript while inputting and printing. Check if matrix
multiplication is possible. If not then display appropriate message?

Form Design-
Code-

Option Explicit
Dim matrixA(10, 10) As Integer, matrixB(10, 10) As Integer, matrixC(10, 10)
Dim intRowA As Integer, intColA As Integer, intRowB As Integer, intColB As Integer, i
As Integer, j As Integer, k As Integer, m As Integer

Private Sub cmdAddition_Click()


If intRowA = intRowB And intColA = intColB Then
For i = 0 To intRowA - 1
For j = 0 To intColA - 1
matrixC(i, j) = (matrixA(i, j) + matrixB(i, j))
Next
Next
Call showmatrixC(i, j)
Else
MsgBox "Addition Can not be performed.", vbOKOnly, "Error"
End If
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdMultiplication_Click()


If intColA = intRowB Then
For i = 0 To intRowA - 1
For j = 0 To intColB - 1
For k = 0 To intColA - 1
matrixC(i, j) = matrixC(i, j) + matrixA(i, k) * matrixB(k, j)
Next
Next
Next
Call showmatrixC(i, j)
Else
MsgBox "Multiplication Can not be performed.", vbOKOnly, "Error"
End If
End Sub

Private Sub cmdOKElementA_Click()


matrixA(i, j) = txtElementA
If i = txtRowA - 1 And j = txtColA - 1 Then
cmdOKElementA.Enabled = False
txtElementA.Enabled = False
lblMatrixA.Caption = "Elements of Matrix A - " + vbNewLine + vbNewLine
For i = 0 To txtRowA - 1
For j = 0 To txtColA - 1
lblMatrixA.Caption = lblMatrixA.Caption & matrixA(i, j) & " "
Next
lblMatrixA.Caption = lblMatrixA.Caption + vbNewLine + vbNewLine
Next
lblMatrixBTitle.Visible = True
lblRowB.Visible = True
lblColB.Visible = True
lblSizeOfB.Visible = True
txtRowB.Visible = True
txtColB.Visible = True
cmdOKSizeB.Visible = True
txtRowB.SetFocus
i=0
j=0
Else
If j = txtColA - 1 Then
i=i+1
j=0
Else
j=j+1
End If
lblElementA.Caption = "Enter element (" & i + 1 & ", " & j + 1 & ")"
txtElementA = Empty
txtElementA.SetFocus
End If
End Sub

Private Sub cmdOKElementB_Click()


matrixB(i, j) = txtElementB
If i = txtRowB - 1 And j = txtColB - 1 Then
cmdOKElementB.Enabled = False
txtElementB.Enabled = False
lblMatrixB.Visible = True
lblMatrixB.Caption = "Elements of Matrix B - " + vbNewLine + vbNewLine
For i = 0 To txtRowB - 1
For j = 0 To txtColB - 1
lblMatrixB.Caption = lblMatrixB.Caption & matrixB(i, j) & " "
Next
lblMatrixB.Caption = lblMatrixB.Caption + vbNewLine + vbNewLine
Next
fraSelectOperation.Visible = True
cmdAddition.Visible = True
cmdSubstraction.Visible = True
cmdMultiplication.Visible = True
i=0
j=0
Else
If j = txtColB - 1 Then
i=i+1
j=0
Else
j=j+1
End If
lblElementB.Caption = "Enter element (" & i + 1 & ", " & j + 1 & ")"
txtElementB = Empty
txtElementB.SetFocus
End If
End Sub

Private Sub cmdOKSizeA_Click()


If (IsNumeric(txtRowA) = True) And (IsNumeric(txtColA) = True) Then
lblElementA.Visible = True
txtElementA.Visible = True
cmdOKElementA.Visible = True
intRowA = Val(txtRowA)
intColA = Val(txtColA)
lblElementA.Caption = "Enter element (" & i + 1 & ", " & j + 1 & ")"
txtRowA.Enabled = False
txtColA.Enabled = False
cmdOKSizeA.Enabled = False
Else
MsgBox "Enter proper values", vbOKOnly, "Error"
txtRowA = Empty
txtColA = Empty
txtRowA.SetFocus
End If
End Sub

Private Sub cmdOKSizeB_Click()


If (IsNumeric(txtRowB) = True) And (IsNumeric(txtColB) = True) Then
lblElementB.Visible = True
txtElementB.Visible = True
cmdOKElementB.Visible = True
intRowB = Val(txtRowB)
intColB = Val(txtColB)
lblElementB.Caption = "Enter element (" & i + 1 & ", " & j + 1 & ")"
txtRowB.Enabled = False
txtColB.Enabled = False
cmdOKSizeB.Enabled = False
txtElementB.SetFocus
Else
MsgBox "Enter proper values", vbOKOnly, "Error"
txtRowB = Empty
txtColB = Empty
txtRowB.SetFocus
End If
End Sub

Private Sub cmdSubstraction_Click()


If intRowA = intRowB And intColA = intColB Then
For i = 0 To intRowA - 1
For j = 0 To intColA - 1
matrixC(i, j) = (matrixA(i, j) - matrixB(i, j))
Next
Next
Call showmatrixC(i, j)
Else
MsgBox "Substraction Can not be performed.", vbOKOnly, "Error"
End If
End Sub

Public Function showmatrixC(row As Integer, col As Integer)


lblResult = Empty
For i = 0 To row - 1
For j = 0 To col - 1

lblResult = lblResult & "(" & i + 1 & "," & j + 1 & ") "
lblResult = lblResult & matrixC(i, j) & " "
Next
lblResult = lblResult + vbNewLine + vbNewLine
Next
For i = 0 To row - 1
For j = 0 To col - 1
matrixC(i, j) = 0
Next
Next
End Function

Private Sub Form_Load()


i=0
j=0
End Sub
Screen Shot-
Q.10. The local library has a summary reading program. The staff keeps a chart of
reading name and bonus points earned.
Create a project using a menu and function procedure that will determine the bonus
points.

&File Menu
-&points
-&sumarry
-&exit

&Edit Menu
-&clear
-&font
-&color

&Help
-&About

Use text boxes to obtain the readers name and number of books read. Use a label to
display the number of bonus points. The point’s menu command should call a function
procedure to calculate the points using this schedule.
The first 3 books are worth 10 points each.
The next 3 books are worth 15 points each.
All books over 6 are worth 20 points each.
The summary menu command displays the average number of books read for all readers
of that session.
The clear menu command clears the name, no. of books read and the bonus points and
then reset the focus.
The color and font menu command change the color and font of information displayed in
the bonus point label.
Use a message box to displayed your name as a programmer for the about option on the
help menu?
Form Design-

Code-

Dim mintBonusPoints As Integer, mintBooks, mintReaders

Private Sub Form_Load()


mintReaders = 0
mintBooks = 0
End Sub

Private Sub mnuAbout_Click()


MsgBox "Designed by Sibtain Masih !", vbOKOnly + vbInformation, "Library"
End Sub

Private Sub mnuClear_Click()


txtName = Empty
txtBooksRead = Empty
lblBonusPoints = Empty
txtName.SetFocus
End Sub

Private Sub mnuColor_Click()


cdbDialog.ShowColor
lblBonusPoints.ForeColor = cdbDialog.Color
End Sub

Private Sub mnuExit_Click()


End
End Sub

Private Sub mnuFont_Click()


cdbDialog.DialogTitle = "Font"
cdbDialog.FontName = "Arial"
cdbDialog.Flags = cdlCFEffects Or cdlCFBoth
cdbDialog.ShowFont

lblBonusPoints.Font.Name = cdbDialog.FontName
lblBonusPoints.Font.Bold = cdbDialog.FontBold
lblBonusPoints.Font.Italic = cdbDialog.FontItalic
lblBonusPoints.Font.Size = cdbDialog.FontSize
lblBonusPoints.Font.Strikethrough = cdbDialog.FontStrikethru
lblBonusPoints.Font.Underline = cdbDialog.FontUnderline
lblBonusPoints.ForeColor = cdbDialog.Color

End Sub

Private Sub mnuPoints_Click()


mintBooks = mintBooks + Val(txtBooksRead)
mintReaders = mintReaders + 1
mintBonusPoints = BonusCalculation(Val(txtBooksRead))
lblBonusPoints = mintBonusPoints
End Sub

Private Sub mnuSummary_Click()


MsgBox "Average Books Read - " & FormatNumber(mintBooks / mintReaders, 2) & "
per user.", vbOKOnly + vbInformation, "Session's Summary"
mintBooks = 0
mintReaders = 0
mnuClear_Click
End Sub

Private Function BonusCalculation(br As Integer) As Integer

If br <= 3 Then
BonusCalculation = 10 * br
ElseIf br <= 6 Then
BonusCalculation = 10 * 3 + 15 * (br - 3)
Else
BonusCalculation = 10 * 3 + 15 * 3 + 20 * (br - 6)
End If
End Function
Screen Shot-

-----------------------------------------*** The End ***-----------------------------------------

Compiled by –
Sibtain Masih
Student of I.T. Department,
AP College,
Grant Road,
Mumbai – 400 008

Vous aimerez peut-être aussi