Professional Documents
Culture Documents
Search (While You Type) in A ComboBox
Search (While You Type) in A ComboBox
experts-exchange.com
31-40 minutes
The specific “while you type” aspect might not be suitable for
every application. However, if that option is turned off, what
remains is a more classical search box, but using the combo's
1 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
2 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
3 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
The first attempt, and I'm sure other developers have started
with this idea, is to trap the `change´ event of the combo box, in
order to perform a new search with each keystroke. This would
be a good idea if there was a way to interrupt a search when a
new keystroke occurs. As it stands, this is not really possible in
Access. The search runs in the same process as the interface,
and a fast typist will soon become very angry at the constant
pointless interruptions.
The search needs to occur only once the combo becomes idle.
This could be achieved by a trigger function, as described in
another article. The idea would be to use the `change´ event
merely to write the current search string to another text box, in
turn used as argument in a trigger function of a third. This
function gets executed as soon as Access is idle, which doesn't
happen when a fast typist is actively entering a search string.
This solution would be quite clean (it would effectively detect the
idle state), but not very elegant. On every form where a hotel
needs to be selected, three interacting controls would be used
(two of them hidden), which becomes hard to maintain.
4 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
This leads to the `not in list´ event. In almost all cases, the
editing will end with a search string that is not as such “in the
list”. However, if a single hotel has been identified, it makes
sense to automatically select that record. As a matter of fact, the
first hotel in the list could be selected whenever the user tabs
out of the control.
A Timer Form
5 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
the sole purpose of counting down some interval, and to trap its
timer event. This is of course a bit of an overkill — opening an
entire form just for a timer — but since Visual Basic isn't itself
multi-threaded, this is the only option. The form really only
needs a module (for easy instantiation) and an active timer
event.
Another form's class module using the timer form could look like
this:
Option Explicit
When the first keystroke occurs in the combo box, a timer form
is instantiated. At each keystroke, the timer interval is reset. If
the timer runs out, the event is used to filter the combo box.
6 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
Should several features use the timer form, they would not enter
in conflict, as each module creates its own instance(s), with
independent timers.
The text is split into words, separated by spaces, and each word
is treated as a distinct search criteria. All keywords need to exist
in the hotel name, but not necessarily in the order give. This is
useful if the user first types “parc” and then, seeing the results,
adds another keyword as in “parc grand” to locate one of the
“Grand Hotel du Parc”. The search string “grand -parc” would be
interpreted literally, and would not mean “all grand hotels
excluding the parc hotels”.
7 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
Option Explicit
8 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
End Sub
The ComboBox
the first non-hidden column combining the name with the city,
Const cstrSelect _
= " SELECT ID," _
& " Chr(9)+Establishment & ', '+City AS
Display," _
& " Establishment," _
& " City," _
& " State" _
& " FROM Hotels"
Const cstrOrderBy _
= " ORDER BY City, Establishment, ID"
9 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
The only surprise here is the Chr(9) in front of the first visible
column. This is the internal code for the Tab character, which
cannot be entered in any field in Access. Although this character
is not visible, it completely shuts down the auto-complete
feature. When the user types “royal”, it will not match the “Royal
Manotel, Genève”, because of the missing leading tab
character.
Notice also that the standard sort order isn't alphabetic. In this
instance, it's more useful to sort on the city than on the hotel
name alone. After entering the keyword “jeune” (for youth
hostels), the city becomes the key column.
The call to the Init method from the form allows the class
module to trap all needed events.
10 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
End Sub
The Debug lines are useful at design time; they will stop
execution if the combo box doesn't trigger the four events
needed. As can be seen in the form module above, the events
do not need to be handled by the form (unless this is needed for
some other purpose), but they must be active.
Combo Events
11 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
Since auto-complete has been shut down, the `not in list´ event
should handle the exit from the combo in a graceful manner. If
the user ends the session with a mouse or arrow key selection
of a hotel, then a hotel has been selected. If not, the event
performs the selection.
12 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
If .ListCount = 0 Then
.RowSource = "SELECT Null, Null, '***
no match ***'"
.Dropdown
.Undo
Response = acDataErrContinue
mfDirty = True
Else
.RowSource = "SELECT " & .ItemData(0) &
", '" & NewData & "'"
Response = acDataErrAdded
mfDirty = True
End If
End With
mvarLast = "*"
End Sub
The core of the function simply selects the first item from the list.
If no items were found, the combo is used as a message box,
and shows “no match”. The code can easily be modified to
handle the case where more than one item is in the list. Instead
of just taking the first one, the function could force the user to
make a selection among them, with or without displaying an
error message. This is elaborated in the demo database.
13 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
or she will not wait for the list refresh and tab out immediately...
This is the most complex, yet probably the least useful code
sample, as it depends totally on the present data and on the
arbitrary search syntax created for it. However, a few essential
programming techniques are worth explaining.
mfrmClock.TimerInterval = 0
If mcboAny.ListIndex >= 0 Then GoTo Done
mfDirty = True
14 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
Done:
sfBusy = False
End Sub
15 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
The core of the function splits the search criteria at the comma,
and further splits the first part at every space. Lines 20-28 build
the criteria against the hotel name, and lines 30-39 against
either the state field or against the city field. The boolean
function IsStateCode() performs a simple lookup of any two-
character search string in the appropriate table.
The Ô in Hôtel
16 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
Summary
17 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
18 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
copy the corresponding SQL query to the class module; the few
places where field names are used must also be updated,
naturally. Finally, the search syntax must be implemented in
order to produce a valid criteria for any search string.
Search Optimization
The assumption for a city search is that the first characters were
entered.
Again, the index can be used, even when the search pattern
starts with a set. However, the keywords for the hotel names
can be found anywhere. This requires a leading wildcard, as in:
However, users will almost always type the first letters of a word.
So if another linked table exists with the distinct words used in
every hotel name, then that table could be used in the criteria as
a subquery.
The leading wildcard is gone, and the index can come back into
play. The engine will lookup the ID numbers of hotels containing
19 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
the word “bains” (or starting like that), and never even open the
other records. What's more, database engines are optimized to
handle multiple joins as criteria and the resulting execution
speed is astonishing, even on large tables when combining
several of these criteria.
TimeOut is simply the delay between the last keystroke and the
search. I settled on 0.3 seconds, but feel free to experiment.
I had first considered writing two distinct articles, the first being
titles “using a combo box as search box” (basically with
OnTheFly turned off), but the code base is so similar that
making it an option is largely preferable. If your application
already has a user option dialogue, it is easy to add the option
to enable or disable “on the fly” searching.
20 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
If you want to remove the option completely, delete the first two
variables described above and the object variable mfrmClock.
Then basically delete the lines that no longer compile; also, the
timer form is no longer needed.
Hotels.mdb
Conclusion
21 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
I hope you found this useful, as starting point for your own
search boxes, as demonstration of dynamic searches with a
custom search syntax, or as inspiration for other “smart combo”
techniques!
22 of 23 5/26/2022, 2:13 PM
Search (While You Type) in a ComboBox about:reader?url=https%3A%2F%2Fwww.experts-exchange.com%2Fa...
Markus G Fischer
(°v°)
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
This article is part of the EE-book unrestricted Access.
23 of 23 5/26/2022, 2:13 PM