Solved

Extract letters from a string contaning numbers

Posted on 2014-07-25
12
138 Views
Last Modified: 2014-07-25
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

0
Comment
Question by:isnoend2001
  • 7
  • 5
12 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40220437
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40220440
I'm sorry but I see I most likely misunderstood your requirement.

What things would make the input invalid?
0
 

Author Comment

by:isnoend2001
ID: 40220470
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
 
LVL 45

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40220482
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40220513
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
 

Author Closing Comment

by:isnoend2001
ID: 40220525
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 45

Expert Comment

by:Martin Liss
ID: 40220535
What about "yjk#k@gdky" ?
0
 

Author Comment

by:isnoend2001
ID: 40220538
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40220545
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
 

Author Comment

by:isnoend2001
ID: 40220552
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
 
LVL 45

Expert Comment

by:Martin Liss
ID: 40220556
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
 

Author Comment

by:isnoend2001
ID: 40220569
thats ok if  "@", "#", "$" it would be invalid mean nothing
The input must contain "yjkkgdky" and the value between 5000 and 9900
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now