Solved

Extract letters from a string contaning numbers

Posted on 2014-07-25
12
153 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 47

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 47

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 47

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 47

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 47

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 47

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 47

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…

710 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