Avatar of Emanuel Morris
Emanuel Morris
Flag for United States of America asked on

Visual Basic Classic to check a text box for float

How to check a text box to see if a float has been entered
Visual Basic Classic

Avatar of undefined
Last Comment
Arana (G.P.)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Martin Liss

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Lee W, MVP

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Fabrice Lambert

There are no way to differenciate an integer from a floating point (double) value since an integer can be stored in a double.
And forcing users to input the format you expect is not an option (users should not adapt to you).

At best, you can use the IsNumeric function (beware that it is subject to local settings), or use a regular expression:
^0*[1-9]\d*(?:[.,]\d+[1-9])?$     Check for numeric with a point or a coma as decimal separator
^0*[1-9]\d*(?:\.\d+[1-9])?$       Check for numeric with a point as decimal separator
^0*[1-9]\d*(?:,\d+[1-9])?$        Check for numeric with a coma as decimal separator
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Fabrice Lambert

@Arana:
Still the same issue, not all floating point numbers have a decimal part.
Your functions fail when given a double without decimal part converted into string as parameter:
Dim Data As String
Data = cstr(cdbl(10))    '// double converted to string

debug.print IsFloat(Data)        '// return False

Open in new window


Arana (G.P.)

Well unless Asker confirms otherwise I believe he wants to know if its a fractional number, because if he wants to check for a float then there is no way to verify it against a string made of numbers(its a string anyway), also float doesn't mean DOUBLE, unless you are refering to SQL Float, and there is no float data type in VB6, if you want the equivalent of C# Float then it will be VB SINGLE not DOUBLE.

CNT(10): 10 would fit in an int, why would you convert 10 to double? , and not a float for sure, unless you specifically assign it to a double data type.

if you want to check against a specific data type, and not only see if your value has a decimal portion then you can use
the vartype function you can use it to specifically check for 1 type like in:

Private Function IsSingle(ByVal value As Variant) As Boolean
    IsSingle =VarType(value) = vbSingle)
End Function

By the way, VB6 does not support regexp unless you call VBA library and all those regexp fail if your last digit is a zero. (ie 101.110) , I know 0 to the right shouldn't matter but as you said you cant force user to not send that as a parameter.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck