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

Find Alpha character in Alphanumeric string 1

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
0
0
US
I pull in a string from a database. The field can be alphanumeric the place I need to write that data is integer only. Is there an easy way to find if a string is numeric or alphanumeric?

-I hate Microsoft!
-Forever and always forward.
 
Writing this question made me think of an answer. "is numeric" made me think I should trying looking that up. I haven't tried it yet, but there is a method Information.IsNumeric that should work.

-I hate Microsoft!
-Forever and always forward.
 
Yes, isnumeric should tell you whether or not the string is numeric or not.

However, does your string contain only A-z,a-z, 0-9? How about spaces? If there are spaces, do you still want the string to be treated as numeric? What about other special cases or characters embeded in the string?
 
IsNumeric will return True if the contents of a variable can be evaluated as a number. i.e. Boolean values, mathematical notation etc. Therefore it's not the most effective way to see if data is only numeric.

Just for completeness look here

For a more complete test how about giving Regular Expressions a go?

Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
There is a quirk about IsNumeric that you should be aware of.

IsNumeric returns true if the value can be converted to ANY numeric data type (not just integer). Unfortunately, this also allows for scientific notation, so all of the following will return true.

21
21.4
21.4e7

If you want to check for integers ONLY. The following function may be useful.

Code:
    Public Function IsInteger(ByVal Data As String) As Boolean
        IsInteger = IsNumeric(Data & ".0e0")
    End Function

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
George - Snap! [wink]

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Harley - Crackle, Pop!

Of course, I noticed a minor flaw with the function I posted. If data is empty or a space, IsInteger returns true. To correct for that...

Code:
    Public Function IsInteger(ByVal Data As String) As Boolean

        If Data.Trim = "" Then
            IsInteger = False
        Else
            IsInteger = Information.IsNumeric(Data & ".0e0")
        End If

    End Function

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
That is all good to know. Thanks you.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top