Board index » delphi » Need help accessing Paradox memo fields as TMemos

Need help accessing Paradox memo fields as TMemos

How can I read the text from a Paradox Memo field? I need to read several
memo fields (from different records in the same table) into several
TMemo variables (created at run time) on the same form. (TDBMemo is not
useful in this case). The memo field contents may be of any length.

Having added the proper TTable object to my form, and added the Paradox
Memo field to my fields list as a TMemoField, I am unable to access the
contents of the memo field.

The TMemofield property Astring yields (Memo) which is of no use.

The Tmemofield property Text yields (Memo), and seems generally
useless as it holds a max of 255 chars in a TMemo.

I have also tried to use the Tmemofield GetData method as follows...

var Buffer: PChar;

GetMem(Buffer,MemoField.DataSize+1);
if not MemoField.GetData(Buffer) then
  MessageDlg('TMemofield is NULL', mtInformation, [mbOK], 0)
else
  begin
    Buffer:=StrCat(Buffer,#0);
    { do something with Buffer }
  end;
FreeMem(Buffer,MemoField.Size+1);

...but the GetData method gives a GPF (Operation not Applicable)

(I also tried it with an untyped pointer, with the same result)

This seems like a very common type of data access. What should I be doing?

Thanks, in advance.

Dave.

 

Re:Need help accessing Paradox memo fields as TMemos


Quote
Dave McDonald (dbmcd...@direct.ca) wrote:

: How can I read the text from a Paradox Memo field? I need to read several
: memo fields (from different records in the same table) into several
: TMemo variables (created at run time) on the same form. (TDBMemo is not
: useful in this case). The memo field contents may be of any length.

This becomes a frequently asked question.

: I have also tried to use the Tmemofield GetData method as follows...
[..]

: ...but the GetData method gives a GPF (Operation not Applicable)

The GetData and SetData method don't work for descendents of TBlobField,
e.g. the TMemoField.

: This seems like a very common type of data access. What should I be doing?

You have to use streams instead - create a TBlobStream somewhat like

Stream := TBlobStream.Create(TheTMemoField, bmread);

and then you have a PChar with complete contents of the Memofield. You then
can either use the settextbuf method of the TMemo or directly use the
LoadFromStream method of the TMemo. (and don't forget to free the stream
afterwards)

Only problem is when you have the memo text with unix-like line endings
because the TDBMemo changes this to the DOS-like CR+LF. By putting the text
into a memo with SetTextBuf this isn't done automatically.

Hope that helps,
   Andy

--
----------------------------------------------------------------------------
Andreas H"orstemeier                       | This Signature is Shareware
email: hoer...@hal1.physik.uni-dortmund.de |
       a...@farpoint.sauerland.de          | Please pay $10 to read
fido:     2:2444/4505.3                    |
astronet: 122/490/1.53                     |
----------------------------------------------------------------------------  

Re:Need help accessing Paradox memo fields as TMemos


Quote
hoer...@hal1.physik.uni-dortmund.de (Andreas Hoerstemeier) wrote:
>You have to use streams instead - create a TBlobStream somewhat like
>Stream := TBlobStream.Create(TheTMemoField, bmread);
>and then you have a PChar with complete contents of the Memofield. You then
>can either use the settextbuf method of the TMemo or directly use the
>LoadFromStream method of the TMemo. (and don't forget to free the stream
>afterwards)
>Only problem is when you have the memo text with unix-like line endings
>because the TDBMemo changes this to the DOS-like CR+LF. By putting the text
>into a memo with SetTextBuf this isn't done automatically.
>Hope that helps,
>   Andy

Here is a source example that I composed to retrieve the contents of a TMemoField into a
PChar along with Control Charactor removal. They are part of my almost finished printer
routines for non-visualy report creation.

RECOMMENDED USE: Create a PChar with size found from TMemoFieldSize. Call
TMemoFiledToPChar to retrieve the text, control chars and all, to your PChar. Optionally
use RemoveCtrlChar to delete ALL line feeds and returns. By following below, you can most
likely do something simular to assign the PChar to a TMemo using bmWrite instead of
bmRead. I know this is possible but I haven't had a use for it yet!

function TBWF.TMemoFieldToPChar(Memo: TMemoField; var Buffer: PChar; Size: Word): Integer;
var
  BS: TBlobStream;
begin
  try begin
    BS := TBlobStream.Create(TMemoField(Memo), bmRead);
    FillChar(Buffer^, Size + 1, #0);
    Result := BS.Read(Buffer^, Size);
  finally
    BS.Free;
  end;
end;

function TBWF.TMemoFieldSize(Memo: TMemoField): Word;
var
  BS: TBlobStream;
begin
  try begin
    BS := TBlobStream.Create(TMemoField(Memo), bmRead);
    Result := BS.Size;
  finally
    BS.Free;
  end;
end;

procedure TBWF.RemoveCtrlChar(var InBuf: PChar; Size: Word);
var
  Input,
  Output,
  Orig: PChar;
begin
  GetMem(Output, Size);
  Input := InBuf;
  Orig := Output;
  while input^ <> #0 do begin
    if (input^ <> #10) and (input^ <> #13) then begin
      output^ := input^;
      inc (output);
    end;
    inc (input);
  end;

  Output^ := #0;
  Output :- Orig;
  Inbuf := Output;
end;

Brett Fleming
bflem...@bev.net

Other Threads