Copying and Pasting Records
Posted: Sat Feb 17, 2018 10:59 pm
There can be occassions when we need to copy a record, either partly or fully, from a table and paste the values in some other record of the same table or another table or append the values as a new record. It is also possible that source and destination databases can be different, eg. copy some values from a DBF and paste (or insert) into a table in MySql database. The situations can vary and different situations require coding specific to those requirements.
FWH makes any such job very easy by providing these functions:
FW_CopyRecord( [uSource = Alias()], [cFieldlist = All] ) --> hRec (hash)
FW_PasteRecrod( [uDest = Alias()], [hRec], [lAppend = .f.] ) --> lSuccess
FW_EditHash( hHash )
uSource and uDest can be Alias(), RecordSet, RowSet, Qry of any Database or even an active XBrowse. If omitted, defauts to current Alias().
FW_CopyRecord() returns a Hash with fieldnames and value. FWH also retains a copy of the hash in its internal memory. If the parameter hRec is omitted in the next call to FW_PasteRecord(), the hash in the memory is used.
When pasting the values are pasted to the corresponding fields in the destination. So, it is not necessary that the fields should be in the same order in the source and destination.
AutoIncrement and DateStamp fields: Paste operation provides safety by not over-writing auto-increment and datestamp fields.
However it is the reponsibility of the programmer to ensure that the source and destination fields have the same data type and widths and also values pasted respect the data constraints of the destination table (eg: Unique values should not be repeated). For this purpose, the programmer can change the values of some fields or even the field names after copying by modifying the Hash.
If required, programmer can use the handy utility FW_EditHash( hHash ) to let the user edit the values before pasting.
Some Examples:
We hope these three functions cover every possible requirement for copying and pasting records from one table to the same or another table of any database.
FWH makes any such job very easy by providing these functions:
FW_CopyRecord( [uSource = Alias()], [cFieldlist = All] ) --> hRec (hash)
FW_PasteRecrod( [uDest = Alias()], [hRec], [lAppend = .f.] ) --> lSuccess
FW_EditHash( hHash )
uSource and uDest can be Alias(), RecordSet, RowSet, Qry of any Database or even an active XBrowse. If omitted, defauts to current Alias().
FW_CopyRecord() returns a Hash with fieldnames and value. FWH also retains a copy of the hash in its internal memory. If the parameter hRec is omitted in the next call to FW_PasteRecord(), the hash in the memory is used.
When pasting the values are pasted to the corresponding fields in the destination. So, it is not necessary that the fields should be in the same order in the source and destination.
AutoIncrement and DateStamp fields: Paste operation provides safety by not over-writing auto-increment and datestamp fields.
However it is the reponsibility of the programmer to ensure that the source and destination fields have the same data type and widths and also values pasted respect the data constraints of the destination table (eg: Unique values should not be repeated). For this purpose, the programmer can change the values of some fields or even the field names after copying by modifying the Hash.
If required, programmer can use the handy utility FW_EditHash( hHash ) to let the user edit the values before pasting.
Some Examples:
- Code: Select all Expand view
// Same DBF
FW_CopyRecord()
DBGOTO( 100 )
FW_PasteRecord()
// or
FW_PasteRecord( nil, nil, .T. )
//-------------------
hRec := FW_CopyRecord( oBrw, "ID,NAME,AGE" )
hRec[ "ID" ] := "0234"
FW_PasteRecord( oRecordSet, hRec, .T. )
We hope these three functions cover every possible requirement for copying and pasting records from one table to the same or another table of any database.