Solved

Autofill a ComboBox

Posted on 1998-11-25
6
552 Views
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.  
0
Comment
Question by:jjmartin
[X]
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
6 Comments
 
LVL 2

Expert Comment

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

Expert Comment

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

0
 
LVL 1

Expert Comment

by:zivf
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.
0
Independent Software Vendors: 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!

 
LVL 7

Expert Comment

by:Inteqam
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 ?

0
 
LVL 3

Accepted Solution

by:
vbWhiz earned 50 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
'Backspace.
'
'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
' http://www.softcircuits.com
' 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$
    Loop
    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
            Else

                '*** 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]

0
 
LVL 3

Author Comment

by:jjmartin
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.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 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