Select Case...End Select - VB6 Sample Program Part 2

Saturday, August 22, 2009 ·

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

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

  • Double click cmdCalBonus button to create sub cmdCalBonus_Click()
  • Write down below program 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 program 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 program 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 program 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



  • 0 comments:

    Post a Comment