Results 1 to 4 of 4

Thread: Search multiple letters in any order vb

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Search multiple letters in any order vb

    Hello everyone!!
    I'm trying to make a code textbox to search multiple letters in any order in a second textbox for a dictionary kind of thing. Let's say I'm looking for the following letters "ory" in the the textbox2 from textbox1 and the word is "extraordinary" which has the three letters and is a match because it contains the three letters it doesn't matter in which order or if it repeats as long it has all three match. I would appreciate some simple code to start with, thanks in advance.

  2. #2
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,666

    Re: Search multiple letters in any order vb

    Code:
    Private Sub Text1_Change()
        If TextHasCharacters(Text2.Text, Text1.Text) Then
            Label1.Caption = "Match"
        Else
            Label1.Caption = "No match"
        End If
    End Sub
    
    Private Function TextHasCharacters(nText As String, nCharacters As String) As Boolean
        Dim c As Long
        
        For c = 1 To Len(nCharacters)
            If InStr(nText, Mid$(nCharacters, c, 1)) = 0 Then Exit Function
        Next
        TextHasCharacters = True
    End Function

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2014
    Posts
    58

    Re: Search multiple letters in any order vb

    Hey thanks. This works fine only thing is when I use text1 to search in text2box i only need to search the letter one time shown not multiple repetitions like if i search two times letter "t" text2 must have only two times the letter "t" in the phrase. In other words is whatever many times i input in the search textbox nothing more or less. Thanks in advance.

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,666

    Re: Search multiple letters in any order vb

    Code:
    Private Sub Text1_Change()
        If TextHasCharacters(Text2.Text, Text1.Text) Then
            Label1.Caption = "Match"
        Else
            Label1.Caption = "No match"
        End If
    End Sub
    
    Private Function TextHasCharacters(nText As String, nCharacters As String) As Boolean
        Dim c As Long
        Dim CharUsed() As Boolean
        Dim Pos As Long
        
        ReDim CharUsed(Len(nText))
        For c = 1 To Len(nCharacters)
            Pos = InStr(nText, Mid$(nCharacters, c, 1))
            Do Until (Not CharUsed(Pos)) Or (Pos = 0)
                Pos = InStr(Pos + 1, nText, Mid$(nCharacters, c, 1))
            Loop
            If Pos = 0 Then Exit Function
            If CharUsed(Pos) Then Exit Function
            CharUsed(Pos) = True
        Next
        TextHasCharacters = True
    End Function

Tags for this Thread

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