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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA Excel VB Form If Then issue

Status
Not open for further replies.

dprayner

Programmer
Oct 14, 2002
140
US
Hi good people.

I am have an unusual problem. I am trying to compare the values in two textboxes and the less than compare is not working for some strange reason. I created a test application to see if it might be something in my larger application, but it does the same thing when I create a form with two textboxes and a command button. The following is the test code:

Private Sub CommandButton1_Click()
If TextBox1.Value < TextBox2.Value Then
MsgBox "Re-enter TB1"
Exit Sub
End If
MsgBox "Greater than"
End Sub

It's pretty strainght-forward and simple, but it doesn't seem to work.

The test number I used was 1000.21 for textBox1 and 500 for Textbox2. The msgbox Reenter comes up when I enter these values. The Greater Than should be coming up though.

Am I doing something wrong? Any suggestions? DAVE
 
The data in the textboxes are - well - text, NOT numeric.

You need to cast the values in the textboxes to doubles for it to work as you wish.

e.g

If CDbl(TextBox1.Value) < CDbl(TextBox2.Value) Then
MsgBox "Re-enter TB1"
Exit Sub
End If
MsgBox "Greater than"
 
One addition to tauphiro's suggestion: Test for an empty string before converting. Otherwise, a Type Mismatch error will occur.


Regards,
Mike
 
Another way:
If Val(TextBox1.Value) < Val(TextBox2.Value) Then

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi all. Thanks it works well. You guys are the best.DAVE
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top