Board index » delphi » How do I create lookup fields in run time without creating persistent fields at design time

How do I create lookup fields in run time without creating persistent fields at design time

Because of the dataset descendant I am using, I prefer to use NO persistent
fields at design time.
At run, time I want to create a lookup field but can't
Quote
I wrote:

  With TStringField.Create(self) do begin
      Size := 10;
      Lookup := true;
      Index := 1;
      FieldName := 'Customer';
      Name := 'ContractsCustomer';
      FieldKind := fkLookup;
      LookupKeyFields := 'Key';
      LookupResultField := 'ShortName';
      LookupDataSet := Companies; // a table
      Dataset := Contracts; // another table
      KeyFields := 'CustomerKey';
  End;
  Contracts.FieldDefs.Update;
  Contracts.Open;  // this line raise exception and said "customerKey" not
found.

However, I got an exception at the line contracts.open and it said
"customerKey" not found. Probably since contracts is not Open yet.
But if I put contracts.Open before all the above code, I get another
exception "Cannot perform this on an open dataset when it executes to the
line:
DataSet := Contracts

what a dilemna.  Can someone help please?
Many thanks.

Dennis

 

Re:How do I create lookup fields in run time without creating persistent fields at design time


Quote
Dennis Poon wrote:

> Because of the dataset descendant I am using, I prefer to use NO persistent
> fields at design time.

I have a similar problem creating a dynamic Calculated field in BCB.  In
my case, I can't use persistent fields as the text for the query is
generated at run-time!  

I think the problem you are getting is that by creating a look-up field,
you have now created a persistent field, only at run time.  This means
that the VCL will only use the persistent fields, and currently your
dataset only has the one.  To test if this is the case, try creating
your missing fields as persistent data fields at run-time in a similar
way.

If that diagnoses the problem, we are still left in the same position.
How can we add calculated/lookup fields at run-time without losing all
the other fields?  If we must lose these fields, is there a call on the
dataset we can make to automatically generate them?  The VCL must get
them internally from somewhere!

If you get an  answer, I'd be delighted to hear of a solution.
Likewise, I'll post back any further findings here.

I'd expect it to be an old problem, VCL is over 5 years old we can't be
the first to hit this problem.  However, I'm having a hard time locating
any other reousrces/responses dealing with the issue...

AlisdairM

Re:How do I create lookup fields in run time without creating persistent fields at design time


Brian Bushay answered that question for me recently.

I quote his message...

Quote
> I was trying to be sneaky.  The order of the fields in the SQL select
> statement, which is created at run time, is controllable by the user.
> I was therefore avoiding having persistent fields in the query

You can add code like this to create persistent fields are runtime
 table1.FieldDefs.Update
  Table1.Close;
  for i := 0 to Table1.FieldDefs.Count - 1 do
   if table1.FindField(table1.FieldDefs[i].Name) = nil then //persistent
field

                                                             //does not
exist

       Table1.FieldDefs.Items[i].CreateField(Table1);

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

--Bill Sparrow--

Re:How do I create lookup fields in run time without creating persistent fields at design time


Quote
Bill Sparrow wrote:
> Brian Bushay answered that question for me recently.

< and passed on a really handy code snippet >

Thanks Bill :? )
I had cobbled a quick solution myself.  Noticing that all I was doing
was using different combinations of fields in the query to filter on,
but always extracting the same result set, I simply added a design time
query that is instantly replaced at run-time.  However, it allowed me to
create my persistent fields!

This was a very limitted circumstance though, and not good for creating
calculated fields at runtime in general.  Your snippet will come in very
handy indeed!

I am quite surprised at the seeming dearth of knowledge on this
subject.  Am I the only one creating such fields at run-time?  Or is the
knowledge deemed so basic it's a sign that I didn't read the friendly
manual?  If the latter, I can assure you my manual was most unfriendly
in this instance!

AlisdairM

Re:How do I create lookup fields in run time without creating persistent fields at design time


Quote
>I am quite surprised at the seeming dearth of knowledge on this
>subject.  Am I the only one creating such fields at run-time?  Or is the
>knowledge deemed so basic it's a sign that I didn't read the friendly
>manual?  If the latter, I can assure you my manual was most unfriendly
>in this instance!

I answer this question often enough to know it is not basic knowledge but it
comes up infrequently enough that my guess is it is rarely used.

--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Other Threads