Code:
Here is aroutine i use to copy tables. We have very large tables 400+ records, but it works for any. Please use only on sample data before you let it lose on any important stuff. Ron '----------------------------------------------- function copy_table as L(copy_from as C,copy_to as C, copy_data as L,copy_rules as L) '----------------------------------------------- on error goto error_handler dim quit_option as C '----------------------------------- ' validate file extensions '----------------------------------- if at(".dbf",copy_to) < 1 copy_to=trim(copy_to)+".dbf" end if if at(".dbf",copy_from) < 1 copy_from=trim(copy_from)+".dbf" end if '----------------------------------- ' check before overwriting file '----------------------------------- if file.exists(copy_to) quit_option=ui_get_radio("Destination file already exists.",1,"Overwrite","Quit") if quit_option ="Quit" then goto quit_out end if file.remove(copy_to) end if dim cnt as N '----------------------------------- ' open files '----------------------------------- tran=table.open(copy_from) num=tran.fields_get() f_handle=tran.field_get(1) f_name=f_handle.name_get() f_type=f_handle.type_get() f_width=f_handle.width_get() f_dwidth=f_handle.dec_get() table.create_begin(f_name,f_type,f_width,f_dwidth) for i = 2 to num f_handle=tran.field_get(i) f_name=f_handle.name_get() f_type=f_handle.type_get() f_width=f_handle.width_get() f_dwidth=f_handle.dec_get() Table.Field_add(f_name,f_type,f_width,f_dwidth) next Table.create_end(copy_to) 'mast.close() '----------------------------------- ' Check if ddd,ddm,ddx fields are to be copied '----------------------------------- if copy_rules copytolen=len(copy_to) copyfromlen=len(copy_from) cpyto_ddd=left(copy_to,copytolen-4)+".ddd" cpyto_ddm=left(copy_to,copytolen-4)+".ddm" cpyto_ddx=left(copy_to,copytolen-4)+".ddx" file.remove(cpyto_ddd) file.remove(cpyto_ddm) file.remove(cpyto_ddx) cpyfrom_ddd=left(copy_from,copyfromlen-4)+".ddd" cpyfrom_ddm=left(copy_from,copyfromlen-4)+".ddm" cpyfrom_ddx=left(copy_from,copyfromlen-4)+".ddx" file_copy(cpyfrom_ddd,cpyto_ddd) file_copy(cpyfrom_ddm,cpyto_ddm) file_copy(cpyfrom_ddx,cpyto_ddx) end if mast=table.open(copy_to) '----------------------------------- ' Want to copy data as well? '----------------------------------- rec=tran.records_get() reccnt=1 if copy_data =.t. cnt=tran.fields_get() tran.index_primary_put() tran.fetch_first() while .not. tran.fetch_eof() mast.enter_begin() for i = 1 to cnt statusbar.set_text("Record "+alltrim(str(reccnt))+" of "+alltrim(str(rec))) tval=tran.field_get(i) mval=mast.field_get(i) mval.value_put(tval.value_get()) next i mast.enter_end(.t.) tran.fetch_next() reccnt=reccnt+1 end while end if tran.close() mast.close() copy_table=.t. quit_out: statusbar.clear() end function error_handler: '---------------------------- ' It is neccessary to skip certain Alpha checks like calculated fields '---------------------------- 'err = error_code_get() 'msg = error_text_get(err) 'ui_msg_box("Error Handler", msg) resume next