Question

I have to convert many Excell files (converted to CSV) to the database layout of my application.

In my table there are some fields that uses foreign keys for other tables lists and when my convert code finds a field like that, it does not have the foreign id, but the simple text typed on the Excell.

I find that at the moment, the best approach is to insert a new row, focus that TDBLookupComboBox field and then send messages through SO to simulate that I am typing the text. The ComboBox autocomplete feature will then select the right item on the foreign list.

If this is the best way, how do I do that? To send these keyboard messages using Delphi 2006 and Firebird?

Was it helpful?

Solution

Instead of "emulating" a user, you should do what the component does by code. It uses the TDataSet.Locate method to find the partial string and sets the DataSet cursor to the found record. Then it uses the lookup source to get the value.

var
  SearchString: string;
begin
  SearchString := 'Dat';
  if LookupDataSet.Locate('MySearchField', SearchString, [loCaseInsensitive, loPartialKey]) then
  begin
    DestDataSet.Edit;
    DestDataSet.FieldByName('FLD_ID').AsInteger := LookupDataSet.FieldByName('FLD_ID').AsInteger;
    // DestDataSet.Post;
  end
  else
    raise Exception.CreateFmt('Lookup value not found for "%s"', [SearchString]);
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top