-
1
- #1
Following the thread thread102-1198575 :
After 2 hours of web research and tests, I finally found a working solution (using Delphi 7.0) for loading and saving richedit text (with attributes) in an access database.
Rules to follow are :
- the Access field must be of type "OLE Object"
- this field must be in the first position in the query variable(because "fields[0]", see later )
- the query must contain the primary key field (unique id the record), at any but the first position
- you must pass an ADO connection
Here are my functions, adapt them as you like
Examples :
Field is "Notes" in table "Actions" with primary key "ID", RE is Richedit component, DB.GetConnection is my own ADO connection
SaveRicheditToAccess (DB.GetConnection, RE, 'SELECT Notes, ID FROM Actions WHERE ID='+IntToStr(ID));
LoadRicheditFromAccess (DB.GetConnection, RE, 'SELECT Notes, ID FROM Actions WHERE ID='+inttostr(id));
Function SaveRicheditToAccess (ADOC : TADOConnection ; RE : TRichEdit ; _Select : String) : Boolean;
var
ms: TMemoryStream;
ADO : TADODataset;
begin
ms := TMemoryStream.Create;
Try
Result := FALSE;
// save content to stream
RE.Lines.SaveToStream(ms);
ms.Seek(0, soFromBeginning) ;
ADO := TADODataset.Create (NIL);
ADO.Connection := ADOC;
ADO.CommandTExt := _SELECT;
ADO.Active := True;
ADO.Edit;
// Put stream content in blob field and post it to database
TBlobField(ADO.Fields[0]).LoadFromStream(ms);
ADO.Post;
Result := TRUE;
Finally
FreeAndNil (ms);
FreeAndNil (ADO);
End;
End;
Function LoadRicheditFromAccess (ADOC : TADOConnection ; RE : TRichEdit ; _Select : String) : Boolean; // Select doit renvoyer LE champ à modifier
var
ms: TMemoryStream;
ADO : TADODataset;
begin
ms := TMemoryStream.Create;
Try
Result := FALSE;
ADO := TADODataset.Create (NIL);
ADO.Connection := ADOC;
ADO.CommandTExt := _SELECT;
ADO.Active := True;
// Load rich edit content into stream
TBlobField(ADO.Fields[0]).SaveToStream(ms);
ms.Seek(0, soFromBeginning) ;
// Load rich edit content from stream to component
RE.Lines.LoadFromStream(MS);
Result := TRUE;
Finally
FreeAndNil (ADO);
FreeAndNil (Ms);
End;
End;
After 2 hours of web research and tests, I finally found a working solution (using Delphi 7.0) for loading and saving richedit text (with attributes) in an access database.
Rules to follow are :
- the Access field must be of type "OLE Object"
- this field must be in the first position in the query variable(because "fields[0]", see later )
- the query must contain the primary key field (unique id the record), at any but the first position
- you must pass an ADO connection
Here are my functions, adapt them as you like
Examples :
Field is "Notes" in table "Actions" with primary key "ID", RE is Richedit component, DB.GetConnection is my own ADO connection
SaveRicheditToAccess (DB.GetConnection, RE, 'SELECT Notes, ID FROM Actions WHERE ID='+IntToStr(ID));
LoadRicheditFromAccess (DB.GetConnection, RE, 'SELECT Notes, ID FROM Actions WHERE ID='+inttostr(id));
Function SaveRicheditToAccess (ADOC : TADOConnection ; RE : TRichEdit ; _Select : String) : Boolean;
var
ms: TMemoryStream;
ADO : TADODataset;
begin
ms := TMemoryStream.Create;
Try
Result := FALSE;
// save content to stream
RE.Lines.SaveToStream(ms);
ms.Seek(0, soFromBeginning) ;
ADO := TADODataset.Create (NIL);
ADO.Connection := ADOC;
ADO.CommandTExt := _SELECT;
ADO.Active := True;
ADO.Edit;
// Put stream content in blob field and post it to database
TBlobField(ADO.Fields[0]).LoadFromStream(ms);
ADO.Post;
Result := TRUE;
Finally
FreeAndNil (ms);
FreeAndNil (ADO);
End;
End;
Function LoadRicheditFromAccess (ADOC : TADOConnection ; RE : TRichEdit ; _Select : String) : Boolean; // Select doit renvoyer LE champ à modifier
var
ms: TMemoryStream;
ADO : TADODataset;
begin
ms := TMemoryStream.Create;
Try
Result := FALSE;
ADO := TADODataset.Create (NIL);
ADO.Connection := ADOC;
ADO.CommandTExt := _SELECT;
ADO.Active := True;
// Load rich edit content into stream
TBlobField(ADO.Fields[0]).SaveToStream(ms);
ms.Seek(0, soFromBeginning) ;
// Load rich edit content from stream to component
RE.Lines.LoadFromStream(MS);
Result := TRUE;
Finally
FreeAndNil (ADO);
FreeAndNil (Ms);
End;
End;