Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy and paste

Status
Not open for further replies.

filipe26

Programmer
Mar 17, 2003
152
PT
Hi i have a form with many db fields like dbtext,dbmemo,etc linking to a dataset.What i want to do is with two buttons,one copies the content of all fields and another pastes the content when the dataset is in edit mode.To the paste button i already have the command setfields(array) but what about copy all fields into an array to put in set fields?How do i do this?Thanks.
 
Hi,
generally speaking is not a good idea to work with dbEdits.
You should work directly with the associated field.

So:

//Save The record
// Somewhere (Form1.OnCreate) => SL:=TStringList.Create;
For i := 0 To Table1.Fields.Count-1 Do
Begin
SL.Add(Table1.Fields[ i].AsString);
End;

//Get The record
Table1.Append;
For i := 0 To Table1.Fields.Count-1 Do
Begin
Table1.Fields[ i].AsString:=SL[ i];
End;
Table1.Post;

// Somewhere (Form1.OnDestroy) => SL.Free;

Ciao,
Geppo Darkson.
 
The only thing I might add to Geppo's answer is to check to see if the column is NULL as opposed to holding an empty string. This is a subtle difference, but it can be important.

If you are sure that you will never intentionally have an empty string as data, then you can just change it to:

Code:
Table1.Append;
for Counter := 0 To Table1.Fields.Count - 1 do
begin
   if ThisStringList.Strings[Counter] <> '' then
       Table1.Fields[Counter].AsString := ThisStringList.Strings[Counter];
end;

If you might need to preserve an empty string as actual source data, then you might use Boolean(ThisStringList.Objects[Counter]) as a flag for a NULL column.


Cheers
 
Hi,
Just to be complete,
you should also check the type of the field, because there are some fields (like Blobs or Memos) that cannot be used with Field.AsString, so you may need to make some extra work to manage the information thath belongs to this fields.

Ciao,
Geppo Darkson.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top