Home About us Development Training Support |
Fast Lookup in FoxProIf 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.
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 itThe 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 courseThis 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.
More...
|