This posting is continuation of previous posting "Select Case...End Select - VB6 Sample Program Part 1". In the previous posting you have designed a program interface like below image, and you have saved it. Select Case Val(txtSalQty.Text) Private Sub cmdCalBonus_Click()
Now, let's writing the program coding for the above program design.
At the end, your program will look like below listing:
Case 1 To 2
txtBonus.Text = "2%"
Case 3, 4
txtBonus.Text = "3%"
Case 5 To 6
txtBonus.Text = "4.5%"
Case 7 To 8
txtBonus.Text = "6.5%"
Case 9
txtBonus.Text = "10%"
Case Else
txtBonus.Text = "15%"
End Select
txtBonusAmt.Text = Val(txtSalQty.Text) * Val(txtPriceUnit.Text) * _
Val(Left(txtBonus.Text, Len(txtBonus.Text) _
- 1)) / 100
Let's test your program to check whether it's correct or not.
Select Case Val(txtSalQty.Text)
Case 1 To 2
txtBonus.Text = "2%"
Case 3, 4
txtBonus.Text = "3%"
Case 5 To 6
txtBonus.Text = "4.5%"
Case 7 To 8
txtBonus.Text = "6.5%"
Case 9
txtBonus.Text = "10%"
Case Else
txtBonus.Text = "15%"
End Select
txtBonusAmt.Text = Val(txtSalQty.Text) * Val(txtPriceUnit.Text) * _
Val(Left(txtBonus.Text, Len(txtBonus.Text) _
- 1)) / 100
End Sub
According to the Bonus Table if Sales Quantity = 5, so the Bonus Percentage should equal to 4.5%. So the Bonus Amount = 5 X 100 X 4.5 / 100. And the result is 22.5. So your program should like below screenshot.
Now I'll explain how the program works. txtBonusAmt.Text = Val(txtSalQty.Text) * Val(txtPriceUnit.Text) * _
Mybe you think that this is a complex statement. But in fact it's just a simple statement.
Val(Left(txtBonus.Text, Len(txtBonus.Text) _
- 1)) / 100
Val(txtSalQty.Text) convert text "5" into number 5
Val(txtPriceUnit.Text) convert text "100" into number 100
Because txtBonus.Text = "4.5%" (this is a text) so:
Len(txtBonus.Text) will return 4, and Len(txtBonus.Text) - 1 will return 3
Left(txtBonus.Text, Len(txtBonus.Text) - 1) equals to Left("4.5%",4-1) will return "4.5"
Then Val(Left(txtBonus.Text, Len(txtBonus.Text) - 1)) equal to Val("4.5") will return number 4.5
So the above formula will equal to:
txtBonusAmt.Text = Val("5")*Val("100")*Val(Left("4.5%",Len("4.5%")-1))/100
<=> txtBonusAmt.Text = 5 * 100 * 4.5 / 100
<=> txtBonusAmt.Text = 22.5
Select Case...End Select - VB6 Sample Program Part 2
Subscribe to:
Post Comments (Atom)
Labels
- Stop Dreaming Start Action (1)
- Table of Contents (1)
- VB6 Programming (36)
- VB6 Sample Program (16)
0 comments:
Post a Comment