Solved

Make a DBCombo do autocomplete?

Posted on 1998-09-15
1
483 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

772 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