Link to home
Start Free TrialLog in
Avatar of GrahamDLovell
GrahamDLovell

asked on

How to load a ClientDataSet in Delphi from a Resource file?

I have a CDS file called PAYGHeadings.cds, which is referenced in a CB.rc file as PAYGHeadings RCDATA system\PAYGHeadings.cds.

The form has an embedded ClientDataSet of cdsPAYGHeadings.

The .RES file seems to be OK, and with the link in the .dpr as {$R 'CB.res' 'CB.rc'}.

So far so good.

Here is the relevant code:
function TfDB.ActivatePAYGHeadings: boolean;
var
  ResStream: TResourceStream;
begin
  try
    ResStream := TResourceStream.Create(hInstance, 'PAYGHeadings', RT_RCDATA);
    cdsPAYGHeadings.LoadFromStream(Resstream);
    cdsPAYGHeadings.IndexDefs.Clear;
    with cdsPAYGHeadings.IndexDefs.AddIndexDef do
    begin
      Name := 'MainIdx';
      Fields := 'EffectiveDate, Scale';
      Options := [ixUnique];
    end;
    cdsPAYGHeadings.IndexName := 'MainIdx';
    result := true;
  except
    screen.cursor := crDefault;
    fDiagnosis.eTable.text := 'PAYGHeadings';
    fDiagnosis.showModal;
    result := false;
    exit;
  end;
end;

Open in new window

The CDS has a primary index of EffectiveDate and Scale;

Yet the code fails on assigning the IndexName.

I suspect I am missing a line or two of code, but I can't fathom the logic of how to set up the appropriate pointers.
ASKER CERTIFIED SOLUTION
Avatar of developmentguru
developmentguru
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GrahamDLovell
GrahamDLovell

ASKER

Thanks developmentguru for your response.

Yes, it is a binary file, and it can be loaded normally.

Can you give me your "loading from XML in a resource" code? That would provide a satisfactory solution to my problem, although it would be nice to understand the correct way to set up the stream (which obviously I have got wrong).

I have not previously loaded data from a resource file.
Further work on developmentguru's suggestion.

I just got the Fields assignment wrong.

In the code cited I had Fields := 'EffectiveDate, Scale';

The fields should have been separated by a semi-colon, not by a comma.

My Stream assignments were obviously OK.