How To Programmatically Copy the Current Record to a New Record (177304)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 3.0b
  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 5.0a
  • Microsoft Visual FoxPro for Windows 6.0
  • Microsoft Visual FoxPro for Macintosh, Professional Edition 3.0
  • Microsoft FoxPro for Windows 2.5
  • Microsoft FoxPro for Windows 2.5a
  • Microsoft FoxPro for Windows 2.5b
  • Microsoft FoxPro for Windows 2.6
  • Microsoft FoxPro for Windows 2.6a
  • Microsoft FoxPro for Macintosh 2.6a
  • Microsoft FoxPro for MS-DOS 2.0
  • Microsoft FoxPro for MS-DOS 2.5
  • Microsoft FoxPro for MS-DOS 2.5a
  • Microsoft FoxPro for MS-DOS 2.5b
  • Microsoft FoxPro for MS-DOS 2.6
  • Microsoft FoxPro for MS-DOS 2.6a
  • Microsoft FoxPro for UNIX 2.6

This article was previously published under Q177304

SUMMARY

This article illustrates how to programmatically copy the contents of a selected record into a new record in the same table.

MORE INFORMATION

The following code may be used in either a program (.prg) file or interactively in the FoxPro Command window.

NOTE: The code in Step 1 creates a new table used to provide sample data for the rest of the steps that illustrate how to copy an existing record. However, any table may be used provided that a duplicate record may be added and no index primary key is violated. Primary key indexes are only available in versions of Visual FoxPro.

  1. Copy the following code to a test program and run it to create Testcopy.dbf that contains five sample records:
          ***** Table setup for TEST of record copy *****
          CREATE TABLE 'testcopy.dbf' ;
          (FNAME C(12), LNAME C(10), SEQUENCE I(2))
    
          ***** Add five records for testing *****
          INSERT INTO testcopy (FNAME, LNAME, SEQUENCE) ;
          VALUES ('Rutherford B.', 'Hayes', 19)
          INSERT INTO testcopy (FNAME, LNAME, SEQUENCE) ;
          VALUES ('James A.', 'Garfield', 20)
          INSERT INTO testcopy (FNAME, LNAME, SEQUENCE) ;
          VALUES ('Chester A.', 'Arthur', 21)
          INSERT INTO testcopy (FNAME, LNAME, SEQUENCE) ;
          VALUES ('Grover', 'Cleveland', 22)
          INSERT INTO testcopy (FNAME, LNAME, SEQUENCE) ;
          VALUES ('Benjamin', 'Harrison', 23)
    							
  2. This step shows one way that may be used to locate the record to copy.
          LOCATE FOR sequence = 22
    						
  3. The following code copies and creates a duplicate record.

    NOTE: The MEMO clause used below is needed only if there is a memo field. The contents of a General field will not be copied.
          SCATTER MEMVAR MEMO   && Copies contents of current record to memory.
          APPEND BLANK          && Creates a new blank record.
          GATHER MEMVAR MEMO    && Copies contents from memory to new record.
    							
  4. BROWSE the table to view the records. Note that record number 6 is a duplicate of record number 4.

REFERENCES

For information about copying a general field, please see the following article in the Microsoft Knowledge Base:

113443 How to Copy a General Field from One Record to Another

(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Perry Newton, Microsoft Corporation

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto KB177304