Math Rules

D

DS

I tried to have this coded properly. It seems to returning all of the
correct answer but one never knows! Could someone please look at my
parentheses to see if they are correct.
Thank you
DS

Select Case Me.TxtCase
Case 1
Me.TxtAnswer = Qty * Price
Case 2
Me.TxtAnswer = (Qty * Price) * Tax
Case 3
Me.TxtAnswer = Qty * Price * (1 + Tax)
Case 4
Me.TxtAnswer = (Qty * Price) * Percent Discount
Case 5
Me.TxtAnswer = (Qty * Price) * (1 - Percent Discount)
Case 6
Me.TxtAnswer = Qty * Dollar Discount
Case 7
Me.TxtAnswer = Qty * (Price - Dollar Discount)
Case 8
Me.TxtAnswer = (Qty * Price) - (Tax * (Qty * Price))
Case 9
Me.TxtAnswer = Tax * (Qty * Price)
Case 10
Me.TxtAnswer = Qty * Price
Case 11
Me.TxtAnswer = (Qty * Price - (Tax * Qty * Price)) * Percent
Discount
Case 12
Me.TxtAnswer = (Qty * Price - (Tax * Qty * Price)) * (1 -
Percent Discount)
Case 13
Me.TxtAnswer = Qty * Dollar Discount
Case 14
Me.TxtAnswer = (Qty * Price - (Tax * Qty * Price)) - Qty *
Dollar Discount
End Select
 
K

Ken Snell \(MVP\)

How is someone expected to answer whether the parentheses are "correct" when
we have no idea what the desired results / logic are? The "correctness" of
the parentheses depends entirely upon what you are wanting the calculations
to do....and we would just be guessing without more information.
 
J

John W. Vinson

I tried to have this coded properly. It seems to returning all of the
correct answer but one never knows! Could someone please look at my
parentheses to see if they are correct.
Thank you
DS

Select Case Me.TxtCase
Case 1
Me.TxtAnswer = Qty * Price
Case 2
Me.TxtAnswer = (Qty * Price) * Tax
Case 3
Me.TxtAnswer = Qty * Price * (1 + Tax)
Case 4
Me.TxtAnswer = (Qty * Price) * Percent Discount
Case 5
Me.TxtAnswer = (Qty * Price) * (1 - Percent Discount)
Case 6
Me.TxtAnswer = Qty * Dollar Discount
Case 7
Me.TxtAnswer = Qty * (Price - Dollar Discount)
Case 8
Me.TxtAnswer = (Qty * Price) - (Tax * (Qty * Price))
Case 9
Me.TxtAnswer = Tax * (Qty * Price)
Case 10
Me.TxtAnswer = Qty * Price
Case 11
Me.TxtAnswer = (Qty * Price - (Tax * Qty * Price)) * Percent
Discount
Case 12
Me.TxtAnswer = (Qty * Price - (Tax * Qty * Price)) * (1 -
Percent Discount)
Case 13
Me.TxtAnswer = Qty * Dollar Discount
Case 14
Me.TxtAnswer = (Qty * Price - (Tax * Qty * Price)) - Qty *
Dollar Discount
End Select

Ken's point is well taken; nobody here could possibly have any idea what the
logical significance of txtCase might be, or whether your expressions in fact
match your business logic. It's supicious that Tax is used in two different
ways (in cases 2 and 3), and that cases 1 and 10 appear to be identical.

One thing that WILL be a problem is that blanks are meaningful. To VBA, Dollar
and Discount are two independent and unrelated variable names, likewise
Percent Discount. The program will look for variables named "Dollar" and
"Percent" and "Discount" and fail. If these are VBA variables, they cannot
contain blanks; try DollarDiscount and PercentDiscount. If they refer to form
controls, use square brackets: [Dollar Discount], or better, rename the
control to eliminate the blank.
 
D

DS

Thanks John, I see the Err of my ways. I appreciate the input about the
blanks. By the way what is the order of operations in math?
Thanks
DS
 
D

DS

Thanks Scuba Diver, I just asked John for the order of operations, I
couldn't remember the term when I needed it! Thank you for suppling it.
DS
 
J

John W. Vinson

Thanks John, I see the Err of my ways. I appreciate the input about the
blanks. By the way what is the order of operations in math?

Multiplication and division first, addition and subtraction afterward. I'd be
inclinde to freely use parentheses to make it unambiguous; I'm sure that

A * B + C

will be equivalent to

(A * B) + C

rather than to

A * (B + C)

but it would be clearer and self-documenting if you use the parenthesized
notation.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 
J

Jan Baird

Jan Baird is out of the country until September 20. Every effort will be
made to respond to messages, but please be patient.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top