Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Adding a Lookup Field to a ADODataset in runtime?

Posted on 2003-03-17
5
Medium Priority
?
1,875 Views
Last Modified: 2010-05-18
Hi,
I like to add a LookupField to a ADO Dataset at runtime, since there's always a
different query filling the dataset I don't know the fields in design time.
But I do know the one field I want to use to get filled with data from the
lookupfield is always there.
The the LookupField needs to be connected to another dataset for the lookupdata.
What need I do to create this kind of field after the ADODataset opens?

Regards,
Corpa
0
Comment
Question by:corpa
  • 3
5 Comments
 
LVL 1

Expert Comment

by:olmy
ID: 8154244
Hi

I found it difficult also to add persistent fields at runtime, but after searching I find it quite easy to do.
I don't have Delphi at this computer, but the basic manover is something like this.

1. create persistent fields for every field in a database. Do this just before calling the <query>.open-procedure.
- I did a simple routine that checkes fieldtypes from query's "fielddefs" (or "fields") that can be retrieved before calling open-procedure. It creates TStringField, TIntegerField, TMemoField, etc. depending the actual field type.
- you have to create persistent field for every field you wish to use with your query.
- dataset-property must be also defined
 
2. also create new persistent field for the lookupfield
3. open the database ;)


I'll send you more detailed examples tommorrow when I have Delphi at my reach.

Regards
  Olmy
0
 
LVL 1

Accepted Solution

by:
olmy earned 500 total points
ID: 8157271
Here are the examples. I hope this helps.

// PART 1 CREATE PERSISTENT FIELDS
//This is for TClientDataset, but you can replace it with etc. TDataSet.

procedure CreatePersistentFields(aDataSet : TClientDataSet);
var
  ii: Integer;
  NewField: TField;
begin
  with aDataSet do
  begin
    FieldDefs.Update;
 
    for ii := 0 to Pred( FieldDefs.Count ) do
    begin
      NewField := nil;
      case FieldDefs[ ii ].DataType of
        ftString : NewField := TField( TStringField.Create( aDataSet ));
        ftWideString : NewField := TField( TWideStringField.Create( aDataSet ));
        ftLargeint : NewField := TField( TLargeintField.Create( aDataSet ));
        ftSmallint : NewField := TField( TSmallIntField.Create( aDataSet ));
        ftInteger : NewField := TField( TIntegerField.Create( aDataSet ));
        ftBoolean : NewField := TField( TBooleanField.Create( aDataSet ));
        ftFloat : NewField := TField( TFloatField.Create( aDataSet ));
        ftCurrency : NewField := TField( TCurrencyField.Create( aDataSet ));
        ftBCD : NewField := TField( TBCDField.Create( aDataSet ));
        ftDate : NewField := TField( TDateField.Create( aDataSet ));
        ftTime : NewField := TField( TTimeField.Create( aDataSet ));
        ftDateTime : NewField := TField( TDateTimeField.Create( aDataSet ));
        ftAutoInc : NewField := TField( TAutoIncField.Create( aDataSet ));
        ftBlob : NewField := TField( TBlobField.Create( aDataSet ));
        ftMemo : NewField := TField( TMemoField.Create( aDataSet ));
        ftGraphic : NewField := TField( TGraphicField.Create( aDataSet ));
        else
          raise Exception.Create( 'CreatePersistentFields: File type not supported "'+ FieldDefs[ii].DisplayName +'"' );
      end;(* case *)
 
      if NewField <> nil then
      with NewField do
      begin
        FieldName := aDataSet.FieldDefs[ ii ].DisplayName;
        Size := aDataSet.FieldDefs[ ii ].Size;
        DataSet := aDataSet;
      end;
    end;(* for *)
  end;(* with *)
end;



// PART 2

      (* create new persistent lookupfield *)
        with TStringField.Create( DataSource.DataSet ) do
        begin
          fieldkind := fkLookup;
          fieldname := //field name must not already exist 'LOOK'+<old fieldname>
          DataSet := //set your own values
          KeyFields := //set your own values
          LookupKeyFields := //set your own values
          LookupDataSet := //set your own values
          LookupResultField := //set your own values
          DisplayLabel := //set your own values
        end;(* TStringField *)
0
 

Author Comment

by:corpa
ID: 8161183
This is exactly what I wanted,
many thanks,

corpa
0
 
LVL 13

Expert Comment

by:rfwoolf
ID: 22645654
Olmy thanks so much for the fantastic solution. I'm posting this 5 years later and it's been a great deal of help! :)
0
 
LVL 1

Expert Comment

by:olmy
ID: 22647459
I'm very happy that you found what you were looking for. Thank you for your lovely message :D
  Olmy
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 11 hours left to enroll

581 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