cantrell
asked on
Stupid Newbie Question
Ok, I feel really dumb about asking this one, but I just can't seem to figure it out, so here it goes.
I have two strings:
STRING1 = 5182000
STRING2 = 532000
then, I compare them like so:
if string1 < string2 then msgbox "do something"
Now, obviously, string1 is not less than string2, yet vb tells me it is.
I realize that I should be comparing integers, however, they are strings that I am getting, and when I try to convert them to integers, I get an "Overflow" error.
I guess, if I could just somehow convert a string to an integer, I think my problems would be solved.... Help!
I have two strings:
STRING1 = 5182000
STRING2 = 532000
then, I compare them like so:
if string1 < string2 then msgbox "do something"
Now, obviously, string1 is not less than string2, yet vb tells me it is.
I realize that I should be comparing integers, however, they are strings that I am getting, and when I try to convert them to integers, I get an "Overflow" error.
I guess, if I could just somehow convert a string to an integer, I think my problems would be solved.... Help!
VB is doing string comparison hence string1 is less than string2. Use CLng and then Compare.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or use CDec to convert to the Decimal data type:
Private Sub Command1_Click()
Dim s1 As String, s2 As String
s1 = "5182000"
s2 = "532000"
If CDec(s1) < CDec(s2) Then
MsgBox s1 & " < " & s2
End If
End Sub
Private Sub Command1_Click()
Dim s1 As String, s2 As String
s1 = "5182000"
s2 = "532000"
If CDec(s1) < CDec(s2) Then
MsgBox s1 & " < " & s2
End If
End Sub
The overflow error is because integers can only handle numbers up to 32767.
If you convert the numbers to longs, rather than integers, things will be hunky dory.
Dim String1 As String
Dim String2 As String
Dim Long1 As Long
Dim Long2 As Long
String1 = "5182000"
String2 = "532000"
Long1 = CLng(String1)
Long2 = CLng(String2)
If Long1 < Long2 Then
' Do somthing
MsgBox True
Else
' Do something else
MsgBox False
End If
Hope this helps -- b.r.t.
If you convert the numbers to longs, rather than integers, things will be hunky dory.
Dim String1 As String
Dim String2 As String
Dim Long1 As Long
Dim Long2 As Long
String1 = "5182000"
String2 = "532000"
Long1 = CLng(String1)
Long2 = CLng(String2)
If Long1 < Long2 Then
' Do somthing
MsgBox True
Else
' Do something else
MsgBox False
End If
Hope this helps -- b.r.t.
Oops. I didn't see deshmukhn's response.
That's what I get for skimming other posts before responding. Sorry guys!
ASKER
everyone seems to have good answers/comments. However, the proposed answer had problems (as stated by 'wsh2') and "wsh2's" comment was the first one in the list that worked for what I needed it to do - otherwise, I would give everyone the points.
Thank you all.
Thank you all.
if CInt(String1) < CInt(String2) then msgbox "Do Something"
CInt is a function that will convert the parameter to an Integer. There is also CLng, CDbl, CSng, etc.