Solved

Problem creating a lookup field at run-time

Posted on 1998-12-14
6
150 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 7

Accepted Solution

by:
BlackMan earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

13 Experts available now in Live!

Get 1:1 Help Now