-
I'm new to the VB5 world and I trying to get Vb to do the
following math calculations:
I have 4 txtboxes: monthly payment (user input) A
monthly payment / 2 B
Shortage amount (user input) C
commandbox Compute
Payment amount (end of calc)
commandbox Exit
What I want to do is after a user inputs a monthly amount and a Shortage amount I want to compute the following:
monthly /2 = B, then if C > B then (C-B)+5, if C < B then
(C+B)+5
Anyone that can help me..I want to thank you in advance.
Thanks!
-
Code:
Dim A
Dim B
Dim C
Private Sub Command1_Click()
A = Text1.Text
C = Text2.Text
B = A / 2
Select Case C
Case Is > B
Text3.Text = (C - B) + 5
Case Is < B
Text3.Text = (C + B) + 5
Case Else
MsgBox ("they must be equal")
End Select
End Sub
text 1 is A, text 2 is C, and text 3 is the result.
bob
-
The problem is that A and C are strings, but B is a numeric. If you check the values, A = "500", B = 250, C = "100". The program takes the first Case > B and uses that calculation.
change
A = Text1.Text
C = Text2.Text
to A = val(text1.text)
C = val(text2.text)
and give it a try...
Steve
VB6 / sp3
-
shanzek should be right.
That'll teach me to write code without testing it. I'm so ashamed.
wait, no. If you're getting -145, then it would be the same as (C - B) + 5. Did you perhaps put the + and - in the wrong spot? Try switching the C-B and the C+B to get what you need.
bob
-
I hate to disagree with Bob, but here is what is happening.
text1.text = "500" => A ' string
text2.text = "100" => C ' string
B = A / 2 = "500" / 2 = 250 ' numeric
putting a stop in after b=a/2 and stepping through...
C > B because "100" is > 250 (according to VB's logic of handling strings versus numbers?)
the key is to convert all of the variables to numerics prior to the comparisons. using the val(A) and val(b) code does this. I'm sure there are other more exact methods to convert to single, double, integer, but the val(..) function works fine for me...
Hope this helps...
Steve
VB6/sp3
-
he's right. I humbly bow to my superior (on this question anyway :) )
bob