adding lookup field at runtime

I try to add a lookup field in a dataset at runtime.
The dataset is created at runtime.

 _lkField := TStringField.Create(p_rsCustomers);
 _lkField.FieldName := 'lkcountry';
 _lkField.FieldKind := fkLookup;
 _lkField.DataSet := p_rsCustomers;
 _lkField.Name   := p_rsCustomers.Name + _lkField.FieldName;
 _lkField.KeyFields := 'countryID';
 _lkField.LookupDataSet := p_rsCountrys;
 _lkField.LookupKeyFields := 'countryid';
 _lkfield.LookupResultField := 'wordingfr';
 p_rsCustomers.FieldDefs.Add( _lkField.Name, ftString,20,false );


I get an error message saying that there is no countryid field in the dataset.
I think the error is that I have no fields definition in the dataset but I cannot how to solve this problem.

Thanks for all


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
Check your spelling. Check your query if you use queries. Besides, don't you need a semicolon or something else between  p_rsCustomers.Name and _lkField.FieldName?
Btw. Did you spell it countryid or countryID?
PHDAuthor Commented:
no need of semicolon.
All is in lowercase..

I think the problem is that the dataset has no definition of the fields because it is created at runtime, just before I add the lookup field.

 p_rsCustomers := tadodataset.Create(self);
 p_rscustomers.Name := 'rscustomers';
 p_rsCustomers.CommandText := 'select custid,lastname,address,zipcodeid,city,state,title,company,countryid from acctcustomer order by custid';
 p_rsCustomers.Connection := p_ConnectionSoc;
you need to make something like this before creating your lookup field

  _coField := TStringField.Create(p_rsCustomers);
 _coField.FieldName := 'countryID';
 _coField.DataSet := p_rsCustomers;
 _coField.FieldKind := fkData;
  _coField.Name := p_rsCustomers.Name + 'countryID';
  p_rsCustomers.FieldDefs.Add(_coField.Name, ftString,20,false );
Wim ten BrinkSelf-employed developerCommented:
Try calling p_rsCustomers.FieldDefs.Update;
before you link the tables. The same for the other table.
Try this:

  FieldDef: TFieldDef;
  StringField: TStringField;
  FieldDef := p_rsCustomers.FieldDefs.AddFieldDef;
  FieldDef.DataType := ftString;
  FieldDef.Name := AnsiString(p_rsCustomers.Name + 'lkcountry');
  FieldDef.Required := True;
  FieldDef.Size := 20;

  TField(StringField) := FieldDef.CreateField(nil);
  StringField.DisplayLabel := 'lkcountry';
  StringField.FieldKind := fkLookup;
  StringField.FieldName := 'lkcountry';
  StringField.KeyFields := 'countryID';
  StringField.LookupDataSet := p_rsCountrys;
  StringField.LookupKeyFields := 'countryid';
  StringField.LookupResultField := 'wordingfr';
  StringField.Name := p_rsCustomers.Name + 'lkcountry';

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.