How can I determine if a String variable contains a valid string? For numeric variables I can use IsNumeric, is there something similar for text?
Printable View
How can I determine if a String variable contains a valid string? For numeric variables I can use IsNumeric, is there something similar for text?
What do you consider a "valid string"? You could check for characters that you don't want or characters that you must have by using InStr.
I would consider a valid string anything containing ascii codes 32 - 126.
1) How do I traverse through each letter in a string?
2) How do I convert a letter to an ascii code in vb?
My vb is a little rough I'm used to c++.
You could try the following:
HTHCode:dim x as integer
dim strCharacter as string
dim lglValidString as boolean
for x = 1 to len(string)
strCharacter = mid$(string,x,1)
if asc(strCharacter) > 31 or asc(strCharacter) < 127 then
lglValidString = True
Else
lglValidString = False
Exit For
End If
Next
If Not lglValid String Then
......
End If
JP
the following function replaces anything that is not in the range 0 to 9 or 'a' to 'z' with a minus sign:
the opposite of Chr$() is Asc()Code:Private Sub cleanseBackupComment()
backupComment = LCase$(backupComment)
backupComment = Replace$(backupComment, " ", "_")
'clean the comment up.. replace all chars not in 0 - 9 to a - z with a minus
Dim charNum As Integer
'up to 0
For charNum = &H0 To &H2F
backupComment = Replace$(backupComment, Chr$(charNum), "-")
Next charNum
'from 9 to underscore (5F)
For charNum = &H3A To &H5E
backupComment = Replace$(backupComment, Chr$(charNum), "-")
Next charNum
backupComment = Replace$(backupComment, Chr$(&H60), "-")
'from z to 255
For charNum = &H7B To &HFF
backupComment = Replace$(backupComment, Chr$(charNum), "-")
Next charNum
End Sub
supply chr with a number, it gives a character. supply chr$ with a number, it gives a string of that character, supply asc with a one char string and it gives a number of that string/char:
Chr$(32) -> " "
Asc(" ") -> 32
asc only works on the first given character, so Asc(" abc") only gives 32 for the initial space
AscW is the Unicode version
Thanks that's exactly what I was looking for. :thumb: