Sometime we need to have record insert to different kind of tables but all those tables has almost same types of field structure, may be there are some fields are unique, for Ex. we have a Parent Table which has the same field structure in their Archive table except one new field i.e. Version No. (sometime it does not).So now how to deal to transfer the data between these two tables without using or declaring record variable. Here the magic with RecrodRef and FieldRef datatypes
Documentation
RecordRef
Lets start: In this section I am going to first Filter the Archive data if we have the same version already there.
// Declaring Two Variables. TargetRecRef: RecordRef; SourceRecRef: RecordRe; ArchiveVersion: Integer; ArchiveVersion := 1; // Here are giving the Target table ref. in this case its Archive Table. TargetRecRef.Open(Database::ArchiveTable); if TargetRecRef.FindLast() then begin // Find from the available records. // Version No. field can be your finding values to increase the new version. fldRef := TargetRecRef.Field(VersionFieldNo); ArchiveVersion := fldRef.Value; ArchiveVersion += 1; fldRef.Value := ArchiveVersion; TargetRecRef.Modify(); end; // Now we are setting up the Original table to update // the Version no. from the Table Table which is already modified in above case. sourceref.gettable(Database::originalTable); // "Version No. field from Original table to have the ref. of Archive table. fldRef := SourceRecRef.Field(VersionNo); fldRef.Value := ArchiveVersion; SourceRecRef.Modify(); Now Time to transfer other fields as well which does not exist in either table (if any) without using TRANSFERFIELDS method Clear(TargetRecRef); Clear(fldRef); TargetRecRef.Open(ArchiveHeader); if SourceRecRef.FindSet() then repeat for i := 1 to SourceRecRef.FieldCount do begin // Will loop through all the fields count which are available. fldRef := TargetRecRef.FieldIndex(i); if SourceRecRef.FieldExist(fldRef.number) then // this statement will skip those records which are not exist in Source Table. fldRef.Value := SourceRecRef.Field(fldRef.Number).Value; end; TargetRecRef.Insert(); // Finally Inserting the Target Table (Archive Table. until SourceRecRef.Next() = 0;