PDA

View Full Version : Solved: Determine data type, string, numeric, alpha...



shamsam1
10-18-2008, 02:22 AM
vb to find id data is sting or numeric or alpha numeric or if it contaions specail chars...

can any 1 guide me

Tommy
10-18-2008, 05:18 AM
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.

shamsam1
10-18-2008, 05:45 AM
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

Tommy
10-18-2008, 06:51 AM
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.


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

Demosthine
10-18-2008, 03:28 PM
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. : pray2:


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


Enjoy.
Scott

shamsam1
10-19-2008, 09:23 PM
thanks for solving guys