Hi, everyone!
I have a new problem with my graduation work. :(
My application must open database at runtime and modify data in this database. The database is random. For that reason I have no idea in which table will be inserted data or update or delete.
The problem is that I must make lookup fields to all Foreign keys.
Example: I have table: article_belong_gr_arts (article belong to some group of articles)
fields:
id_article - primary key(PK),
article_name,
id_gr_arts - foreign key(FK) to table gr_articles
I must make a lookup field to id_gr_arts.
But I have table text_ch_desc_article(text characteristic describe article) that have fields:
id_article - FK to article_belong_gr_arts ->his lookup field must return article name
id_text_ch - FK to table that have id_text_ch - PK text_ch_type - type of the characteristic (listed ot not) and text_ch_name - the name of the characteristic
id_list_text_ch - FK to some listed text value if the text characteristic is listed type
text_value - varchar if the characteristic is not listed

example for listed characteristic - color of hair: blond, brown...
example for not listed characteristic - note: Do not give that to kids under 3 years...

As you see in fist table I have 1 FK at second I have 3. I can't make 1 lookup field to all tables and solve the problem.

I try to write a test project where I have two tables:
test1 - id_test1 - PK; test1_name
test2 - id_test2 - PK; test2_name; id_test1 - FK
I write this code:

void __fastcall TForm1::LookupFieldsClick(TObject *Sender)
{
        Test2Table->TableName="TEST2"; //Table2Table - TTable Component from BDE Palette
        Test1Table->TableName="TEST1"; //Table1Table - TTable Component from BDE Palette

        if (Test2Table->Active==true)      //Builder wants Table2Table to be close
                Test2Table->Active=false;

        CountFields->Close();       //CountFields - TQuery that gives the number of
        CountFields->Prepare();     //columns in TEST2  
        CountFields->ParamByName("pktblname")->AsString="TEST2";
        CountFields->Open();
        int allfieldscount = CountFields->FieldByName("CountColName")->AsInteger;

        for (int i=0; i<allfieldscount; i++)  //Here must be add all fields from TEST2
           Test2Table->FieldDefs->AddFieldDef();  //to the DBGrid but obviously that
                                                  //doesn't happen

        TStringField   *LookupField = new TStringField(this);

        LookupField->FieldName = "MyLookup";
        LookupField->FieldKind = fkLookup;
        LookupField->DataSet = Test2Table;

        LookupField->LookupDataSet = Test1Table;

        FKQuery->Close();         //FKQuery - TQuery that gives the name of FK-name
        FKQuery->Prepare();
        FKQuery->Open();

        LookupField->KeyFields = FKQuery->FieldByName("FKNames")->AsString;

        LookupField->LookupKeyFields = "ID_TEST1";
        LookupField->LookupResultField = "TEST1_NAME";

        LookupField->Name = "Test2TableMyLookup";

        Test2Table->Active = true; //Error: Test2Table Field 'ID_TEST1' not found
        //For some reason Test2Table have no added fields in FieldDef Property 
}

The problem is that I can't "add all fields" of this table in DBGrid at runtime.
The other problem is that if I add them at design time (which is not allow to my graduation work) when I execute the same code in the DBGrid appear ID_TEST2,TEST2_NAME,ID_TEST1,MyLookup fields and everything is fine.

How to make it work in runtime? How to add all fields in runtime? Please help.
I search in internet two days and half and I am desperate. Most of the examples was for Delphi. I try to translate it to C++ but without success. Please help!

Thanks in advanced!

I found that
AddFieldDef is only valid in the context of creating field definitions for a table to be created with a subsequent call to the CreateTable or CreateDataSet method. It cannot be used to add fields to an already-existing table.
But that doesn't solve my problem. ;(
Why is it so simple to Add all fields with Field Editor at design time and it is so hard to add them at runtime? ;(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.