Page 1 of 1

Copying and Pasting Records

PostPosted: Sat Feb 17, 2018 10:59 pm
by nageswaragunupudi
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:
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.

Re: Copying and Pasting Records

PostPosted: Sun Feb 18, 2018 7:06 am
by dutch
Thanks, Mr.Rao.
Great and make use more easier.
Regards,
Dutch

Re: Copying and Pasting Records

PostPosted: Sun Feb 18, 2018 11:20 am
by Marc Venken
Thanks.

If we want to use a condition like ex. (copy all records from source to target) where

State = "US"

Is there a small sample for dbf and xBrowse (Xbrowse also if there is a selection done)

I would do something like this, but often you guys do it in 1 or 2 lines ))

use cust NEW
copy structure to temp
use temp new

do while !cust->(eof())
if cust->state = "US"
hRec := FW_CopyRecord( cust , "ID,NAME,AGE" )
hRec[ "ID" ] := "0234"
FW_PasteRecord( temp, hRec, .T. )
endif
cust->(dbskip())
enddo
close all

Re: Copying and Pasting Records

PostPosted: Sun Feb 18, 2018 11:50 am
by nageswaragunupudi
Mr Marc

That is a separate topic and FWH provides powerful functions to do what you want.

Please see documentation on
FW_AdoImportFromDBF()
and
FWMARIADB
oCn:ImportFromDBF()

These functions provide for all possible options and requirements