Solved

Problem creating a lookup field at run-time

Posted on 1998-12-14
6
152 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Jpg /PNG To GIF 5 138
Moving (cutting/pasting) controls in a TTabbedNotebook... 7 34
Delphi...Split view - idea? 1 70
DBGrid or StringGrid ? 6 91
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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

822 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