search as you type combobox in vb6

I wanted to search a text in a combobox with any character in the option and not starting from the first character
Kalyan Kumar YelamanchiliBusiness Application DeveloperAsked:
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.

GrahamSkanRetiredCommented:
It isn't quite clear what you are trying to do. Can you give an example, please?
PatHartmanCommented:
Comboboxes are designed to filter as you type - left to right.  

Are you asking how to make them work some other way?   That isn't going to happen.  There is no user code that controls the process, nor are there any properties.

You might be able to accomplish your goal by adding an additional unbound combo to the form.  You can rearrange the visible column so that it sorts the way you want.  For example,  we have a utility abbreviation that starts with a two character state code.  I could parse that abbreviation so that the state code appears at the end instead.  That way the combo would list the abbreviations in order by utility name rather than by state.
Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
Private Sub cboClients_KeyPress(KeyAscii As Integer)

    Dim cb As Long
    Dim FindString As String
   
    If KeyAscii < 32 Or KeyAscii > 127 Then Exit Sub
   
    If cboClients.SelLength = 0 Then
        FindString = cboClients.Text & Chr$(KeyAscii)
    Else
        'instrTemp = InStr(cboClients.Text, ",")
        'FindString = Left$(cboClients.Text, Len(cboClients.Text) - instrTemp) & Chr$(KeyAscii)
        'MsgBox FindString
        'Split(cboClients.Text, ",", CompareMethod.TextCompare)
        'MsgBox splitTemp
        FindString = Left$(cboClients.Text, cboClients.SelStart) & Chr$(KeyAscii)
    End If
   
    SendMessage cboClients.hwnd, CB_SHOWDROPDOWN, 1, ByVal 0&

    cb = SendMessage(cboClients.hwnd, CB_FINDSTRING, -1, ByVal FindString)
   
    If cb <> CB_ERR Then
        cboClients.ListIndex = cb
        cboClients.SelStart = Len(FindString)
        cboClients.SelLength = Len(cboClients.Text) - cboClients.SelStart
    End If
   
    KeyAscii = 0
   
End Sub


This code actually allows me to enter a character by character starting from left to right, however, I would want to search the dropdown by any character which is present irrespective of the starting character.

For eg: one of the option is 13040001, XYZ Company.

but when i type XYZ it doesnt provide me any record. Instead, if I type 13 it will help me pick up the option. Can you help me solve it?
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!

Robert ShermanOwnerCommented:
Is this a Microsoft Access question or a Visual Basic application question?  

In Access, you would usually have the values 13040001 and 'XYZ Company' in separate fields, and you would use a combobox that is bound to the first column (a key field) but which displays the second column and, as such, is automatically searched on the values in the second column.  So, you would not see the number, and just typing XYZ will bring you to the correct record or close to it if there were more than one value starting with XYZ..

If it's a classic Visual Basic forms application that you are talking about, that might be different as I don't recall whether the combo box in that case is different from the Access/VBA combobox.
Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
Its a Visual Basic application and I need help in that
Robert ShermanOwnerCommented:
Can you separate the number portion from the text and use a multi-column rowsource for the combobox?  

What version of Visual Basic?
Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
I can separate it with the comma but I am not able to start the search from the character after the comma. It's 2010 version. The code I have been using is already posted above if you could help
Robert ShermanOwnerCommented:
Also, note that my advice was based on the fact that you have tagged this question with both "MS Access" and "Visual Basic Classic".   Combo Boxes are quite different between the two.

From the code sample you provided, I would think this was a combobox on an MS Access form and that your code is VBA.   So, you're confirming that this isn't a combobox on an Access form?
Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
Ya this is a vb6 form
Robert ShermanOwnerCommented:
How are you populating the combo box?   Can you provide a code sample?
Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
I wouldn't want to change the order of the data in the combo box as it is one of the requirement. Hence, I was requesting for a search which is effective irrespective of the order
Robert ShermanOwnerCommented:
I'm almost certain that the code sample you provided is code for an MS Access/VBA form combobox.

You have also said that you are using a 2010 version, yet you say that this is vb6.  Visual Basic 6 is a very old product that hasn't been updating in over 10 years, so I ask again, are you sure this is a vb6 application and NOT a Microsoft Access question?

I'm not trying to give you a hard time, I just need to be sure what application/language we are talking about in order to give you a proper answer.
Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
Sorry it's a Microsoft Visual Basic 6.0 (SP6) which is a 2000 edition. And that is written in vb6
Robert ShermanOwnerCommented:
How about using something like this instead of the Windows API call:

    Dim lngIndex As Long
    
    cboClients.ListIndex = -1
    
    For lngIndex = 0 To cboClients.ListCount - 1
        If InStr(1, UCase(cboClients.List(lngIndex)), UCase(FindString)) _
          And cboClients.ItemData(lngIndex) <> 1 Then
            cboClients.ListIndex = lngIndex
            cboClients.ItemData(lngIndex) = 1
            Exit Sub
        End If
    Next

Open in new window

Robert ShermanOwnerCommented:
Here's a more complete example that I retrieved from here.   You'll need to adapt it to match your combo box control name.

Option Explicit

Private blnBackSpace As Boolean

Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
 If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
        If Combo1.Text <> vbNullString Then
          'Let the Change event know that it shouldn't respond to this change.
          blnBackSpace = True
        End If
    End If
End Sub

Private Sub Combo1_Change()
    Dim i As Long
    Dim lngText As Long
    'If firing in response to a backspace or delete, don't run the auto-complete
    'complete code. (Otherwise you wouldn't be able to back up.)
    If blnBackSpace = True Or Combo1.Text = vbNullString Then
        blnBackSpace = False
        Exit Sub
    End If
  
    'Run through the available items and grab the first matching one.
    For i = 0 To Combo1.ListCount - 1
        If InStr(1, Combo1.List(i), Combo1.Text, vbTextCompare) = 1 Then
            'Save the SelStart property.
            lngText = Combo1.SelStart
            Combo1.Text = Combo1.List(i)
            'Set the selection in the combo.
            Combo1.SelStart = lngText
            Combo1.SelLength = Len(Combo1.Text) - lngText
            Exit For
        End If
    Next
End Sub

Open in new window

Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
Doesnt the selstart option start the search with the first character in the list i.e with 130 and so on instead of XYZ which I spoke about an example earlier
Kalyan Kumar YelamanchiliBusiness Application DeveloperAuthor Commented:
it still searches from the starting of the character.
Robert ShermanOwnerCommented:
Give this a shot... I don't have VB6 handy, so you might need to tweak it a bit.  

What I would expect to happen is that as you type, if it finds a matching item it should make that the selected item in the dropdown.  It should also make a text selection of everything from the last character that you typed to the end of the string.   So, if "1234, XYZAB" was an item in the list, and you type "XYZ" at that point this entry should be the selected item AND "ZAB" should be the selected/highlighted portion.  

This approach is using a module-level variable to keep track of the search term as it is typed in.  Let me know what happens.

Option Explicit
Private strSearchFor As String

Private blnBackSpace As Boolean

Private Sub cboClients_KeyDown(KeyCode As Integer, Shift As Integer)
Dim intTermLocation as Integer
 If KeyCode = vbKeyBack Or KeyCode = vbKeyDelete Then
        If cboClients.Text <> vbNullString Then
          'Let the Change event know that it shouldn't respond to this change.
          blnBackSpace = True
          strSearchFor = Left$(strSearchFor, len(strSearchFor) -1)
       Else
          strSearchFor = ""
       End If
  Else
       strSearchFor = strSearchFor & chr$(KeyCode)
    End If
End Sub

Private Sub cboClients_Change()
    Dim i As Long
    Dim lngText As Long
    Dim intWhere As Integer
    'If firing in response to a backspace or delete, don't run the auto-complete
    'complete code. (Otherwise you wouldn't be able to back up.)
    If blnBackSpace = True Or cboClients.Text = vbNullString Then
        blnBackSpace = False
        Exit Sub
    End If

    'Run through the available items and grab the first matching one.
    For i = 0 To cboClients.ListCount - 1
        intWhere = InStr(1, cboClients.List(i), strSearchFor, vbTextCompare)
        if intWhere > 0 Then
            cboClients.Text = cboClients.List(i)
            'Set the selection in the combo.
            cboClients.SelStart = intWhere + (len(strSearchFor)) - 2 ' this might need to be 1
            cboClients.SelLength = Len(cboClients.Text) - (cboClients.SelStart)
            Exit For
        End If
    Next
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
Robert ShermanOwnerCommented:
Hey Kalyan,

Just curious, did that last solution work?   I was unable to test it due to lack of vb6 on my machine.

-Rob
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.