?
Solved

Make a DBCombo do autocomplete?

Posted on 1998-09-15
1
Medium Priority
?
492 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
wj7ster earned 200 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

Independent Software Vendors: 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!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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