FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Copying and Pasting Records
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
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:
Code (fw): Select all Collapse
// 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.
Regards



G. N. Rao.

Hyderabad, India
Posts: 1598
Joined: Fri Oct 07, 2005 05:56 PM
Re: Copying and Pasting Records
Posted: Sun Feb 18, 2018 07:06 AM

Thanks, Mr.Rao.
Great and make use more easier.
Regards,
Dutch

Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 1487
Joined: Tue Jun 14, 2016 07:51 AM
Re: Copying and Pasting Records
Posted: Sun Feb 18, 2018 11:20 AM

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

Marc Venken

Using: FWH 23.08 with Harbour
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Copying and Pasting Records
Posted: Sun Feb 18, 2018 11:50 AM

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

Regards



G. N. Rao.

Hyderabad, India

Continue the discussion