?
Solved

Problem creating a lookup field at run-time

Posted on 1998-12-14
6
Medium Priority
?
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Accepted Solution

by:
BlackMan earned 800 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

770 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