# percentages

Percentages

i have an order type system where a user enters in a product number
and it searches the database for that product and displays its
description and unit price, which works ok, but i am trying to
calculate the total as well as working out the discount

there is TWO problems

1. not all customers get discount so i keep getting an error
saying not divisible by zero if i enter "0" into the discount
textbox

2. How can i get the percentage equation working where it automatically
works out the percentage discount taken off the total

Private Sub txtTotal_Change(Index As Integer)
If IsNumeric(txtUnitPrice(Index)) And IsNumeric(txtQuantity(Index)) And Val(txtDiscount(Index)) > 0 Then
calculate = Val(txtUnitPrice(Index)) * Val(txtQuantity(Index)) + Val(txtDiscount(Index) / 100) * Val(txtTotal(Index))
txtTotal(Index).Text = calculate
End If
End Sub

cheers
###### Who is Participating?

Commented:
you would place code like this:

Dim Calcluate as Currency
If IsNumeric(txtUnitPrice(Index)) And IsNumeric(txtQuantity(Index)) Then
calculate = Val(txtUnitPrice(Index)) * Val(txtQuantity(Index)) *( 1 - Val(txtDiscount(Index) / 100)

txtTotal(Index) = Format\$(Calculate, "\$#,##0.00)
Else
txtTotal(Index) = "Invalid Amount"
End If

and I would call this from each of  the Validate Event handler procedures for the txtUnitPrice(index), txtQuantity(index) and txtDiscount(Index) text boxes - so that if ANY of those three values is changed, you recalculate the total for THAT line.

then is, something like this:

Private Function CalcTotal(Index as Integer) as Integer
Dim Calcluate as Currency

If IsNumeric(txtUnitPrice(Index)) And IsNumeric(txtQuantity(Index)) Then
calculate = Val(txtUnitPrice(Index)) * Val(txtQuantity(Index)) *( 1 - Val(txtDiscount(Index) / 100)

txtTotal(Index) = Format\$(Calculate, "\$#,##0.00)
CalcTotal = 0
Else
txtTotal(Index) = "Invalid Amount"
CalcTotal = -1
End If
End Sub

then you would code this sort of like this:

Option Explicit

Private Sub txtUnitPrice_Validate(Index As Integer, Cancel As Boolean)
Cancel = CalcTotal(Index)
End Sub

Private Function CalcTotal(Index As Integer) As Integer
Dim Calculate As Currency

If IsNumeric(txtUnitPrice(Index)) And IsNumeric(txtQuantity(Index)) Then
Calculate = Val(txtUnitPrice(Index)) * Val(txtQuantity(Index)) * (1 - Val(txtDiscount(Index) / 100))

txtTotal(Index) = Format\$(Calculate, "\$#,##0.00")
CalcTotal = 0
Else
txtTotal(Index) = "Invalid Amount"
CalcTotal = -1
End If

End Function

Private Sub txtQuantity_Validate(Index As Integer, Cancel As Boolean)
Cancel = CalcTotal(Index)
End Sub

Private Sub txtDiscount_Validate(Index As Integer, Cancel As Boolean)
Cancel = CalcTotal(Index)
End Sub

make sure that each ot the textboxes is loaded with a DEFAULT Text value of 0 (this is done by making the Text property of each text box, in desing mode, 0 - the number 0)

see if that gets you started.

Arthur Wood
0

Commented:
the formula that you show in the code snippet will ADD the "discount" to the total, not subtract (a DISCOUNTis usually subtracted from the base amount, not added.

Where are you getting the error that you canot divide by 0, as the code you show MULTIPLIES by 0.  Why would you ever be dividing by the DISCOUNT percentage?

And the formula that you show should be correct (except for the SIGN (+/-) issue noted above), even in the case of a Discount percentage of 0, so there is no real reason to only allow txtDiscount >0, as you appear to be doing.

Arthur Wood
0

Commented:
in fact the formula that you show is VERY wrong:

when a Discount is applied to a sale:

Real Amount = Quantity_Sold * (Base_Price * (1 - Discount/100))

so you should be SUBTRACTING the Discount percentage from 1, then MULTIPLY that factor by the Unit Price, and then By the Number of Items Sold (at the Discounted Price)

Arthur Wood
0

Commented:
Why do you use txtTotal_Change event to calculate things?
Where is "calculate" sub?

Val(anything) does *not* return correct value on my regional setting.  Use correct conversion functions, like CDbl, CCur... and function IsNumeric to check if entered value is numeric.
0

Commented:
Sometimes dividing works for discounting . . .

Anyway . . .

I think your question is how to handle those pesky ZEROS

DiscountedAmount = SubTotal / iif(discount = 0, 1, discount)

Any amount divided by 1 is no discount, otherwise  . . .

Well you get the picture.

Good luck -

<`{{{><

Gene
0

Author Commented:
Arthur_Wood

thanks for your comment, could you post a sample showing how i can do this using code, i am a student and havn't got to grips with VB yet!

thanks for all the help!

DD66
0

Commented:
> Calculate = Val(txtUnitPrice(Index)) * Val(txtQuantity(Index)) * (1 - Val(txtDiscount(Index) / 100))

Arthur_Wood, your code is not locale aware.  Do not use Val().

If Discount is 7.5%, and if decimal separator is comma (e.g. in Germany), txtDiscount will hold "7,5"

? Val("7,5")
7      ' not OK

Use CDbl() or CCur() :

Calculate = CDbl(txtUnitPrice(Index).Text) * CDbl(txtQuantity(Index).Text) * _
(1 - CDbl(txtDiscount(Index).Text) / 100)

Val() will work OK only for whole numbers, which do not have thousand separator.

? Val("2000")
2000   ' OK

? Val("2.000")
2      ' not OK
0

Commented:
ameba,
Being in the States, the Locale-awareness is NOT a big issue, so it never occured to me.  Thanks for the note, though it is probably of very little significance to me, it may be to others.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.