Results 1 to 10 of 10

Thread: find a number in textbox from vb6

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    46

    find a number in textbox from vb6

    Hi
    I want vb6 code to find the number in the text box and display it in another box with one of the following conditions
    1 . For example, find a four-digit number in the text
    Or
    2 . For example, find a number between 1000 and 2000 in the text box please guide me .
    Thank you
    Sorry for my English type
    Last edited by aghamali; Jul 9th, 2017 at 08:55 AM.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: find a number in textbox from vb6

    Do you know the number or are you looking to find all 4 digit numbers or all numbers between 1000 and 2000. Do the numbers contain thousand separators? More details would be helpful
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    46

    Re: find a number in textbox from vb6

    abc defgh kslkh52sdf khsdfklh1895gskdf jgkjjkg sdj9999gkjg

    For example, look up the text above and if it finds a number between 1000 and 2000, it will show in a text box

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: find a number in textbox from vb6

    In this case, you will need to test every character in the textbox and keep track of whether the character is numeric or not. Once you have 4 consecutive numeric characters, you found what you are looking for. However, I think that logic is too simple, because what if you encounter 5, 6 or more consecutive numeric characters? As you can see, this is not terribly difficult, it will just require that you make some descisions

    Some test code provided. Modify as needed. This is air-code, not tested so apologize if any errors.
    Code:
    Dim i As Long, lCount as Long, lNumber As Long
    For i = 1 to Len(Text1.Text)
        Select Case Asc(Mid$(Text1.Text, i, 1))
        Case 48 to 57  ' numeric characters 0 - 9 inclusively
            If lCount = 3 Then ' already have 3 numbers and this is the 4th
                lNumber = CLng(Mid$(Text1.Text, i - 3, 4))
                ' test the value of lNumber. If it is what you are looking for then exit the loop
                ' else if you want to continue looking starting with the next character, set lCount to 0 
                ' else other decision factors
                MsgBox "4-digit number found: " & lNumber, vbOkOnly, "Test"
                Exit For
            Else
                lCount = lCount + 1
            End If
        Case Else
            lCount = 0
        End Select
    Next
    Last edited by LaVolpe; Jul 9th, 2017 at 10:08 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    46

    Re: find a number in textbox from vb6

    Thank you for the answer

    How should the code be used?
    I am an amateur

  6. #6
    PowerPoster
    Join Date
    Feb 2017
    Posts
    4,987

    Re: find a number in textbox from vb6

    Code:
    Private Sub Command1_Click()
        Text2.Text = ExtractNumbers(Text1.Text)
    End Sub
    
    Private Function ExtractNumbers(nText As String, Optional nLowestValue As Long = 1000, Optional nHighestValue As Long = 2000) As String
        Dim iChr As String
        Dim c As Long
        Dim iNum As String
        Dim iVal As Long
        
        For c = 1 To Len(nText) + 1
            iChr = Mid(nText, c, 1)
            If IsNumeric(iChr) Then
                iNum = iNum & iChr
            ElseIf iNum <> "" Then
                iVal = Val(iNum)
                If (iVal >= nLowestValue) And (iVal <= nHighestValue) Then
                    If ExtractNumbers <> "" Then ExtractNumbers = ExtractNumbers & " "
                    ExtractNumbers = ExtractNumbers & iNum
                End If
                iNum = ""
            End If
        Next c
    End Function

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: find a number in textbox from vb6

    I'm sure others may be willing to write this for you. I've given you some sample logic, but am not willing to write your project for you. Am very happy to help, to offer suggestions, or help identify logic and logic errors. I will offer this:

    Add a command button to your form. In the command button's click event, paste the sample code. Change Text1 in the sample code to your textbox control's name. I tweaked the sample code a bit to display the 1st 4-digit number found. Add sample text to your textbox and click the button when running for the test.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  8. #8

    Thread Starter
    Member
    Join Date
    Jul 2017
    Posts
    46

    Re: find a number in textbox from vb6

    I thank you very much
    I understand you
    You've helped a lot so far

  9. #9
    New Member VineethChandran's Avatar
    Join Date
    Oct 2017
    Location
    Thiruvananthapuram, India
    Posts
    5

    Re: find a number in textbox from vb6

    I'm not an expert in VB. Try this code. this may help you.

    Code:
    Public Sub ExtractNumbersToTextbox(SoureTestbox As TextBox, DestinationTextbox As TextBox, minValue As Integer, maxValue As Integer)
    Dim AllNumbersFromSourceString As String
    Dim SelectedNumbers() As String
    DestinationTextbox.Text = ""
    AllNumbersFromSourceString = ""
    
        For i = 1 To Len(SoureTestbox.Text)
            If IsNumeric(Mid(SoureTestbox.Text, i, 1)) = True Then
            AllNumbersFromSourceString = AllNumbersFromSourceString & Mid(SoureTestbox.Text, i, 1)
            Else
                If Right(AllNumbersFromSourceString, 1) <> "#" Then
                AllNumbersFromSourceString = AllNumbersFromSourceString & "#"
                End If
            End If
        Next i
    
    SelectedNumbers = Split(AllNumbersFromSourceString, "#")
    
    For ai = 0 To UBound(SelectedNumbers)
        If SelectedNumbers(ai) <> "" Then
            If SelectedNumbers(ai) < (maxValue + 1) And SelectedNumbers(ai) > (minValue - 1) Then
            DestinationTextbox.Text = DestinationTextbox.Text & SelectedNumbers(ai) & ", "
            End If
        End If
    Next ai
    
    End Sub
    
    Private Sub Command1_Click()
    ExtractNumbersToTextbox TextBox1, TextBox2, 1000, 2000
    End Sub

  10. #10
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: find a number in textbox from vb6

    a different approach : but be sure to look at the last result
    Code:
    Private Sub Command1_Click()
        Dim i As Integer
        Dim S As String
        Dim J As Integer
        S = "abc1234567890qwerty1129876azerty12314E2"
        Debug.Print S
        For i = 1 To Len(S)
            J = Val(Mid(S, i, 4))
            If J > 999 Then
               If J < 2001 Then
                  Debug.Print J; " ---- between 1000 and 2000 at position " & i
               Else
                  Debug.Print J; " at position " & i
               End If
            End If
        Next
    End Sub
    do not put off till tomorrow what you can put off forever

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width