Tuesday, August 18, 2009

Select Case End Select - VB6 Sample Programme Part 2

This posting is continuation of previous posting "Select Case End Select - VB6 Sample Programme Part 1". In the previous posting you have designed a programme interface like below image, and you have saved it.
Select Case End Select VB6 Sample Program

Now, let's writing the programme coding for the above programme design.

  • Double click cmdCalBonus button to create sub cmdCalBonus_Click()
  • Write down below programme coding in the cmdCalBonus_Click() area

    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

    At the end, your programme will look like below listing:

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

    Let's test your programme to check whether it's correct or not.
  • Click Start button on your VB toolbar
  • Type a number (for example 5) on your textbox with label Your Sales Quantity
  • Type a number (for example 100) on your textbox with label Price Unit (USD)
  • And finally click Calculate Bonus button
  • Notice what happen on your program, what's the result?

    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.
    Select Case End Select VB6 Sample Program

    Now I'll explain how the programme works.
  • When you type 5 in the Your Purchase Quantity, then type 100 in the Price Unit (USD), and click Calculate Bonus button, so the cmdCalBonus_Click() executed.
  • Then this statement "Val(txtSalQty.Text)" will convert text "5" into number 5
  • Then in Select Case.. defined that txtBonus.Text is "4.5%" according to the Sales Quantity => 5
  • Then program executes below statement:
    txtBonusAmt.Text = Val(txtSalQty.Text) * Val(txtPriceUnit.Text) * _
    Val(Left(txtBonus.Text, Len(txtBonus.Text) _
    - 1)) / 100
    Mybe you think that this is a complex statement. But in fact it's just a simple statement.
    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






  • 1 comments:

    Unknown said...

    Designing of form is convinent in saple programs....

    cathrin disusa
    Cash Online Get Easy cash at your door step

    Post a Comment