Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
500MB 27GB Web Hosting - $9.95/Month
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleSee if a string contains only digits or only letters
Keywordsstring, numeric, alpha, digits, letters
CategoriesStrings
 
This example uses two functions, AllDigits and AllLetters, that return True if a string contains only digits/letters. They examine each character to see if they fall within the allowed range.
 
' Return True if the string contains only digits.
Private Function AllDigits(ByVal txt As String) As Boolean
Dim ch As String
Dim i As Integer

    AllDigits = True
    For i = 1 To Len(txt)
        ' See if the next character is a non-digit.
        ch = Mid$(txt, i, 1)
        If ch < "0" Or ch > "9" Then
            ' This is not a digit.
            AllDigits = False
            Exit For
        End If
    Next i
End Function

' Return True if the string contains only letters.
Private Function AllLetters(ByVal txt As String) As Boolean
Dim ch As String
Dim i As Integer

    AllLetters = True
    txt = UCase$(txt)
    For i = 1 To Len(txt)
        ' See if the next character is a non-digit.
        ch = Mid$(txt, i, 1)
        If ch < "A" Or ch > "Z" Then
            ' This is not a letter.
            AllLetters = False
            Exit For
        End If
    Next i
End Function
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2003 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated