random inaccuraciy in my project

JMaher
JMaher used Ask the Experts™
on
I am working on a program that puts (say) eggs into cartons. Each carton holds 30 eggs. The user can put an egg into any carton and position they want, provided it is not already used. So I have the following code in the Form Load part of the program:
MaxCart = GetSetting("GolfBalls", "Carts", "number", 400)
 MaxCart is also a Public variable.
Now when I try to enter a number in the "Carton" section, I am told that the number is too high! Here is that code:

Private Sub cmdAdd_Click()

If txtCart.Text > MaxCart Then
msgtxt = "There are only " & MaxCart & " cartons!"
msghead = "Number too high"
MsgBox msgtxt, , msghead
txtCart.SetFocus
Exit Sub
End If

There is other stuff as well, but this is the offending bit. If I enter number 4, all is OK. But if I enter 5, I get the message box up. Also other numbers, like 6, 7, 8, 65, 98 etc. But number 399 is OK! So are other random numbers! I have stepped through the program, and this is where I believe the problem can only be. I even set up a text box to display MaxCart and it always says 400. Any ideas?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Edited text of question.
Commented:
You need to test for value, not string...

If Val(txtCart.Text) > MaxCart Then


Cheers!

Author

Commented:
As usual, an easy answer... but could you tell me WHY testing for string the way I have been will allow some numbers but not others?

Commented:
All I can tell you is VB doesn't compare strings and numeric values...

Cheers!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial