Extract letters from a string contaning numbers

i have a regristraction routine that check for valid input
I thought the following would work, but if the user only entered numbers and no letters
it could be fooled, how can i make sure the proper letters are input ?


Private Sub Command5_Click()
    Dim Success As Boolean
    Dim Value As String
    Dim StringEntered As String
    StringEntered = "yjk65kg12dky" 'this will be textbox value entered by user
    Value = GetNumbers(StringEntered)
    
  If Val(Value) >= 5000 And Val(Value) <= 9900 Then
  'now check if the correct letters are typed
    Success = True
  Else
    Success = False
  End If
    MsgBox Success

End Sub
Function GetNumbers(Value As String) As String
'extract and return numbers from a string
    Dim Index As Long
    Dim Digit As String
    Dim Final As String
    Dim Count As Long
    
      Count = 1
      GetNumbers = space(Len(Value))
      For Index = 1 To Len(Value)
        Digit = Mid(Value, Index, 1)
        If Digit Like "[0-9]" Then
          Mid(GetNumbers, Count, 1) = Digit
          Count = Count + 1
        End If
      Next
    
      GetNumbers = Left(GetNumbers, Count - 1)
End Function

Open in new window

isnoend2001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
This will result in just the letters in Final. If you just want numbers then remove the Not.

      For Index = 1 To Len(Value)
       If Not IsNumeric(Mid$(Value, Index, 1)) Then
          Count = Count + 1
          Final = Final & Mid$(Value, Index, 1)
        End If
      Next

Open in new window

0
Martin LissOlder than dirtCommented:
I'm sorry but I see I most likely misunderstood your requirement.

What things would make the input invalid?
0
isnoend2001Author Commented:
Thanks martin liss
If the user only typed numbers and no letters it could be valid or

 StringEntered = "yjk65kg12dky" 'this will be textbox value entered by user
the output would also need the letters "yjkkgdky" and the number value  5000 to 9900
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Martin LissOlder than dirtCommented:
Dim Index As Long

    Dim Digit As String
    Dim strLetters As String
    Dim strNumbers As String
    Dim Count As Long
    
    For Index = 1 To Len(TextBox1.Text)
      If IsNumeric(Mid$(TextBox1.Text, Index, 1)) Then
          'Count = Count + 1 What do you want to count?
          strNumbers = strNumbers & Mid$(TextBox1.Text, Index, 1)
       Else
          strLetters = strLetters & Mid$(TextBox1.Text, Index, 1)
       End If
    Next

    MsgBox strNumbers
    MsgBox strLetters

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Or perhaps something like this.

    Const Valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

    For Index = 1 To Len(TextBox1.Text)
        If InStr(1, Mid$(TextBox1.Text, Index, 1), Valid) = 0 Then
            MsgBox "Invalid character '" & Mid$(TextBox1.Text, Index, 1) & "' found"
            Exit Sub
       End If
    Next

Open in new window

0
isnoend2001Author Commented:
Thanks MartinLiss
I added this Function from your code
Function GetLetters(value As String)
   Dim Index As Long
    Dim Count As Long
    Dim Final As String
      For Index = 1 To Len(value)
       If Not IsNumeric(Mid$(value, Index, 1)) Then
          Count = Count + 1
          Final = Final & Mid$(value, Index, 1)
        End If
      Next
    GetLetters = Final
                                         
End Function
and checked it against "yjkkgdky"
0
Martin LissOlder than dirtCommented:
What about "yjk#k@gdky" ?
0
isnoend2001Author Commented:
No your last post allows any numbers or letters.
I need to restrict the input to values  to be between 5000 to 9900 and must contain
the letters: "yjkkgdky"
0
Martin LissOlder than dirtCommented:
Ok then I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
isnoend2001Author Commented:
What does this mean  "yjk#k@gdky"
Hope you can answer my next question i am going to post. its regarding this same routine
0
Martin LissOlder than dirtCommented:
I assumed that someone was entering a value containing characters like "@", "#", "$", etc in a textbox and if so your code in post ID: 40220525 would treat those as non-numeric.
0
isnoend2001Author Commented:
thats ok if  "@", "#", "$" it would be invalid mean nothing
The input must contain "yjkkgdky" and the value between 5000 and 9900
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.