Lookup creation in run time Problem

Discussion of open issues, suggestions and bugs regarding Virtual Data Access Components for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
m227
Posts: 59
Joined: Mon 06 Aug 2007 12:41

Lookup creation in run time Problem

Post by m227 » Tue 17 May 2011 13:54

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?

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Tue 17 May 2011 14:27

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;

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Wed 18 May 2011 08:13

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.

m227
Posts: 59
Joined: Mon 06 Aug 2007 12:41

Post by m227 » Wed 18 May 2011 08:35

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

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Wed 18 May 2011 09:26

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)

m227
Posts: 59
Joined: Mon 06 Aug 2007 12:41

Post by m227 » Thu 19 May 2011 07:50

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.

AlexP
Devart Team
Posts: 5530
Joined: Tue 10 Aug 2010 11:35

Post by AlexP » Thu 19 May 2011 09:19

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.

Post Reply