Board index » delphi » HELP: Retrieve a TMemoField and copy lines of text to TStrings object

HELP: Retrieve a TMemoField and copy lines of text to TStrings object

I am trying to retrieve a TMemoField via FieldByName() using SQL and store
the result in a TStrings object.  When I try to do this, I get a run-time
210.

An example:

TMyRec = record
  Name : String[30];
  Address: TStrings;
end;

function GetCleanRec: TMyRec;
begin
  with Result do
  begin
    Name := '';
    Address := TStrings.Create;
  end;
end;

function GetDBRec(Record: LongInt): TMyRec;
var Qry: TQuery;
begin
   {Init of Qry and SQL open work fine... ommitted}
  with Result do
  begin
    Name := FieldByName('Name').AsString;
    Address.Text := FieldByName('Address').Text;  {<---- Line in question}
  end;
end;

How can I get this to work?  Is there an easier way at all?  Can a TStrings
be part of a record?

Thanks,
- Craig.

 

Re:HELP: Retrieve a TMemoField and copy lines of text to TStrings object


Quote
"Mega PC" <meg...@lia.co.za> wrote:
> I am trying to retrieve a TMemoField via FieldByName() using SQL and store
> the result in a TStrings object.  When I try to do this, I get a run-time
> 210.

Isn't that due to the abstract nature of TStrings: as stated by the
ref., it has no meanings of storage by itself. Why not use string? If
you need some features of TStrings, maybe you should use TStringList?

Re:HELP: Retrieve a TMemoField and copy lines of text to TStrings object


Quote
Mega PC wrote:

> I am trying to retrieve a TMemoField via FieldByName() using SQL and store
> the result in a TStrings object.  When I try to do this, I get a run-time
> 210.

TStrings is used by components for string storage.  You need to use
TStringList, and the Assign method -

MyStringList.Assign(FieldByName('Whatever'));

--
Mark.

Re:HELP: Retrieve a TMemoField and copy lines of text to TStrings object


Quote
"Mega PC" <meg...@lia.co.za> wrote:
>I am trying to retrieve a TMemoField via FieldByName() using SQL and store
>the result in a TStrings object.  When I try to do this, I get a run-time
>210.
>An example:
>TMyRec = record
>  Name : String[30];
>  Address: TStrings;
>end;
>function GetCleanRec: TMyRec;
>begin
>  with Result do
>  begin
>    Name := '';
>    Address := TStrings.Create;
>  end;
>end;
>function GetDBRec(Record: LongInt): TMyRec;
>var Qry: TQuery;
>begin
>   {Init of Qry and SQL open work fine... ommitted}
>  with Result do
>  begin
>    Name := FieldByName('Name').AsString;
>    Address.Text := FieldByName('Address').Text;  {<---- Line in question}
>  end;
>end;
>How can I get this to work?  Is there an easier way at all?  Can a TStrings
>be part of a record?
>Thanks,
>- Craig.

You have to create an instance of the object first before you can make
assignments to it.

myrec := tMyrec.Create

MyRec.Name := 'xxxxx';
myRec.Address := TStrings.Create;
myRec.Address - do something.

The TMyRecs also have to be passed to your functions as parameters or
created within your functions to be returned as results or passed by
reference to updated
result := myRec

Other Threads