VB6 - Search value from Combobox in Textbox multilines.

Hi

I need help on this search functionality.

In my ComCode1. i can have specific value, ex: B2.
When i would click in the Search button after selecting a value in the combobox, it would bring the cursor directly on the line from the Textbox where :
Mid$(strBuffer, 6, 2) = ComCode1.Text

Open in new window


How can i do that?

Thanks again for your help.
Private Sub cmdSearch1_Click()
    Dim lngSelectedLine As Long
    Dim lngLength       As Long
    Dim lngFirstCharPos As Long
    Dim strBuffer       As String

    ' Determine selected line number (Starts at 0)
    lngSelectedLine = SendMessage(EDI_CODE.hwnd, EM_LINEFROMCHAR, -1, 0)
    lngFirstCharPos = SendMessage(EDI_CODE.hwnd, EM_LINEINDEX, lngSelectedLine, 0&)
    ' length of line
    lngLength = SendMessage(EDI_CODE.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)

    strBuffer = Space(lngLength)
    ' get line text
    Call SendMessage(EDI_CODE.hwnd, EM_GETLINE, lngSelectedLine, ByVal strBuffer)
    
    'Select line if text equals to ComCode1.Text
    If Mid$(strBuffer, 6, 2) = ComCode1.Text Then
        
    End If

End Sub

Open in new window


Search value
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:
What is wrong with the code that you posted?
0
Wilder1626Author Commented:
I need to be able to set focus on the line with a match value from the combobox in below macro

 'Select line if text equals to ComCode1.Text
    If Mid$(strBuffer, 6, 2) = ComCode1.Text Then
        ****  SetFocus on the line****
    End If

Open in new window

0
Martin LissOlder than dirtCommented:
I added lines 19 - 21.
Private Sub cmdSearch1_Click()
    Dim lngSelectedLine As Long
    Dim lngLength       As Long
    Dim lngFirstCharPos As Long
    Dim strBuffer       As String

    ' Determine selected line number (Starts at 0)
    lngSelectedLine = SendMessage(EDI_CODE.hwnd, EM_LINEFROMCHAR, -1, 0)
    lngFirstCharPos = SendMessage(EDI_CODE.hwnd, EM_LINEINDEX, lngSelectedLine, 0&)
    ' length of line
    lngLength = SendMessage(EDI_CODE.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)

    strBuffer = Space(lngLength)
    ' get line text
    Call SendMessage(EDI_CODE.hwnd, EM_GETLINE, lngSelectedLine, ByVal strBuffer)
    
    'Select line if text equals to ComCode1.Text
    If Mid$(strBuffer, 6, 2) = ComCode1.Text Then
        Text1.SetFocus
        Text1.SelStart = lngFirstCharPos
        Text1.SelLength = lngLength        
    End If

End Sub

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Wilder1626Author Commented:
I've just tested like below. I didn't get any error but i didn't set focus also.

    Dim lngSelectedLine As Long
    Dim lngLength       As Long
    Dim lngFirstCharPos As Long
    Dim strBuffer       As String

    ' Determine selected line number (Starts at 0)
    lngSelectedLine = SendMessage(EDI_CODE.hwnd, EM_LINEFROMCHAR, -1, 0)
    lngFirstCharPos = SendMessage(EDI_CODE.hwnd, EM_LINEINDEX, lngSelectedLine, 0&)
    ' length of line
    lngLength = SendMessage(EDI_CODE.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)

    strBuffer = Space(lngLength)
    ' get line text
    Call SendMessage(EDI_CODE.hwnd, EM_GETLINE, lngSelectedLine, ByVal strBuffer)
    
    'Select line if text equals to ComCode1.Text
    If Mid$(strBuffer, 6, 2) = ComCode1.Text Then
        EDI_CODE.SetFocus
        EDI_CODE.SelStart = lngFirstCharPos
        EDI_CODE.SelLength = lngLength
    End If

Open in new window


No result
0
Martin LissOlder than dirtCommented:
Please post your project.
0
Wilder1626Author Commented:
sure.
This is a sample
EDI-app-VB6.zip
0
Martin LissOlder than dirtCommented:
How do I reproduce your problem?


I've got to leave. Will pick this up tomorrow.
0
Wilder1626Author Commented:
In the search field, put S5 and click the command button Search.

In the TextBox on the left, it should get focus where Mid$(strBuffer, 6, 2) = ComCode1.Text (S5)

So after doing step 1 and 2 below, it should set focus at Step 3.

Search
0
Martin LissOlder than dirtCommented:
Where does the data for EDI_CODE textbox come from because when I start the app it doesn't have any data in it.
0
Wilder1626Author Commented:
It comes from an email EDI Tender with many details.

You should see something now.
EDI-app-VB6.zip
0
Martin LissOlder than dirtCommented:
Is EDI_CODE a normal, built-in, VB6 textbox? I ask because when I run any of your projects that contains it I get a "Property Text in EDI_CODE had an invalid file reference" error message.
0
Wilder1626Author Commented:
EDI_CODE is the textbox where i normally paste the EDI tender.
I'm sending you a Text file with EDI info, in case you replace the EDI_CODE textbox four your test.
EDI-CODE.jpg
Tender-edi.txt
0
Martin LissOlder than dirtCommented:
Wait. In our previous question you were asking to be able to determine the row that the user selected and then put part of that row into a grid. This seems to be different and it looks like you want to find a specific value in the textbox and then highlight the row that contains that value. If I'm correct then you can't use the code from our previous question. What you want to do can be done but first tell me if I understand what you want.
0
Wilder1626Author Commented:
You are almost correct.

I want to find a specific value from ComCode1.Text in the textbox, and then highlight the Text in the specific line that contains that value, and also bring the cursor on that specific line.

Those that make sense?
0
Martin LissOlder than dirtCommented:
Try this.
Private Sub cmdSearch1_Click()
    Dim lngSelectedLine As Long
    Dim lngLength       As Long
    Dim lngFirstCharPos As Long
    Dim strBuffer       As String
    Dim lngPos As Long
    
    ' Find the starting charachter of the string in the textbox
    lngPos = InStr(1, EDI_CODE.Text, ComCode1.Text)
    ' Determine the line it's in
    lngSelectedLine = SendMessage(EDI_CODE.hwnd, EM_LINEFROMCHAR, lngPos, 0&)

    lngFirstCharPos = SendMessage(EDI_CODE.hwnd, EM_LINEINDEX, lngSelectedLine, 0&)
    ' length of line
    lngLength = SendMessage(EDI_CODE.hwnd, EM_LINELENGTH, lngFirstCharPos, 0&)

    strBuffer = Space(lngLength)
    ' get line text
    Call SendMessage(EDI_CODE.hwnd, EM_GETLINE, lngSelectedLine, ByVal strBuffer)
    
    EDI_CODE.SetFocus
    EDI_CODE.SelStart = lngFirstCharPos
    EDI_CODE.SelLength = lngLength

End Sub

Open in new window

0
Wilder1626Author Commented:
We are almost there. I forgot 1 small detail from my first post.

Normally, it should only search the value if it shows at position: 6 to 8 of each lines.

Never somewhere else like below picture.
EDIEDI-CODE-no3.jpg
0
Martin LissOlder than dirtCommented:
Change line 9 to the following (untested).

lngPos = InStr(1, Mid$(EDI_CODE.Text, 6,2), ComCode1.Text)
0
Wilder1626Author Commented:
Just tried it and it only select the first line of the textbox.
 
Edit Code
0
Martin LissOlder than dirtCommented:
Try this. If it doesn't work then change line 32 to

lngLineStart = lngLineStart + lngLength

Private Sub cmdSearch1_Click()
    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
    
    ' 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
            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

Open in new window

0
Wilder1626Author Commented:
This is weird. I read the macro, it make sense, but still does not select the line based on:
Mid$(EDI_CODE.Text, lngLineStart + 6, 2) = ComCode1.Text

I tried both way but changing line 32, and it does not do anything.
EDI-app-VB6.zip
0
Martin LissOlder than dirtCommented:
I started your app and copied the contents of your tender-edi.txt file into the textbox. I then selected S5 from the combobox and clicked search. When I did that the 15th line "O204DS5  1 " was selected. Note the space after the 1 in the text of the line. Depending on how the data that's in the textbox is created there may be one of more characters at the end of the line that need to be accounted for and that's what the 2 in the lngLineStart = lngLineStart + lngLength + 2 line is all about. As I implied when I ran the code after pasting the data in and using +2 it worked. When you said you tried it both ways I assume you meant both with and without the +2. If so try + 1 and see if that works. If not then put a breakpoint on the If Mid$(EDI_CODE.Text, lngLineStart + 6, 2) = ComCode1.Text line and the first and second time the code gets there, highlight the Mid$(EDI_CODE.Text, lngLineStart + 6, 2) part and see if you can figure out how to adjust the lngLineStart + lngLength + 2 line so that it works for you. Just in case you aren't familiar with the Mid method, the Mid$(EDI_CODE.Text, lngLineStart + 6, 2) code says "look in EDI_CODE.text, starting at character lngLineStart + 6 (since we are looking for characters starting in the 6th position of any given line) for a length of 2.
0
Wilder1626Author Commented:
I may know why you don't see the problem.

In the first row of the tender, please add:
O204DN4  Brampton                      ONL6Y5S5         CAN        

Then, in the ComCode1, put S5.

It will select the first row even if S5 is at position 46.

I will also test what you said just above.
0
Martin LissOlder than dirtCommented:
I added that text at the top and it still works correctly for me.
Working
0
Wilder1626Author Commented:
did you run the below macro?:
   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
    
    ' 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
            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

Open in new window

0
Martin LissOlder than dirtCommented:
Yes, exactly.
0
Wilder1626Author Commented:
very very strange. Let me see why for my, the above macro don't find it..

Let me try something else.
0
Martin LissOlder than dirtCommented:
Just before line 32, add this and post what shows up in the Immediate Window after you run the app.

        If lngLine < 5 Then
            Debug.Print lngLineStart & ": " & Mid$(EDI_CODE.Text, lngLineStart + 6, 2)
        End If

Open in new window

0
Wilder1626Author Commented:
wow, i don't have any message from the above code.

Ok, i really need to find the problem. :(

Be back soon
0
Martin LissOlder than dirtCommented:
Put a breakpoint on line 10. Assuming that the code gets there, press F8 to step line by line through the code to see what's happening.
0
Wilder1626Author Commented:
If i break at line 10, then use F8, it goes to:For lngLine = 1 To lngLineCount - 1

If i hit again on F8, it goes to End Sub.

end sub
0
Martin LissOlder than dirtCommented:
You somehow changed the the yellow line. It should be For lngLine = 0 To....
0
Wilder1626Author Commented:
sorry, , flip back to For lngLine = 0 To lngLineCount - 1.

But i still have the same result. I'm sure i'm missing something and it will jump at me very soon. :)
0
Martin LissOlder than dirtCommented:
Back aways I asked if EDI_CODE was a normal, built-in, VB6 textbox. Is it?
0
Wilder1626Author Commented:
I may be missing Private Const EM_GETLINECOUNT
0
Wilder1626Author Commented:
yes it is. i took a normal Textbox and named it: EDI_CODE
0
Martin LissOlder than dirtCommented:
What is the value of lngLineCount  after this line is executed?

lngLineCount = SendMessage(ByVal EDI_CODE.hwnd, EM_GETLINECOUNT, 0&, 0&)
0
Martin LissOlder than dirtCommented:
I may be missing Private Const EM_GETLINECOUNT
OMG yes, my fault!!!

Private Const EM_GETLINECOUNT = &HBA
0
Wilder1626Author Commented:
No worry. That made me validate the full code. :)
This is not a bad thing.

Let me do couple of tests. But i think we have it. :)
0
Martin LissOlder than dirtCommented:
If it's possible that there might not be "S5" or other combobox values in the textbox, we probably should add some validation.
0
Wilder1626Author Commented:
That is a good idea.
0
Wilder1626Author Commented:
After this topic, i will open another one with a similar request, i will also look for a NEXT function as i may have multiple rows of S5 for example.

By clicking next, it would jump to the next line with the same value.
0
Martin LissOlder than dirtCommented:
Added lines 9, 23 and 36 to 38.
Private Sub cmdSearch1_Click()
    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
End Sub

Open in new window

0

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
Wilder1626Author Commented:
This is perfect. Thank you so much.
0
Martin LissOlder than dirtCommented:
You're welcome and sorry for my oversight .
0
Wilder1626Author Commented:
No problem. That forced me to try to understand the code. And this is a good thing. ;-)
0
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.