YOUSEFUL / DELPHI / COMPONENTS
TAtlasDBLookupCombo
A data-aware lookup combo box that selects by key or by case-insensitive display text, checks the key against the lookup table and leaves the master record in edit state.
Description
TAtlasDBLookupCombo is a TDBLookupComboBox descendant. Set ListSource, KeyField and ListField for the lookup table and DataSource and DataField for the master field, exactly as for the standard control. The drop-down list, keyboard handling and KeyValue are the standard ones.
It adds two methods for selecting in code:
SelectKeychecks that the key exists in the lookup dataset and that the master field can be written, then stores the key in the master field.SelectTextfinds a display value, case-insensitively, as a prefix or as the whole value, and passes its key toSelectKey.
Both put the lookup dataset's cursor back where it was, and neither posts: the master dataset is left in edit state for your code to Post or Cancel. When the key is missing or the master field cannot be written, the master record is not touched.
The lookup dataset must be active, browsing and bidirectional. Only the first field named in ListField is searched. Atlas Info adds no properties or events to this control. Errors raised by its checks are EAtlasDatabaseError.
Members
| Member | Kind | What it does |
|---|---|---|
SelectKey | method | Selects the lookup record whose KeyField equals Value and writes Value to the master field. |
SelectText | method | Searches the first field named in ListField for Text with Locate and loCaseInsensitive. |
Methods
SelectKey method
procedure SelectKey(const Value: Variant);Selects the lookup record whose KeyField equals Value and writes Value to the master field.
Checks, in order:
- The
ListSourcedataset is active (An active data source is required), browsing and bidirectional (Lookup requires a browsing bidirectional dataset). Valueis found withLocateonKeyField(Lookup key does not exist).Nullis not searched for; it clears the selection.- When
DataSourceis assigned: its dataset is active, the control is notReadOnly, and theDataFieldfield is a writable data field (Field is read-only: <name>). A Null value for aRequiredmaster field raisesLookup value is required.
The standard SelectKeyValue then puts the master dataset into edit state, assigns the field and fires OnClick. Without a DataSource, only KeyValue changes. The lookup dataset's current record is restored whether or not the call succeeds.
Lookup.SelectKey(3); // CategoryID := 3, master record in dsEdit
Customers.Post;SelectText method
function SelectText(const Text: string; Partial: Boolean = True): Boolean;Searches the first field named in ListField for Text with Locate and loCaseInsensitive. With Partial True (the default) Text must be the start of the value (loPartialKey); with False it must equal the whole value.
Returns False and changes nothing when no record matches. When one matches, passes that record's KeyField value to SelectKey and returns True, so all of SelectKey's checks apply: a read-only master field still raises. The lookup dataset needs the same state as for SelectKey, and its cursor is restored.
if Lookup.SelectText('par') then // finds "Partner"
Customers.Post;Examples
Add a lookup in code
Shows the category name of each customer and stores the category's ID in the customer's CategoryID field.
uses Data.DB, Vcl.Controls, Atlas.DBControls;
function AddCategoryLookup(Parent: TWinControl; Customers, Categories: TDataSource): TAtlasDBLookupCombo;
begin
Result := TAtlasDBLookupCombo.Create(Parent);
Result.Parent := Parent;
Result.ListSource := Categories; // lookup table with ID and Name
Result.KeyField := 'ID';
Result.ListField := 'Name';
Result.DataSource := Customers; // master table
Result.DataField := 'CategoryID';
end;Select by typed text or by key
ChooseCategory(Lookup, 'par') selects Partner and posts; an unknown text returns False and leaves the record alone. SetCategory(Lookup, 9) raises Lookup key does not exist when there is no category 9.
uses Atlas.DBControls;
function ChooseCategory(Lookup: TAtlasDBLookupCombo; const Typed: string): Boolean;
begin
Result := Lookup.SelectText(Typed); // case-insensitive prefix match on the first ListField
if Result then
Lookup.DataSource.DataSet.Post; // SelectText leaves the master record in edit state
end;
procedure SetCategory(Lookup: TAtlasDBLookupCombo; CategoryID: Integer);
begin
Lookup.SelectKey(CategoryID); // raises EAtlasDatabaseError if the key is not in the list
Lookup.DataSource.DataSet.Post;
end;Related
TAtlasDBGrid · EAtlasDatabaseError
Also in Atlas Info
TAtlasDBGrid · TAtlasDBRichEdit · TAtlasDataFilter · TAtlasFilterEdit