Mega Code Archive

 
Categories / Delphi / Examples
 

Duplicating records

Need to make exact copies of records with some slight variations? This in itself wouldn't be a problem, but if there are many tables that need this functionality and they had a variety of types and structures, it can hit your nerves. The XBase solution would have been to store all the fields into memory variables, append a new record, then replace all the fields with the memory variables. As it turns out, that can be the solution to the problem with Delphi as well. The procedure introduced here, AppendCurrent, works this way: it creates a Variant Array it populates the array with each of field's values from the selected table it calls the Append method of the table it populates the table's fields with the values from the variant array it passes it back in the edit state ready for changing The following listing shows you the code. {************************************************ // procedure AppendCurrent // // Will append an exact copy of the current // record of the dataset that is passed into // the procedure and will return the dataset // in edit state with the record pointer on // the currently appended record. ************************************************} Procedure AppendCurrent(Dataset:Tdataset); VAR aField : Variant ; i : Integer ; BEGIN // create a variant Array aField := VarArrayCreate([0,DataSet.Fieldcount-1],VarVariant); // read values into the array for i := 0 to (DataSet.Fieldcount-1) do aField[i] := DataSet.fields[i].Value ; DataSet.Append ; // put array values into new the record for i := 0 to (DataSet.Fieldcount-1) do DataSet.fields[i].Value := aField[i] ; END;