[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1447
  • Last Modified:

adding lookup field at runtime

Hi,
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 );

 p_rsCustomers.Open;

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

Sang-Do




0
PHD
Asked:
PHD
1 Solution
 
Wim ten BrinkCommented:
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?
0
 
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;
0
 
vadim_tiCommented:
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 );
0
 
Wim ten BrinkCommented:
Try calling p_rsCustomers.FieldDefs.Update;
before you link the tables. The same for the other table.
0
 
LiveBootlegCommented:
Try this:

var
  FieldDef: TFieldDef;
  StringField: TStringField;
begin
  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';
end;
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now