Link to home
Start Free TrialLog in
Avatar of Wilder1626
Wilder1626Flag for Canada

asked on

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
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I'm on it and almost there.
Avatar of Wilder1626

ASKER

Thank you so much for your help. Really appreciate
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow. this is more very very good. Many thanks again for your help.
You're welcome. Let me know if you need any help with other functionality in your app.
I will. thanks
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.
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

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.
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.
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.
That is a good idea. That will force them to use the Paste and Clear command.
Yes, It is better with: If Mid$(EDI_CODE.Text, lngCharCount + 6, Len(ComCode1.Text)) = ComCode1.Text Then
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.
Ok. Let me try this now. I like the idea. I will let you know the result shortly
That is perfect, thanks. I like the update from your post: 40688021
YW. It's a way to make up for my forgetting about the EM_ constant.
lol , what can i say!!!! you're forgetting made me really happy with the final results ;-)