Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Autofill a ComboBox

Posted on 1998-11-25
Medium Priority
Last Modified: 2013-12-26
I have browsed about 2000 questions here and finally got tired.  I need to autofill a combobox as the user types, with data from the list that matches what the user is typing.  
Question by:jjmartin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 1488507
Something like the Windows help system (when you type a letter, the first items beggining with that letter are displayed) ??

Expert Comment

ID: 1488508
ok ,
Where do you type , in a text box or something


Expert Comment

ID: 1488509
there is no 'auto way' to do this.
1. empty the combo/list box (clear method)
2. fill it with the data that matches what you typed

you can use a collection of the objects that are the whole and insert (as you go through the collection with For Each) only the ones that match the current criteria (item Like txt & "*"). trying to do this with data from a database will produce poor results.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1488510
any way , i think the whole needed thing is bad , and without any good results ,

Why do you want to display just those who match , why don't you stop at the first match ?


Accepted Solution

vbWhiz earned 200 total points
ID: 1488511
I also was once in search of the perfect VB combobox, one that would act like the combobox built into access. I stumbled across the source code for this one - so I can't take credit for the code, but it works great!

Sample Code:

'AutoFill - Automatic string fill demo
'Copyright (c) 1997 SoftCircuits Programming (R)
'Redistributed by Permission.
'This Visual Basic 5 example demonstrates how you can use a combo box
'to implement an "autofill" feature. Autofill attempts to enter the
'remainder of the string the user is typing. A combo box is used
'because they implement both a text box and a list. The code finds the
'first item in the list that begins with the text entered so far. It
'then appends the remainder of the matching list item to the text in
'the text box.
'The appended characters not typed by the user are highlighted. This
'way, any new keystrokes typed by the user automatically replace them,
'thereby not getting in the way of normal typing. Code is also added
'to make exceptions to certain keystrokes such as Delete and
'This program may be distributed on the condition that it is
'distributed in full and unchanged, and that no fee is charged for
'such distribution with the exception of reasonable shipping and media
'charged. In addition, the code in this program may be incorporated
'into your own programs and the resulting programs may be distributed
'without payment of royalties.
'This example program was provided by:
' SoftCircuits Programming
' P.O. Box 16262
' Irvine, CA 92623
Option Explicit

'Windows declarations
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
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 CB_FINDSTRING = &H14C
Private Const CB_ERR = (-1)

'Declarations for alternate code (see comments below)
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const CB_SETCURSEL = &H14E

'Private flag
Private m_bEditFromCode As Boolean

Private Sub Form_Load()
    Dim sSysDir As String, sFile As String

    'Get files from system directory for test list
    Screen.MousePointer = vbHourglass
    sSysDir = Space$(256)
    GetSystemDirectory sSysDir, Len(sSysDir)
    sSysDir = Left$(sSysDir, InStr(sSysDir, Chr$(0)) - 1)
    If Right$(sSysDir, 1) <> "\" Then
        sSysDir = sSysDir & "\"
    End If
    sFile = Dir$(sSysDir & "*.*")
    Do While Len(sFile)
        Combo1.AddItem sFile
        sFile = Dir$
    Screen.MousePointer = vbDefault
End Sub

'Certain keystrokes must be handled differently by the Change
'event, so set m_bEditFromCode flag if such a key is detected
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case KeyCode
        Case vbKeyDelete
            m_bEditFromCode = True
        Case vbKeyBack
            m_bEditFromCode = True
    End Select
End Sub

Private Sub Combo1_Change()
    Dim i As Long, j As Long
    Dim strPartial As String, strTotal As String

    'Prevent processing as a result of changes from code
    If m_bEditFromCode Then
        m_bEditFromCode = False
        Exit Sub
    End If
    With Combo1
        'Lookup list item matching text so far
        strPartial = .Text
        i = SendMessage(.hwnd, CB_FINDSTRING, -1, ByVal strPartial)
        'If match found, append unmatched characters
        If i <> CB_ERR Then
            'Get full text of matching list item
            strTotal = .List(i)
            'Compute number of unmatched characters
            j = Len(strTotal) - Len(strPartial)
            If j <> 0 Then
                'Append unmatched characters to string
                m_bEditFromCode = True
                .SelText = Right$(strTotal, j)
                'Select unmatched characters
                .SelStart = Len(strPartial)
                .SelLength = j

                '*** Text box string exactly matches list item ***

                'Note: The ListIndex is still -1. If you want to
                'force the ListIndex to the matching item in the
                'list, uncomment the following line. Note that
                'PostMessage is required because Windows sets the
                'ListIndex back to -1 once the Change event returns.
                'Also note that the following line causes Windows to
                'select the entire text, which interferes if the
                'user wants to type additional characters.
'                PostMessage Combo1.hwnd, CB_SETCURSEL, i, 0
            End If
        End If
    End With
End Sub

Private Sub cmdClose_Click()
    Unload Me
End Sub

[-Note- Simply place a combobox on a form and copy this code onto the form. You can package all this into your own control if you don't like the idea of pasting this code everywhere you have a combobox]


Author Comment

ID: 1488512
Thanks for the help, this worked perfectly.  Sorry to those that posted comments.  I didn't mean to be unclear, I was just in a hurry.

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question