random inaccuraciy in my project

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
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?
Who is Participating?
mcriderConnect With a Mentor Commented:
You need to test for value, not string...

If Val(txtCart.Text) > MaxCart Then

JMaherAuthor Commented:
Edited text of question.
JMaherAuthor 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?
All I can tell you is VB doesn't compare strings and numeric values...

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.

All Courses

From novice to tech pro — start learning today.