• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 503
  • Last Modified:

Make a DBCombo do autocomplete?

Can a DBCombo be made to do autocomplete?  I want when you start typing, for the first matching entry to APPEAR in the combo box - like it happens in Excel, Quicken, Money, etc.
1 Solution
STEP 1: Place the following in the declaration section of a module:

' =========BEGIN: MODULE CODE LISTING=============
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const EM_UNDO = &HC7
Public Const CB_FINDSTRING = &H14C
' =========END: MODULE CODE LISTING=============

STEP 2: Place the following in the declaration section of a Class Module (Name the class as 'CAutoFill'):
' =========BEGIN: CLASS MODULE CODE LISTING=============
Public Sub AutoFill(ByRef cmbCombo As Control, ByVal iKey As Integer, Optional ByVal bFlag As Boolean = False, Optional ByVal bShift As Integer = 0)
' Local Variables.

Dim iStart          As Long
Dim sString         As String
Static iLeftOff     As Long
' * * * * * * *

    ' Enable error handling
    On Error GoTo AutoFill_Error1
    ' Are we dealing with letters here?
    If iKey >= 32 And iKey <= 127 Then
        ' Check if the user wants to undo
        If bShift <> 0 Then
            If LCase(Chr$(iKey)) = "z" Then
                Call SendMessage(cmbCombo.hwnd, EM_UNDO, True, 0&)
                Exit Sub
            End If
        End If
        ' Else we go on...
        iStart = 1
        iStart = cmbCombo.SelStart
        If iLeftOff <> 0 Then
          cmbCombo.SelStart = iLeftOff
          iStart = iLeftOff
        End If

        sString = CStr(Left(cmbCombo.Text, iStart))
         cmbCombo.ListIndex = SendMessage(cmbCombo.hwnd, _
                  CB_FINDSTRING, -1&, ByVal CStr(Left( _
                  cmbCombo.Text, iStart)))
        If cmbCombo.ListIndex = -1 And bFlag Then
            cmbCombo.Text = Left$(cmbCombo.Text, Len(cmbCombo.Text) - 1)
            cmbCombo.SelStart = Len(cmbCombo.Text)
        ElseIf cmbCombo.ListIndex = -1 And Not bFlag Then
            iLeftOff = Len(sString)
            cmbCombo.Text = sString
        End If
        cmbCombo.SelStart = iStart
        cmbCombo.SelLength = Len(cmbCombo.Text) - iStart + 1
        iLeftOff = 0
    End If
    On Error GoTo 0
    Exit Sub
    ' Tell the user what is going on...
    'Err.Raise Err.Number, , Err.Description
    On Error GoTo 0
    ' We need nothing else here...
    Exit Sub
End Sub
' =========END: CLASS MODULE CODE LISTING=============

STEP 3: Put the following in the general declaration section of the form containing a DBCombo (DBCombo1)
Make sure that the 'Style' Property for the combo is set to '0'

' =========BEGIN: FORM MODULE CODE LISTING=============
Private AutoFill            As New CAutoFill

Private Sub DBCombo1_KeyUp(KeyCode As Integer, Shift As Integer)

    ' if you want to limit the user to only type items that are available in the list, set the third
    ' parameter to True.  Setting this parameter to False will allow the user to type something
    ' that is not in the list.
    AutoFill.AutoFill Combo1, KeyCode, True, Shift
End Sub
' =========END: FORM MODULE CODE LISTING=============

STEP 4: Now run the application and type something in the combo.  It should fill with one of the items.

This function works for normal combo boxes as well.  To test this code, it might be better to set up a test project with a normal combo.  Remember to change the name of the combobox to 'DBCombo1' in order to use this code, or change the code.

Pre-populate the combobox (by setting the list property in design time, or by adding items in run time) and run the sample.

Any propblems, give me a yell.


Willem Sevenster


Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now