Solved

Stupid Newbie Question

Posted on 2000-05-03
8
138 Views
Last Modified: 2010-05-02
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!
0
Comment
Question by:cantrell
8 Comments
 
LVL 2

Expert Comment

by:Jagar
ID: 2774586
Do this
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.
0
 

Expert Comment

by:deshmukhn
ID: 2774626
VB is doing string comparison hence string1 is less than string2. Use CLng and then Compare.
0
 
LVL 14

Accepted Solution

by:
wsh2 earned 50 total points
ID: 2774628
String comparisons in VB are from Left to Right.. numeric comparisons the opposite.

The CInt function that Jagr gave you above will convert the string to Integer.. BUT it will also Error Out because your numbers are larger than 32k (the limit for Integers). The better function to use is Val(String) as it is Int, Lng, Dbl, Sng insensitive.. and will automatically type the string for your comparison. In effect, you would then change your IF statement to read:

If Val(String1) < Val(String2) Then ...





0
 
LVL 32

Expert Comment

by:Erick37
ID: 2774633
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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 7

Expert Comment

by:BarryTice
ID: 2775452
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.
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 2775455
Oops. I didn't see deshmukhn's response.
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 2775458
That's what I get for skimming other posts before responding. Sorry guys!
0
 
LVL 2

Author Comment

by:cantrell
ID: 2775767
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.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now