48
Visual Basic 6 Visual Basic 6 Practical Solutions Practical Solutions for for T.Y.B.Sc.(I.T.) 2009 - 2010 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

19014796 TY BscIT 10 VB Practical Solution 20092010

Embed Size (px)

Citation preview

Page 1: 19014796 TY BscIT 10 VB Practical Solution 20092010

Visual Basic 6Visual Basic 6

Practical Solutions Practical Solutions

for for

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

Guided by – Hon. Prof. Mr. Vahid Kapadia

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

Page 2: 19014796 TY BscIT 10 VB Practical Solution 20092010

Q.1. Create a project for Coffee Shop?

Form Design-

Page 3: 19014796 TY BscIT 10 VB Practical Solution 20092010

Code-

Option ExplicitDim mcurSubTotal As CurrencyDim mcurTotal As CurrencyDim mcurGrandTotal As CurrencyDim mintCustomerCount As Integer

Private Sub cmdCalculate_Click()Dim curPrice As CurrencyDim intQuantity As IntegerDim curTax As CurrencyDim curItemAmount As Currency

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

If optCappuccino.Value = True Then curPrice = curCappuccinoPriceElseIf optEspresso.Value = True Then curPrice = curExpressoPriceElseIf optLatte.Value = True Then curPrice = curLattePriceElseIf optIcedCappuccino.Value = True Or optIcedLatte.Value = True Then curPrice = curIcedPriceEnd 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

Page 4: 19014796 TY BscIT 10 VB Practical Solution 20092010

MsgBox "Quantity must be numeric.", vbExclamation, "Numeric Test Failed" txtQuantity = Empty txtQuantity.SetFocusEnd IfEnd 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 IfEnd Sub

Private Sub cmdExit_Click()EndEnd 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

Page 5: 19014796 TY BscIT 10 VB Practical Solution 20092010

'Display appropriate display item and enable check box With chkTax .Enabled = True .Value = vbChecked End WithEnd 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 IfEnd Sub

Page 6: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shot-

Page 7: 19014796 TY BscIT 10 VB Practical Solution 20092010

Q.2. Create a project to read the income and calculate Income Tax, Surcharge and Net income. Where Income Tax isIncome TaxFirst 1,00,000 NilNext 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-

Page 8: 19014796 TY BscIT 10 VB Practical Solution 20092010

Code-

Private Sub cmdCalculate_Click()Dim mIncome As Currency, mTax As Currency, mSurcharge As Currency

mIncome = Val(txtIncome)

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

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

txtTax = mTaxtxtSurcharge = mSurchargetxtNetIncome = mIncome - (mTax + mSurcharge)

End Sub

Private Sub cmdClear_Click()txtIncome = EmptytxtTax = EmptytxtSurcharge = EmptytxtNetIncome = EmptytxtIncome.SetFocusEnd Sub

Private Sub cmdExit_Click()EndEnd Sub

Page 9: 19014796 TY BscIT 10 VB Practical Solution 20092010

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

Page 10: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shot-

Page 11: 19014796 TY BscIT 10 VB Practical Solution 20092010

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-

Page 12: 19014796 TY BscIT 10 VB Practical Solution 20092010

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.SetFocusEnd 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 SubPublic Function checkVal(Ascii As Integer) As Integer If (Ascii >= 48 And Ascii <= 57) Or (Ascii = 8) Then checkVal = Ascii Else checkVal = 0 End IfEnd Function

Page 13: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shots-

Page 14: 19014796 TY BscIT 10 VB Practical Solution 20092010
Page 15: 19014796 TY BscIT 10 VB Practical Solution 20092010

Q.4. Create a project to cover different properties of combo box and list box?

Form Design-

Page 16: 19014796 TY BscIT 10 VB Practical Solution 20092010

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 IfEnd 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 IfEnd Sub

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

Private Sub cmdExit_Click()EndEnd 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

Page 17: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shot-

Page 18: 19014796 TY BscIT 10 VB Practical Solution 20092010

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)

Page 19: 19014796 TY BscIT 10 VB Practical Solution 20092010

.strName = lstName.List(lstName.ListIndex) .curPercentage = Val(txtPercentage) .curMathsMarks = Val(txtMathsMarks) .strCategory = txtCategory End With Put #1, lstName.ListIndex + 1, mudtStudentEnd 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 iEnd Sub

Private Sub cmdExit_Click() EndEnd Sub

Private Sub cmdModify_Click() With mudtStudent .curFormNo = Val(txtFormNo)

Page 20: 19014796 TY BscIT 10 VB Practical Solution 20092010

.strName = lstName.List(lstName.ListIndex) .curPercentage = Val(txtPercentage) .curMathsMarks = Val(txtMathsMarks) .strCategory = txtCategory End With Put #1, lstName.ListIndex + 1, mudtStudentEnd 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 #1End Sub

Page 21: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shots-

Page 22: 19014796 TY BscIT 10 VB Practical Solution 20092010

Q.6. Create a project to read 20 numbers and print its median?

Form Design-

Page 23: 19014796 TY BscIT 10 VB Practical Solution 20092010

Code-

Dim x(20) As Integer, cnt As Integer

Private Sub cmdExit_Click() EndEnd 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 IfEnd Sub

Private Sub Form_Load() lblNumber.Caption = "Enter Number " & cnt + 1End Sub

Page 24: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shot-

Page 25: 19014796 TY BscIT 10 VB Practical Solution 20092010

Q.7. Create a project for KBC game show through database?

Form Design-

Code-

Option ExplicitDim mintCounter As IntegerDim mcurAmount As CurrencyDim mcurPrizeAmoutn As CurrencyDim intQuestionNumber As Integer, intRandom As Integer, i As IntegerDim strName As String, strNum As StringDim mintarrQuestionNumber(15) As Integer

Private Sub cmdExit_Click()EndEnd 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

Page 26: 19014796 TY BscIT 10 VB Practical Solution 20092010

lblOptC.Visible = False lblOptD.Visible = False Else optA.Visible = False optB.Visible = False lblOptA.Visible = False lblOptB.Visible = False End If cmd5050.Visible = FalseEnd IfEnd Sub

Private Sub cmdEndGame_Click()EndEnd 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 IfEnd Sub

Private Sub cmdNext_Click()With optA .Value = False .Visible = True .Enabled = TrueEnd With

With optB .Value = False

Page 27: 19014796 TY BscIT 10 VB Practical Solution 20092010

.Visible = True .Enabled = TrueEnd With

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

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

lblOptA.Visible = TruelblOptB.Visible = TruelblOptC.Visible = TruelblOptD.Visible = True

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

SetQuestionSetCurrentQuestionColorSetPreviousQuestionColor

cmdNext.Enabled = FalseEnd 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

Page 28: 19014796 TY BscIT 10 VB Practical Solution 20092010

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 IfElse MsgBox "Ah! Wrong Answer.", vbOKOnly + vbCritical, "Wrong Answer !" MsgBox "Right Answer is - " + lblAnswer cmdWithDraw_ClickEnd IfEnd 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 = FalseEnd IfEnd Sub

Private Sub cmdWithDraw_Click()MsgBox "You Have Won - " + lblAssuredPrizeMoneycmdExit_ClickEnd Sub

Private Sub Form_Load()mintCounter = 0MsgBox "Welcome to the KBC Game Show.", vbOKOnly, "KBC III"SetCurrentQuestionColorEnd Sub

Public Sub SetCurrentQuestionColor()Select Case mintCounter + 1 Case 1

Page 29: 19014796 TY BscIT 10 VB Practical Solution 20092010

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

Page 30: 19014796 TY BscIT 10 VB Practical Solution 20092010

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 SelectEnd 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

Page 31: 19014796 TY BscIT 10 VB Practical Solution 20092010

lblTotalPrizeMoney = "Rs. " + lbl13 + " /-" Case 14 lblTotalPrizeMoney = "Rs. " + lbl14 + " /-" Case 15 lblTotalPrizeMoney = "Rs. " + lbl15 + " /-" lblAssuredPrizeMoney = lblTotalPrizeMoneyEnd SelectEnd 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 = vbBlackEnd SelectEnd Sub

Private Sub optA_Click()cmdWithDraw.Enabled = FalseEnd Sub

Page 32: 19014796 TY BscIT 10 VB Practical Solution 20092010

Private Sub optB_Click()cmdWithDraw.Enabled = FalseEnd Sub

Private Sub optC_Click()cmdWithDraw.Enabled = FalseEnd Sub

Private Sub optD_Click()cmdWithDraw.Enabled = FalseEnd 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 WendEnd IfEnd Sub

Page 33: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shot-

Page 34: 19014796 TY BscIT 10 VB Practical Solution 20092010

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 CommercialUp to 200 units Rs.0.5/unit Rs.3/unitNext 300 Rs.2/unit Rs.5/unitNext 300 Rs.5/unit Rs.10/unitExcess 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-

Page 35: 19014796 TY BscIT 10 VB Practical Solution 20092010

Code-

Option ExplicitDim mCurMeterReading As Integer, mPrevMeterReading As Integer, mUnitsConsumed As Integer, mDeposit As IntegerDim 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 DoubleDim mDate As DatePrivate 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

Page 36: 19014796 TY BscIT 10 VB Practical Solution 20092010

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

Page 37: 19014796 TY BscIT 10 VB Practical Solution 20092010

lbl2DueDate = Empty lbl3DueDate = Empty txt1DueDateAmt = Empty txt2DueDateAmt = Empty txt3DueDateAmt = EmptyEnd Sub

Private Sub cmdExit_Click() EndEnd 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 IfEnd 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 IfEnd Function

Private Sub cmdPrint_Click()PrintFormEnd 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)

Page 38: 19014796 TY BscIT 10 VB Practical Solution 20092010

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 IfEnd Function

Screen Shot-

Page 39: 19014796 TY BscIT 10 VB Practical Solution 20092010

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-

Page 40: 19014796 TY BscIT 10 VB Practical Solution 20092010

Code-

Option ExplicitDim 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 IfEnd Sub

Private Sub cmdExit_Click()EndEnd 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 IfEnd Sub

Private Sub cmdOKElementA_Click()matrixA(i, j) = txtElementAIf 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

Page 41: 19014796 TY BscIT 10 VB Practical Solution 20092010

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 = 0Else 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.SetFocusEnd IfEnd Sub

Private Sub cmdOKElementB_Click()matrixB(i, j) = txtElementBIf 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

Page 42: 19014796 TY BscIT 10 VB Practical Solution 20092010

j = 0Else 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.SetFocusEnd IfEnd 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 = FalseElse MsgBox "Enter proper values", vbOKOnly, "Error" txtRowA = Empty txtColA = Empty txtRowA.SetFocusEnd IfEnd 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

Page 43: 19014796 TY BscIT 10 VB Practical Solution 20092010

MsgBox "Enter proper values", vbOKOnly, "Error" txtRowB = Empty txtColB = Empty txtRowB.SetFocusEnd IfEnd 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 IfEnd Sub

Public Function showmatrixC(row As Integer, col As Integer)lblResult = EmptyFor 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 + vbNewLineNextFor i = 0 To row - 1 For j = 0 To col - 1 matrixC(i, j) = 0 NextNextEnd Function

Private Sub Form_Load()i = 0j = 0End Sub

Page 44: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shot-

Page 45: 19014796 TY BscIT 10 VB Practical Solution 20092010

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?

Page 46: 19014796 TY BscIT 10 VB Practical Solution 20092010

Form Design-

Code-

Dim mintBonusPoints As Integer, mintBooks, mintReaders

Private Sub Form_Load()mintReaders = 0mintBooks = 0End Sub

Private Sub mnuAbout_Click()MsgBox "Designed by Sibtain Masih !", vbOKOnly + vbInformation, "Library"End Sub

Private Sub mnuClear_Click()txtName = EmptytxtBooksRead = EmptylblBonusPoints = EmptytxtName.SetFocusEnd Sub

Private Sub mnuColor_Click() cdbDialog.ShowColor lblBonusPoints.ForeColor = cdbDialog.ColorEnd Sub

Private Sub mnuExit_Click()EndEnd Sub

Private Sub mnuFont_Click()

Page 47: 19014796 TY BscIT 10 VB Practical Solution 20092010

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 = mintBonusPointsEnd 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_ClickEnd Sub

Private Function BonusCalculation(br As Integer) As Integer

If br <= 3 Then BonusCalculation = 10 * brElseIf br <= 6 Then BonusCalculation = 10 * 3 + 15 * (br - 3)Else BonusCalculation = 10 * 3 + 15 * 3 + 20 * (br - 6)End IfEnd Function

Page 48: 19014796 TY BscIT 10 VB Practical Solution 20092010

Screen Shot-

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

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