Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Autofill a ComboBox

Posted on 1998-11-25
6
Medium Priority
?
565 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

916 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