adding lookup field at runtime

Posted on 2004-11-04
Last Modified: 2011-10-03
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


Question by:PHD
    LVL 17

    Expert Comment

    by:Wim ten Brink
    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?
    LVL 6

    Author Comment

    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;
    LVL 6

    Expert Comment

    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 );
    LVL 17

    Expert Comment

    by:Wim ten Brink
    Try calling p_rsCustomers.FieldDefs.Update;
    before you link the tables. The same for the other table.
    LVL 1

    Accepted Solution

    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';

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Suggested Solutions

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now