Solved

Make a DBCombo do autocomplete?

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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.
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…
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…

760 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

23 Experts available now in Live!

Get 1:1 Help Now