?
Solved

Adding a Lookup Field to a ADODataset in runtime?

Posted on 2003-03-17
5
Medium Priority
?
1,826 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
[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
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
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 Month14 days, 14 hours left to enroll

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