Solved

Make a DBCombo do autocomplete?

Posted on 1998-09-15
1
479 Views
Last Modified: 2013-12-26
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.
0
Comment
Question by:kbakst
1 Comment
 
LVL 2

Accepted Solution

by:
wj7ster earned 50 total points
ID: 1486988
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
            Beep
            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
   
'----------------------------------------------------------------------------------------------------
AutoFill_Error1:
'----------------------------------------------------------------------------------------------------
   
    ' Tell the user what is going on...
    'Err.Raise Err.Number, , Err.Description
   
    On Error GoTo 0
   
    ' We need nothing else here...
    Exit Sub
   
    Resume
   
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.



Regards


Willem Sevenster






0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now