0

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?

NaN
  • 8,596
  • 20
  • 79
  • 153
  • 3
    Why is your user interface having anything to do with an automated import process? You should not have to use a GUI component's key handling to do anything like this - you should be doing the lookup yourself in code with a query. You may have to use a `LIKE` in the `WHERE` clause, but using a GUI control's autocomplete to find a match is not the solution. GUI controls should be used for the **user interface** (the *UI* part of `GUI`), not to substitute for actual query functionality. – Ken White Aug 17 '12 at 04:36
  • 1
    Epic bad idea. Stuff like this gives "Delphi programmers" a bad name. – Warren P Aug 17 '12 at 12:37
  • I have to agree with both Ken and Warren. Simulating key-strokes or mouse clicks is a really bad solution when used as flow control, rather than user interface. Think about an alternative design. – Sean B. Durkin Aug 17 '12 at 12:49
  • I do agree with them too! I am not that noob it sounds, but I have to do it fast and the entire application is finished four years ago. I have to do that as an emergency to one of the clients. The thing is that I have all the events made in OnXxxxx, and I don't have the time to remake all the field filling and the queryies, but I see that I have o choice... – NaN Aug 17 '12 at 13:07
  • 1
    @EASI: Using the Lookup-DataSet's Locate method is much easier than sending keystrokes around. You already have the data in the Lookup-DataSet, so you can do LookupDataSet.Locate('FieldName', SearchString, [loCaseInsensitive, loPartialKey]) and the LookupDataSet will point to the exact same record that the DBLookupComboBox would point to. – Andreas Hausladen Aug 17 '12 at 19:43
  • I did that yesterday because I was at the dead line. This is be best approach indeed. Please convert to a answer so I can choose it. – NaN Aug 18 '12 at 02:28
  • Even under time pressure that which does not work is not faster. – Warren P Aug 19 '12 at 00:37

1 Answers1

1

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;
Andreas Hausladen
  • 8,081
  • 1
  • 41
  • 44