Link to home
Start Free TrialLog in
Avatar of MaxdOut
MaxdOut

asked on

Problem creating a lookup field at run-time

I am having troubles creating a lookup field at runtime. The problem that I am having seems to be that since I am creating a field for the table, I am losing all of the previous fields when I set the active property to true. This seems very puzzling to me. Below is a code snippet of what I am trying to do.

--------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);
var
  Table1, Table2 : TTable;
  LookupFld:TStringField;

begin
  Table1 := TTable.Create(Form1);
  Table1.Databasename := 'REPDATA';
  Table1.Tablename := 'P';

  Table2 := TTable.Create(Form1);
  Table2.Databasename := 'REPDATA';
  Table2.Tablename := 'C';
  Table2.Active := TRUE;

  // I'm adding these lines just to show a comparison
  Table1.Active := TRUE;         // Fieldcount = 91
  Table1.Active := FALSE;

  LookupFld:=TStringField.Create(Table1);
  LookupFld.FieldName:='FNAME';
  LookupFld.keyfields := 'CKEY';
  LookupFld.lookupdataset := Table2;
  LookupFld.lookupKeyFields := 'PRKEY';
  LookupFld.lookupResultField := 'NAME';
  LookupFld.fieldkind := fkLookup;
  LookupFld.Dataset:=Table1;

  Table1.Active:=true;               // Fieldcount = 1

  Table1.Free;
  Table2.Free;
end;


When I try to open the table, I get an error stating that field "CKEY" is not found. This field does exist in the table. I don't understand why it is that when I create a field for a table, the fieldcount is 1, yet when I just open a table, the fieldcount is as it should be. I've also tried creating the "CKEY" field and then this works, however the fieldcount is then 2 and the other fields are not available.

Any help is appreciated. Thanks in advance.

Sean
Avatar of dwwang
dwwang

This is some how complex to explain:

If you want to add a field to the table, you must firstly add all fields (exist in your table)to it, i.e., to right click the table and then select Fields Editor - "Add Fields..." to add all needed fields to the TTable. This will resolve your losing fields problem.

Is has something to do with the way Delphi adds the fields. I had the same problem once and found a workaround, I'll dig the code up..
Avatar of MaxdOut

ASKER

dwwang, your proposed answer would be a viable solution, however the key to this is that I need to create everything at runtime (both the tables and the fields) and thus I can't add the fields in this manner. Do I need to manually add every field to the table in order for this to work?
ASKER CERTIFIED SOLUTION
Avatar of BlackMan
BlackMan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MaxdOut

ASKER

Thank you very much BlackMan.

You helped me solve a problem which has been slowing me down for a few days. Very much appreciated!

MaxdOut
Oh, I was so unlucky that was unnable to post my answer here before the question finished.

I have a real tricky solution for this problem, it's result-equivalent to BlackMan's, but better.

Hmmm... I decide to keep it until another person met such kind of problem and provide another 200 points! :-))