Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Microsoft: Access Forms FAQ

Combo Boxes

How Do I Set Up Find As You Type In A Combo Box? by tomh1
Posted: 11 May 06

Firefox and the various Desktop Search applications have introduced users to the concept of "find as you type." Combo Boxes in MS Access offer an automatic AutoComplete property to automatically select the first entry that matches the characters that the user types.

AutoComplete assumes that the user is typing the first character and typing characters that should be successive. For instance, if the user types "g" in a list that contains "Andrew," "Carl," "George" and "Scott," AutoComplete selects "George," but typing "o" would not return any results.

Some programs have extended the find-as-you-type concept to include automatic "abbreviations." Examples include Colibri and AppRocket for Windows and LaunchBar and Butler for Mac OS X. In such cases, find-as-you-type will find any entry with the typed characters. In the example above, typing "o" would return "George" and "Scott," and typing "oe" would return just "George."

Implementing this in Access proves to be fairly easy.
  1. Create a combo box on a form.
  2. Create an event procedure for the combo box's OnChange event.
  3. Copy and paste the code, below, into the OnChange procedure
  4. Replace "Combo0" with the name of your combo box.
  5. Change the strSQL strings to match your table and fields.
  6. You're done.


Private Sub Combo0_Change()
' Function Description:
' Filter a combo box list as the user types, similarly to how application
' launchers like Colibri, AppRocket and LaunchBar opperate.
    ' e.g. if the list contains the names of U.S. Presidents, and
    ' the user types "gw," then the resulting SQL WHERE clause will
    ' look like "Name Like '*g*w*'" and the resulting list
    ' will include George Washington, George H. W. Bush and
    ' George W. Bush, among others.
    ' The order is preserved, so that typing "wg" creates an SQL WHERE
    ' clause like "Name Like '*w*g*'" and the resulting list would
    ' include George Washington but not the Bushes.
' This is accomplished by grabbing the text typed by the user in the
' combo box's edit field, creating an SQL SELECT statement from it,
' and finally applying that SQL statement to the combo box's
' .RowSource property.

' Form design settings:
'  Set AutoExpand to No
'  Column Count 3
'  Keyed on column 1 (record primary key)
'  Showing column 2 (user-readable data) column 2 width > 0
'  First and Second column width=0

Dim strText, strFind

' Get the text that the user has typed into the combo box editable field.
strText = Me.Combo0.Text

' If the user has typed something in, then filter the combobox
' list to limit the visible records to those that contain the
' typed letters.
' Otherwise (if the field is blank), the user has deleted whatever
' text they typed, so show the entire (unfiltered) list
If Len(Trim(strText)) > 0 Then
    ' Show the list with only those items containing the typed
    ' letters.
    ' Create an SQL query string for the WHERE clause of the SQL
    ' SELECT statement.
    strFind = "Name Like '"
    For i = 1 To Len(Trim(strText))
        If (Right(strFind, 1) = "*") Then
            ' When adding another character, remove the
            ' previous "*," otherwise you end up with
            ' "*g**w*" instead of "*g*w*."
            ' This has no apparent impact on the user, but
            ' ensures that the SQL looks as intended.
            strFind = Left(strFind, Len(strFind) - 1)
        End If
        strFind = strFind & "*" & Mid(strText, i, 1) & "*"
    strFind = strFind & "'"
    ' Create the full SQL SELECt string for the combo box's
    ' .RowSource property.
    strSQL = "SELECT tName.nameKey, tName.Name, SortOrder FROM tName Where " & _
    strFind & " ORDER BY SortOrder;"
    '' NOTE: to remove the order requirement, such that typing "wg"
    '' and "gw" return the same results, the SQL WHERE clause needs
    '' to look like "Name Like '*w* AND *g*'."
    '' The code above should be changed as follows:
    ''For i = 1 To Len(Trim(strText))
    ''   strFind = strFind & "Name Like '*" & Mid(strText, i, 1) & "*' And "
    ''strSQL = "SELECT tName.nameKey, tName.Name, SortOrder from tblApps Where " & _
    ''Left(strFind, Len(strFind) - 5) & " Order By SortOrder"
    ' Filter the combo list records using the new SQL statement.
    Me.Combo0.RowSource = strSQL
    ' Show the entire list.
    strSQL = "SELECT tName.nameKey, tName.Name, tName.SortOrder FROM tName ORDER BY tName.SortOrder; "
    Me.Combo0.RowSource = strSQL
End If

' Make sure the combobox is open so the user
' can see the items available on list.

End Sub

One difference between the application launchers mentioned above and this code is that the application launchers remember your last selection, and intelligently reorder the list to bring your most common selections to the top. This code does not do that, though it should be possible through the combo box's OnLostFocus event and (probably) an extra field or two in the corresponding table.

Back to Microsoft: Access Forms FAQ Index
Back to Microsoft: Access Forms Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close