Autocomplete in VFP9

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox. Other properties give you more control over the way that the lookup and autocomplete will work, but at it's simplest all you have to do is to set the AutoComplete property true to enable the behaviour.

As the user types into the textbox, a list drops down to show previous entries from that textbox which match the characters entered so far:

Setting up AutoComplete

There are three properties which control the behaviour of AutoComplete. All three are on the Data tab of the Properties window:

1. AutoComplete

This can take five values:

  1. No autocomplete.
  2. An alphabetical list.
  3. With the most frequently-used values first.
  4. With the most recently-used values first.
  5. In a user-defined order.

This is the only property that you need to alter to enable the basic autocomplete behaviour. A value of 1 is usually the best choice because the alphabetic order seems to be the easiest for the users to grasp.

2. AutoCompTable

As is typical with Microsoft FoxPro, the architecture is open and the past values are stored in a dbf file. The AutoCompTable property holds the name of this table. If the table does not exist then it will be created when the form opens. If you leave the name blank then VFP will use a table named AutoComp.dbf in the home directory.

The table is opened when the form opens and closed again when the form closes but does not appear in the list of work areas. You can modify the contents of the table with the form on screen and change the AutoComplete behaviour in real time.

3. AutoCompSource

This is the identifying name which will be used to look up past values for the textbox. If you give no name then VFP will use the name of the textbox itself. This means that several textboxes on different forms can share the same list provided that they all have the same name. It also means that all textboxes that you've left at the default name of "Text1" will share the same list.

If you're not using VFP 9

The lookup code here provides a similar feature in earlier versions of Visual FoxPro.