Solved

Extract letters from a string contaning numbers

Posted on 2014-07-25
12
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
12 Comments
 
LVL 48

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 48

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 48

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 48

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
 
LVL 48

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 48

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 48

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

617 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