Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating a file from an existing file

Status
Not open for further replies.

FOXPROG

Programmer
Jan 14, 2001
51
US
Is there an easy way to get the fields from a table and build a new table with just the fields the user wants included?
 

To copy only structure with specific fields to another table:

use oldtable
copy structure to newtable fields field1, field3, field5

To copy specific fields with data to another table:

use oldtable
copy fields field1, field3, field5 to newtable



 
The problem is that I will not know what fields until the user selects them. So I am having trouble creating the statement on the fly.
 

You can store each field the user selects to memory variables and build the statement using macro substitution.

Assume the table had 5 fields - Field1 to field5 and the user selects field1, field3 and field5.

IF mfield1 = .t.
mselfields = ' field1 '
ENDIF
IF mfield2 = .t.
mselfields = mselfields + iif(empty(mselfields),'field2 ',',field2 ')
ENDIF
IF mfield3 = .t.
mselfields = mselfields + iif(empty(mselfields),'field3 ',',field3 ')
ENDIF
IF mfield4 = .t.
mselfields = mselfields + iif(empty(mselfields),'field4 ',',field4 ')
ENDIF
IF mfield5 = .t.
mselfields = mselfields + iif(empty(mselfields),'field5 ',',field5 ')
ENDIF

****Error code - if no fields selected etc.

To copy only structure with specific fields to another table:

use oldtable
copy structure to newtable fields &mselfields

To copy specific fields with data to another table:

use oldtable
copy fields &mselfields to newtable




 
I doubt you really want to generate copies of tables with limited column list. You don't want to have redundancy all over the place. It depends of course, how temporarily the data will be used, but for temporary use or for view or print of data you'd use a cursor, not copy to another file.

for long term usage the fieldlist might be more important then the data copied, as it goes out of sync as soon as more data is inserted into the main table.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top