Page 1 of 1

Lookup creation in run time Problem

Posted: Tue 17 May 2011 13:54
by m227
Hello.
I found it impossible to create lookup field in run time.

Code: Select all

  vtZamowienie.FieldByName('Sklep_ID').Visible    := True; // to check that this field is present

  vtZamowienie.Close;
  with TStringField.Create(vtZamowienie) do
  begin
      FieldKind := fkLookup;
      FieldName := 'Sklep';
      DataSet   := vtZamowienie;
      Name      := DataSet.Name + FieldName;
      LookupDataSet := vtSklep;
      LookupKeyFields := 'ID';
      LookupResultField := 'Nazwa';
      KeyFields := 'Sklep_ID';
      Lookup := True;
      vtZamowienie.Open; // <----------- here starts error
      vtZamowienie.FieldDefs.Add(FieldName, ftString, 90, False);
  end;
It produces error vtZamowienie: Field 'Sklep_ID' not found. However it is in data set. Can somebody tell me where the error is?

Posted: Tue 17 May 2011 14:27
by AlexP
Hello,

I could not reproduce the problem.
Please send a complete small sample to alexp*devart*com to demonstrate it.

Also provide the following information:
- the exact version of VirtualTable.
- the exact version of your IDE;

Posted: Wed 18 May 2011 08:13
by AlexP
Hello,

To resolve the problem, you should add fields to the vtOrder.Fields list like:

Code: Select all

var
  Client_ID, ID: TIntegerField;
  Product: TStringField;
begin
  vtOrder.Close;
  Client_ID:= TIntegerField.Create(nil);
  Client_ID.FieldName := 'Client_ID';
  Client_ID.DataSet:= vtOrder;

  ID:= TIntegerField.Create(nil);
  ID.FieldName := 'ID';
  ID.DataSet:= vtOrder;

  Product:= TStringField.Create(nil);
  Product.FieldName := 'Product';
  Product.DataSet:= vtOrder;

  with TStringField.Create(vtOrder) do
   .....
or add them with the help of the Fields Editor visual form.

Posted: Wed 18 May 2011 08:35
by m227
Hello Alex.

Unfortunatelly in my real application I load data from file (TVirtualTable.LoadFromFile) so I cannot create this dataset in run-time (as far as i know VirtualTable file storage stores structure too).

My algorithm is:

1. Load VT from file with foreign keys (i.e. Order.Client_ID)
2. Add Lookup to other loaded VT (Client.ID)

I cannot declare lookup in design time cause it will be removed after loading VT.

Michal

Posted: Wed 18 May 2011 09:26
by AlexP
Hello,

In the next version of VirtualTable we will change its behavior - when data is loaded into VirtualTable, fields which are already declared will not be removed.

Now you can add the needed fields after loading data:

1. Load VT from file with foreign keys (i.e. Order.Client_ID)
2. Add the Client_ID field (As I wrote earlier)
3. Add Lookup to the other loaded VT (Client.ID)

Posted: Thu 19 May 2011 07:50
by m227
Hello again Alex. I'm afraid I don't get it.

After point 1. as you mentioned I already have Order.Client_ID field, so how can I add this field again in point 2?

If about update: My problem is not to retain initial fields after loading data from VTD but to be able to add new after loading, so probably your future solution will not suit me.

Posted: Thu 19 May 2011 09:19
by AlexP
Hello,

Below is an example of loading data from files to TvirtualTable and adding the LookUp field in the runtime:

Code: Select all

var
  Client_ID: TIntegerField;
  vtClient, vtOrder :TVirtualTable;
begin
  vtClient:= TVirtualTable.Create(nil);
  VtOrder:= TVirtualTable.Create(nil);

  vtClient.LoadFromFile('Client.vtd');
  vtClient.Open;
  vtOrder.LoadFromFile('Order.vtd');
  vtOrder.Open;


  vtOrder.Close;
  Client_ID:= TIntegerField.Create(nil);
  Client_ID.FieldName := 'Client_ID';
  Client_ID.DataSet:= vtOrder;

  with TStringField.Create(vtOrder) do
  begin
    FieldKind          := fkLookup;
    FieldName          := 'Client';
    DataSet            := vtOrder;
    Name               := DataSet.Name + FieldName;
    LookupDataSet      := vtClient;
    LookupKeyFields    := 'ID';
    LookupResultField  := 'Name';
    KeyFields          := 'Client_ID';
    Lookup             := True;
    vtOrder.Open;
    vtOrder.FieldDefs.Add(FieldName, ftString, 90, False);
  end;
Please change this sample so the problem can be reproduced with it.