Fast Lookup in FoxPro


If you want to let users make a fast choice from values in a table then you normally use a combo or list box. This technique works well until you need to work with a large table. The technique described here lets the user choose from a million and a half postcodes.

When the user enters each character of the postcode the textbox finds and displays the first address which matches that partial code. The sample shows the situation after the user has typed 'B4'.

The first entry with this code is 'B4 6AA' and the matching address is displayed.
[Partial postcode]
   
If the user goes on to type 'B41' - a non-existent postcode - then the address changes to show 'Not found' and the postcode itself turns red.

As the user backspaces to delete the '1', the address reverts to the last good entry matching 'B4'.
[Incorrect postcode]

The autocomplete of the postcode field and the display of the address happen as quickly as the user can enter and delete characters into the textbox. A combobox populated with the million and a half postcodes took minutes to load and ran very slowly.

How to do it

The code to achieve this effect is surprisingly simple. The form itself opens the PostCode table and sets it in PostCode order. All the rest is done by the InteractiveChange method of the textbox:

With This
   lcTarget = Left(Upper(Alltrim(.Value)),.SelStart)
   lnLength = .SelStart      && Remember the cursor position
   If Seek (lcTarget)
     .ForeColor = 0
     *-- Fill the rest of the postcode and ...
     *-- ... put the cursor back where it was
     .Value = postcode
     .SelStart = lnLength
     .Parent.lblAddress.Caption = address1 + Chr(13) + ;
             address2 + Chr(13) + town
   Else
     .ForeColor = 255
     .Value = lcTarget
     .SelStart = lnLength
     .Parent.lblAddress.Caption = 'Not found'
   Endif
Endwith

If you are using VFP 9 then you can use the new AutoComplete property of the textbox to get much the same effect without any coding at all.

Workshop course

This particular requirement came up during a workshop course. We went on to convert the textbox into a self-contained control and save it as a class. This class has properties such as cTableName, cOrder, cSearchField and cDisplayField and can be dropped onto any form in order to provide a fast lookup facility.


Hints & tips

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.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map