VB6 - Find Next function

Hi again.

I'm searching for a find next function in a MultiLine TextBox.

Ex:
If in ComCode1 i have the value S5 and in my EDI_CODE texbox, i have S5 in 4 different lines, the next command button will go over the lines with S5, one by one.

I still need to only search if the  ComCode1 value show in Mid$(EDI_CODE.Text, lngLineStart + 6, 2) .
Mid$(EDI_CODE.Text, lngLineStart + 6, 2) = ComCode1.Text

This code will search for the first instance but i need to be able to go to the next lines with that same value.
 Dim lngSelectedLine As Long
    Dim lngLength       As Long
    Dim lngFirstCharPos As Long
    Dim strBuffer       As String
    Dim lngLineCount As Long
    Dim lngLine As Long
    Dim lngLineStart As Long
    Dim bFound As Boolean
    
    ' Determine the number of lines in the textbox
    lngLineCount = SendMessage(ByVal EDI_CODE.hwnd, EM_GETLINECOUNT, 0&, 0&)
    
    ' Loop through them
    For lngLine = 0 To lngLineCount - 1
        ' Find the starting position of the line
        lngFirstCharPos = SendMessage(EDI_CODE.hwnd, EM_LINEINDEX, lngLine, 0&)

        ' Find length of line
        lngLength = SendMessage(EDI_CODE.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)
        ' see if there's a match in that line
        If Mid$(EDI_CODE.Text, lngLineStart + 6, 2) = ComCode1.Text Then
            bFound = True
            EDI_CODE.SelStart = lngLineStart
            strBuffer = Space(lngLength)
            ' Get line text
            Call SendMessage(EDI_CODE.hwnd, EM_GETLINE, lngSelectedLine, ByVal strBuffer)
            ' Highlight the line
            EDI_CODE.SetFocus
            EDI_CODE.SelStart = lngFirstCharPos
            EDI_CODE.SelLength = lngLength
            Exit For
        End If
        lngLineStart = lngLineStart + lngLength + 2
    Next
    If Not bFound Then
        MsgBox "'" & ComCode1.Text & " 'not found in list"
    End If

Open in new window


Thanks again
LVL 11
Wilder1626Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
I'm on it and almost there.
Wilder1626Author Commented:
Thank you so much for your help. Really appreciate
Martin LissOlder than dirtCommented:
I made a number of changes. I hope I'm listing them all here.

In the Declarations section add line 8.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
                                     ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_LINELENGTH = &HC1
Private Const EM_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private mlngStartLine As Long

Open in new window


Add this sub.
Private Sub Search()
    Dim lngSelectedLine As Long
    Dim lngLength       As Long
    Dim lngFirstCharPos As Long
    Dim lngLine As Long
    Dim lngLineCount As Long
    Dim lngCharCount As Long
    Dim bFound As Boolean
    Const QUOTE = """"
    
    If ComCode1.Text = "" Then
        MsgBox "Please first make a selection in" & QUOTE & "Search field" & QUOTE
        Exit Sub
    End If
    
    ' Determine the number of lines in the textbox
    lngLineCount = SendMessage(ByVal EDI_CODE.hwnd, EM_GETLINECOUNT, 0&, 0&)
    
    ' Accumulate the characters before the current line
    If mlngStartLine > 0 Then
        For lngLine = 0 To mlngStartLine - 1
             ' Find the starting position of the line
            lngFirstCharPos = SendMessage(EDI_CODE.hwnd, EM_LINEINDEX, lngLine, 0&)
            
            ' Find length of line
            lngLength = SendMessage(EDI_CODE.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)
            lngCharCount = lngCharCount + lngLength + 2
        Next
    End If
       
    ' Loop through them
    For lngLine = mlngStartLine To lngLineCount - 1
        ' Find the starting position of the line
        lngFirstCharPos = SendMessage(EDI_CODE.hwnd, EM_LINEINDEX, lngLine, 0&)

        ' Find length of line
        lngLength = SendMessage(EDI_CODE.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)
        ' see if there's a match in that line
        If Mid$(EDI_CODE.Text, lngCharCount + 6, 2) = ComCode1.Text Then
            bFound = True
            mlngStartLine = lngLine + 1
            EDI_CODE.SelStart = lngCharCount
            ' Highlight the line
            EDI_CODE.SetFocus
            EDI_CODE.SelStart = lngFirstCharPos
            EDI_CODE.SelLength = lngLength
            Exit For
        End If
        lngCharCount = lngCharCount + lngLength + 2
    Next
    If Not bFound Then
        If mlngStartLine = 0 Then
            MsgBox "'" & ComCode1.Text & "' not found in list"
        Else
            MsgBox "No more '" & ComCode1.Text & " ' found in list"
        End If
    End If

End Sub

Open in new window


Change these two subs as shown. Note that cmdSearch1 code is completely replaced by the call to 'Search'.
Private Sub cmdSearch1_Click()
    mlngStartLine = 0
    Search
End Sub

Private Sub cmdSearchNext_Click()
    Search
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Wilder1626Author Commented:
Wow. this is more very very good. Many thanks again for your help.
Martin LissOlder than dirtCommented:
You're welcome. Let me know if you need any help with other functionality in your app.
Wilder1626Author Commented:
I will. thanks
Martin LissOlder than dirtCommented:
I just noticed a tiny typo. In line 55 above, change

MsgBox "No more '" & ComCode1.Text & " ' found in list"

to

MsgBox "No more '" & ComCode1.Text & "' found in list"

There was an unwanted space after the last ampersand and double-quote.
Wilder1626Author Commented:
Thanks. I didn't see that one.

I also did a small adjustment so that it start at line 1 if i change the value in Combobox or if i want to start the same search again.

 If Not bFound Then
        If mlngStartLine = 0 Then
            MsgBox "'" & ComCode1.Text & "' not found in list"
        Else
            MsgBox "No more '" & ComCode1.Text & "' found in list"
            mlngStartLine = "0"
        End If
    End If
    

Open in new window

Martin LissOlder than dirtCommented:
Another change you might want to make is to set the Locked property of EDI_CODE to True so that it can't be changed by the user.
Martin LissOlder than dirtCommented:
Oops, there's a problem. The code is set up to only look for two-digit values so values like "AT8" will currently never be found.
Martin LissOlder than dirtCommented:
To correct that problem change

If Mid$(EDI_CODE.Text, lngCharCount + 6, 2) = ComCode1.Text Then

to

If Mid$(EDI_CODE.Text, lngCharCount + 6, Len(ComCode1.Text)) = ComCode1.Text Then

in the Search sub.
Wilder1626Author Commented:
That is a good idea. That will force them to use the Paste and Clear command.
Wilder1626Author Commented:
Yes, It is better with: If Mid$(EDI_CODE.Text, lngCharCount + 6, Len(ComCode1.Text)) = ComCode1.Text Then
Martin LissOlder than dirtCommented:
A couple of suggestions for small improvements:


Change the Enabled properties of cmdSearch1 and cmdSearchNext to False. Then add the following code
Private Sub ComCode1_Change()
If Trim(ComCode1.Text) = "" Then
    cmdSearch1.Enabled = False
    cmdSearchNext.Enabled = False
End If

End Sub

Private Sub ComCode1_Click()
If ComCode1.ListIndex <> -1 Then
    cmdSearch1.Enabled = True
    cmdSearchNext.Enabled = True
End If
End Sub

Open in new window


With those changes the user won't be able to search with a blank value so you can then remove this code from the Seach sub.
    If ComCode1.Text = "" Then
        MsgBox "Please first make a selection in" & QUOTE & "Search field" & QUOTE
        Exit Sub
    End If

Open in new window


Also it would be better to move the ComCode1_DropDown code to Form_Load since I assume it only ever needs to be done once. If the code is in Form_Load you don't need the ComCode1.Clear line.
Wilder1626Author Commented:
Ok. Let me try this now. I like the idea. I will let you know the result shortly
Wilder1626Author Commented:
That is perfect, thanks. I like the update from your post: 40688021
Martin LissOlder than dirtCommented:
YW. It's a way to make up for my forgetting about the EM_ constant.
Wilder1626Author Commented:
lol , what can i say!!!! you're forgetting made me really happy with the final results ;-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.