Weeding out wrong character in string

bjreb

Registered User.
Local time
Today, 21:40
Joined
May 7, 2001
Messages
37
I have a bit of code that I use to weed out letters in a string. The String must be all numbers and spaces but sometimes a letter sneaks in. Right now I just iterate the string character by character using the ascii equivalent. If it comes across a letter it closes and reports an error.
Is there a small elegant way to run this with as little code as possible?

Thanks
 
What is your process now? Perhaps it can be tweaked.
 
Sure, look into the LIKE operator.
 
If it comes across a letter it closes and reports an error.
Is there a small elegant way to run this with as little code as possible?
I'm talking about this part alone, since you already have a function that iterates each character.
 
Here is my code

For iCounter = 1 To iStr
iLen = Asc(Mid(sstring, iCounter, 1))
Select Case iLen
Case Is = 32

Case Is < 48
sLetter = Chr(iLen)
swawa = Replace(swawa, sLetter, "")
iStr = iStr - 1
Case Is > 57
sLetter = Chr(iLen)
swawa = Replace(swawa, sLetter, "")
iStr = iStr - 1

End Select

Next
 
You can also assign a string to an array of bytes, such that each byte is the ascii code for that character. Consider the following code . . .
Code:
Private Sub Test19384710()
    Dim chars() As Byte
    Dim var

    chars = StrConv("123 This is a test 456", vbFromUnicode)
    For Each var In chars
        Debug.Print Chr(var), Asc(var)
    Next
End Sub
 
not sure if this is covered but for identifying the miscreant values
Code:
 SELECT *
 FROM myTable
 WHERE myField Like '*[a-z]*'
which should root out all upper and lowercase letters

alternatively

Code:
 SELECT *
 FROM myTable
 WHERE isnumeric(replace(myField," ",""))=false
will identify all records that have any non numeric character other than a space
 

Users who are viewing this thread

Back
Top Bottom