Solved

Problem creating a lookup field at run-time

Posted on 1998-12-14
6
151 Views
Last Modified: 2010-04-04
I am having troubles creating a lookup field at runtime. The problem that I am having seems to be that since I am creating a field for the table, I am losing all of the previous fields when I set the active property to true. This seems very puzzling to me. Below is a code snippet of what I am trying to do.

--------------------------------------------------------

procedure TForm1.Button1Click(Sender: TObject);
var
  Table1, Table2 : TTable;
  LookupFld:TStringField;

begin
  Table1 := TTable.Create(Form1);
  Table1.Databasename := 'REPDATA';
  Table1.Tablename := 'P';

  Table2 := TTable.Create(Form1);
  Table2.Databasename := 'REPDATA';
  Table2.Tablename := 'C';
  Table2.Active := TRUE;

  // I'm adding these lines just to show a comparison
  Table1.Active := TRUE;         // Fieldcount = 91
  Table1.Active := FALSE;

  LookupFld:=TStringField.Create(Table1);
  LookupFld.FieldName:='FNAME';
  LookupFld.keyfields := 'CKEY';
  LookupFld.lookupdataset := Table2;
  LookupFld.lookupKeyFields := 'PRKEY';
  LookupFld.lookupResultField := 'NAME';
  LookupFld.fieldkind := fkLookup;
  LookupFld.Dataset:=Table1;

  Table1.Active:=true;               // Fieldcount = 1

  Table1.Free;
  Table2.Free;
end;


When I try to open the table, I get an error stating that field "CKEY" is not found. This field does exist in the table. I don't understand why it is that when I create a field for a table, the fieldcount is 1, yet when I just open a table, the fieldcount is as it should be. I've also tried creating the "CKEY" field and then this works, however the fieldcount is then 2 and the other fields are not available.

Any help is appreciated. Thanks in advance.

Sean
0
Comment
Question by:MaxdOut
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Expert Comment

by:dwwang
ID: 1351016
This is some how complex to explain:

If you want to add a field to the table, you must firstly add all fields (exist in your table)to it, i.e., to right click the table and then select Fields Editor - "Add Fields..." to add all needed fields to the TTable. This will resolve your losing fields problem.

0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1351017
Is has something to do with the way Delphi adds the fields. I had the same problem once and found a workaround, I'll dig the code up..
0
 

Author Comment

by:MaxdOut
ID: 1351018
dwwang, your proposed answer would be a viable solution, however the key to this is that I need to create everything at runtime (both the tables and the fields) and thus I can't add the fields in this manner. Do I need to manually add every field to the table in order for this to work?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Accepted Solution

by:
BlackMan earned 200 total points
ID: 1351019
Here's the workaround I made when I had the problem:
It is important to set Active to True and then to False before you add the fields, otherwise they are deleted again. This code works for D2, I don't know if the problem is still there in D3/D4..

  With FtblVarer Do
  Begin
    Active := True;
    Active := False;
    for I := 0 to FieldDefs.Count - 1 do
      with FieldDefs[I] do
       if DataType <> ftUnknown then CreateField(Self);
  End; (* With *)

  lField := TFloatField.Create(Self);
  lField.Calculated := True;
  lField.FieldName := 'Lagerverdi';
  lField.DataSet := FtblVarer;


0
 

Author Comment

by:MaxdOut
ID: 1351020
Thank you very much BlackMan.

You helped me solve a problem which has been slowing me down for a few days. Very much appreciated!

MaxdOut
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1351021
Oh, I was so unlucky that was unnable to post my answer here before the question finished.

I have a real tricky solution for this problem, it's result-equivalent to BlackMan's, but better.

Hmmm... I decide to keep it until another person met such kind of problem and provide another 200 points! :-))
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

914 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

14 Experts available now in Live!

Get 1:1 Help Now