Live, As-You-Type Search in FileMaker

FileMaker 10’s Script Triggers feature opens the door to create richer, more dynamic and more responsive user interfaces.  For example, it is now possible to create as-you-type search functionality similar to that found in iTunes and Mac OS X Spotlight where the list of results updates dynamically as the user types. This is a slick feature. I’ve added this functionality to a few of my client’s databases, and it has never failed to elicit a glowing response.

Place a global search field with a script trigger in the header of your list layout for live search-as-you-type functionality

Place a global search field with a script trigger in the header of your list layout for live search-as-you-type functionality

Step One: Create a Global Search Field

This step is the same as Step One in my article, Google-Like Searches In FileMaker. If you have already done this for that project, you can skip this step and use the same field. Otherwise, create a text field in any table. In the field options, set the new field to use global storage as shown in the figure below.

Place a global search field with a script trigger in the header of your list layout for live search-as-you-type functionality

Under the storage tab in the field options, select the “Use global storage (one value for all records)” check box.

Tip: To keep your data tables clean, I recommend having a separate table to hold global fields, such as this one, that are not directly associated with a particular table. Global fields can be accessed from any context whether or not a relationship exists between the tables.

Step Two: Create the Triggered Find Script

Create a script that will be triggered when the user enters text. The script will be similar to the one created in the Google-Like Searches In Filemaker – Non Contiguous article except that it will also capture the cursor position within the find field and return it there when complete, and it will not present an error dialog if the found set is empty. Here’s an overview:

  • Set variables to store the position of the cursor in the search field and the selection size.
  • Enter find mode (do not pause or specify find request).
  • Loop through each search term in the global field, create a find request and use the “Set Field” script step to set the find criteria in each field to search. (For more information on understanding this looping process, see Google-Like Searches In Filemaker – Non Contiguous.)
  • Perform Find in first loop iteration and Constrain Found Set in subsequent iterations.
  • Use the Set Selection script step to place the cursor back in the search field at the position stored at the beginning of the script.

The script will look something like this:

Set Variable [ $selectionStart ; Get ( ActiveSelectionStart ) ]
Set Variable [ $selectionSize ; Get ( ActiveSelectionSize ) ]
Commit Records/Requests [ No dialog ]
Set Variable [ $searchList ; Substitute ( g_SearchField ; ” ” ; ¶ ) ]
Set Variable [ $i ; 1 ]
Loop
Exit Loop If [ $i > ValueCount ( $searchList ) ]
Set Variable [ $thisTerm ; GetValue ( $searchList ; $i ) ]
Enter Find Mode [ ]
Set Field [ Contacts::First Name; $thisTerm ]
New Record/Request
Set Field [ Contacts::Last Name; $thisTerm ]
New Record/Request
Set Field [ Contacts::Street Address 1; $thisTerm ]
New Record/Request
Set Field [ Contacts::City 1; $thisTerm ]
New Record/Request
Set Field [ Contacts::State Province 1; $thisTerm ]
New Record/Request
Set Field [ Contacts::Postal Code 1; $thisTerm ]
Set Error Capture [ On ]
If [ $i = 1 ]
Perform Find [ ]
Else
Constrain Found Set [ ]
End If
Set Error Capture [ Off ]
Exit Loop If [ Get ( FoundCount ) = 0 ]
Set Variable [ $i ; $i + 1 ]
End Loop
Set Selection [ Globals::g_SearchField; Start Position: $selectionStart; End Position: $selectionSize ]
Tip: Maintaining performance and responsiveness is critical in this script since it will be run at each keystroke. To help with this, I recommend the following:

  • Add a Freeze Window script step to the beginning of this script and a Refresh Window script to the end. Doing so will help to improve performance and eliminate any possible screen flashes. This is a good general procedure to practice in any of your scripts that change modes, found sets or layouts.
  • Use only fields that can be indexed in the search. Avoid related fields or calculations that include them.

Step 3 – Set the Script Trigger

In layout mode, right click on the search field and click “Set Script Triggers …” A dialog will appear:

In the script trigger dialog, select the action (or event) that you want to trigger the script, and select the script to be triggered.

In the script trigger dialog, select the action (or event) that you want to trigger the script, and select the script to be triggered.

Select the OnObjectModify action which will run the specified script any time text is entered in or deleted from the field. Click Select and choose the script that you created in Step Two.

…And You’re Done!

Now the user can simply begin typing in the field and their search results will be refined as they type without requiring that the user enter find mode, know which fields to search or even click a button.

If you have any questions or comments feel free to post a comment below or contact me directly.

60 Comments