Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math code not working? 4

Status
Not open for further replies.

DK47

Programmer
Jun 3, 2003
118
US
I can't seem to figure out what is wrong with this code.
Text1 receives a number from the IF statement and the txtTollerance receives a number from an ado control. (I also loaded a number in the text box with Form_Load)

The embedded IF statment fires even if the Text1 value is less than the txtTollerance value!

Note: The upper IF statement works well. The correct number is placed in the Text1 control when the program runs.



Private Sub cmdCheckLoss_Click()

If txtValueThisUpdate < txtHiVal Then
txtHold = (txtHiVal - txtTotalWithdrew) - txtValueThisUpdate
Text1 = (txtHold / txtHiVal) * 100
Text1 = FormatNumber(Text1, 0)

If Text1 >= txtTollerance Then
Label7.Visible = True
frmLossProtect.BackColor = vbRed
End If

Else
Label6.Visible = True

End If

End Sub
 
First thing I would do is use a bit more strongly typed evaluations.

Greetings,
Rick
 
Not sure what you're doing, but i see one little detail. You're talking about 'Text1 receives a number', but (if im right) you're making string to string comparison (Text1 seems like a string), and you need a number to number comparison.

Hope this helps,

David.
 
I think VDavid is going along the right lines. Try:
If val(Text1) >= val(txtTollerance) Then

Good Luck!
 
DK47,
Wow 29 questions with lot's of replies and you have found none to be helpful/expert post.

see FAQ222-2244
 
Try this

Private Sub cmdCheckLoss_Click()

convertNonNumericToZero txtValueThisUpdate
convertNonNumericToZero txtHiVal
convertNonNumericToZero txtHold
convertNonNumericToZero txtTotalWithdrew
convertNonNumericToZero Text1
convertNonNumericToZero txtTollerance

If txtValueThisUpdate.Text < txtHiVal.Text Then


txtHold.Text = CStr((CDbl(txtHiVal.Text) - CDbl(txtTotalWithdrew.Text)) - CDbl(txtValueThisUpdate.Text))

If CDbl(txtHiVal.Text) <> 0# Then
Text1.Text = CStr((CDbl(txtHold.Text) / CDbl(txtHiVal.Text)) * 100#)
Else
Text1.Text = vbNullString
End If

Text1.Text = FormatNumber(Text1.Text, 0)

If CDbl(Text1.Text) >= CDbl(txtTollerance.Text) Then
Label7.Visible = True
frmLossProtect.BackColor = vbRed
End If

Else
Label6.Visible = True

End If


End Sub

Private Sub convertNonNumericToZero(ByRef objTextBox As TextBox)

With objTextBox
.Text = IIf(IsNumeric(.Text), .Text, 0#)
End With
End Sub
 
Hey Dwight,

My two cents: Make sure you're comparing and manipulating numbers, not strings.

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
1st To DrJavaJoe,
For your information I have clicked on the &quot;Mark this post as helpful&quot; many, many times. I did so even when the response was only marginally helpful. Also I take the time to thank each and every one who responds to my questions. If you have reviewed my posts you will see that.
Also a review of my past posts will show that several weeks ago I mentioned that I had marked many posts as helpful but my profile did not indicate that I had. There may be something wrong with the link on the page.
All I can say is I am very, very appreciative to all who have taken time to try and help me. That includes your message on this post.


2nd Andy,vladk,vanvb,VDavid, and LazyMe
Thanks all for your input.
Both text boxes have number inputs. One from an ado bound field and the other from the code evaluation I showed.

I will try the val( ) thing first suggested by vanvb, thanks van.
Then I'll relook at how I set up the code.
I have marked all these responses as helpful. Sorry if they don't get through.

All you guys, have a great night and thanks again.
Regards,
Dwight



 
vanvb,
Thanks a bunch. the Val(text1) >= Val(text2) worked.
Seems soooo easy when you know the answer.
Thanks again to all those that helped, andy, vlad, vdavid
 
When you click to mark as helpfull, a box comes up that you have to click on a line to confirm the action. Is that not what happens for you? Are you clicking on the text to confirm?
 
vanvb,
No box apprears when I click on the &quot;Mark this post as a helpful/expert post!&quot;

Thanks for alerting me to this. I'll look into it tomorrow.

Regards,
Dwight
 
Dwight,

Sounds like your browser isn't letting you create pop-up windows. If that's the case you may want to look at thread608-132319.

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
DK47, i have marked AndyWyatt's post as helpful on your behalf [wink]
 
Why thank you gusset, that is very kind.

Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;A computer program does what you tell it to do, not what you want it to do.&quot; -- Greer's Third Law
 
Lazyme,
What do you mean by &quot;Strongly worded evaluations???&quot;
DK
 
Strongly typed evaluations is what I said...

And by that I mean what various other members already noted as well:
You're using very weakly typed evaluations (strings to compare numeric values etc.). Whenever you want to do math calculations, or whatever else evaluations, you should always convert to the correct type. In this case you would want to convert to numbers with CInt(), CLng() (I prefer these above using the Val() function, since the Cxx functions take into account local settings).

Greetings,
Rick
 
Rick,
Thanks for your help.
AS you can see I am new at VB6.
I am self taught and learning more every day.
My newest project is an investment program for my company and the help you gave me is greatly appreciated.
Thanks Again,
Dwight

Here's a start!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top