Consulting

Results 1 to 6 of 6

Thread: Solved: Determine data type, string, numeric, alpha...

  1. #1
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location

    Solved: Determine data type, string, numeric, alpha...

    vb to find id data is sting or numeric or alpha numeric or if it contaions specail chars...

    can any 1 guide me

  2. #2
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    Can you post a sample?
    If the string is numeric you can use IsNumeric function. Otherwise you will either have to use regexpressions or search the string one character at a time. But with a couple of samples we could figure out the best approach.

  3. #3
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    sample "1t001252-11" else "12345" else " asasd" else "
    "asd123"
    i want to fine if given string is numeric else AlphaBetical else alphanumeric else if it contains specail chars

    i am using this code to find if string is numeric or alphanumeric but for special chars i need code special chars should incude "-""_" ex sample "1t_001252-11" i want code to verify if string contains specail chars

    Public Function IsAlphaBetical(TestString As String) As Boolean
        
        Dim sTemp As String
        Dim iLen As Integer
        Dim iCtr As Integer
        Dim sChar As String
        
        'returns true if all characters in a string are alphabetical
        'returns false otherwise or for empty string
        
        sTemp = TestString
        iLen = Len(sTemp)
        If iLen > 0 Then
            For iCtr = 1 To iLen
                sChar = Mid(sTemp, iCtr, 1)
                If Not sChar Like "[A-Za-z]" Then Exit Function
            Next
        
        IsAlphaBetical = True
        End If
        
    End Function
    
    Public Function IsAlphaNumeric(TestString As String) As Boolean
    
        Dim sTemp As String
        Dim iLen As Integer
        Dim iCtr As Integer
        Dim sChar As String
        
        'returns true if all characters in a string are alphabetical
        '   or numeric
        'returns false otherwise or for empty string
        
        sTemp = TestString
        iLen = Len(sTemp)
        If iLen > 0 Then
            For iCtr = 1 To iLen
                sChar = Mid(sTemp, iCtr, 1)
                If Not sChar Like "[0-9A-Za-z]" Then Exit Function
            Next
        
        IsAlphaNumeric = True
        End If
        
    End Function
    Public Function IsNumericOnly(TestString As String) As Boolean
        
        Dim sTemp As String
        Dim iLen As Integer
        Dim iCtr As Integer
        Dim sChar As String
        
        'returns true if all characters in string are numeric
        'returns false otherwise or for empty string
        
        'this is different than VB's isNumeric
        'isNumeric returns true for something like 90.09
        'This function will return false
        
        sTemp = TestString
        iLen = Len(sTemp)
        If iLen > 0 Then
            For iCtr = 1 To iLen
                sChar = Mid(sTemp, iCtr, 1)
                If Not sChar Like "[0-9]" Then Exit Function
            Next
        
        IsNumericOnly = True
        End If
        
    End Function

  4. #4
    Moderator VBAX Master Tommy's Avatar
    Joined
    May 2004
    Location
    Houston, TX
    Posts
    1,184
    Location
    The below sub wiil determine if the string has special characters, numbers, and alpha characters.

    NOTE: the space character is returned as a special character.

    [VBA]
    Sub WhatsItGot(AStr As String, Alph As Boolean, Numeric As Boolean, SpecChar As Boolean)
    Dim mI As Long, mTemp As String
    For mI = 1 To Len(AStr)
    mTemp = Mid(AStr, mI, 1)
    If Asc(mTemp) < 58 And Asc(mTemp) > 47 Then
    Numeric = True
    ElseIf (Asc(mTemp) > 64 And Asc(mTemp) < 91) Or (Asc(mTemp) > 96 And Asc(mTemp) < 123) Then
    Alph = True
    ElseIf Asc(mTemp) < 65 Or Asc(mTemp) > 122 Or (Asc(mTemp) > 90 And Asc(mTemp) < 97) Then
    SpecChar = True
    End If
    Next
    End Sub

    'My test sub
    Sub testit()
    Dim a As String, Alph As Boolean, Numeric As Boolean, SpecChar As Boolean
    a = "1t001252-11"
    WhatsItGot a, Alph, Numeric, SpecChar
    Alph = False
    Numeric = False
    SpecChar = False
    a = "12345"
    WhatsItGot a, Alph, Numeric, SpecChar
    Alph = False
    Numeric = False
    SpecChar = False
    a = " asasd"
    WhatsItGot a, Alph, Numeric, SpecChar
    Alph = False
    Numeric = False
    SpecChar = False
    a = "asd123"
    WhatsItGot a, Alph, Numeric, SpecChar
    Alph = False
    Numeric = False
    SpecChar = False
    a = "1t_001252-11"
    WhatsItGot a, Alph, Numeric, SpecChar
    Alph = False
    Numeric = False
    SpecChar = False
    End Sub

    [/VBA]

  5. #5

    Cool Like Statement

    Good Afternoon Everyone.

    I find that an excellent way to determine this is using the Like operator. As far as I can tell, this doesn't give any wrong results.

    [vba]
    Public Enum StringTypes
    Alphabetical = 1
    AlphaNumeric = 2
    Numerical = 3
    Special = 4
    End Enum

    Public Sub Test()
    GetType "Johnson"
    GetType "F. D. Roosevelt"
    GetType "Johnny357"
    GetType "123456"
    End Sub

    Public Function GetType(aString As String) As StringTypes
    If UCase(aString) Like "*[A-Z]*" And _
    Not UCase(aString) Like "*[!A-Z]*" Then
    GetType = Alphabetical

    ElseIf aString Like "*[0-9]*" And _
    UCase(aString) Like "*[A-Z]*" Then
    GetType = AlphaNumeric

    ElseIf aString Like "*[0-9]*" And _
    Not UCase(aString) Like "*[!0-9]*" Then
    GetType = Numerical
    Else
    GetType = Special

    End If

    Debug.Print aString & " = " & GetType
    End Function
    [/vba]

    Enjoy.
    Scott

  6. #6
    VBAX Contributor
    Joined
    May 2008
    Location
    bangalore
    Posts
    199
    Location
    thanks for solving guys

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •