*=============================================================== * Program: Table Importer * Purpose: generic import code * Author: Michael Gass * Email: support@instructionalsoftware.us * Copyright: Offered as public domain on the UT * Last revision: 12/06/2006 * Purpose * The number and names of import and current tables fields must match * Determine which fields need to be added to the imported tables * Add fields to import tables so number, name, type, and size match * fields already in the DBF * Then move data from importable table to current table * Parameters: importdir = location of table to import * filename = name of current table * importname = name of table to import * Variables: imp = number of fields in import table * cur = number of fields in current table * afieldname = field name from array * afieldtype = field type from array * afieldsize = field size from array * zfieldtoadd = combination of data to add using ALTER TABLE * zexecute = code to execute to import data from import table * aMyStruc = array containing structure of current TABLE *=============================================================== LPARAMETERS importdir, curtable, oldtable LOCAL old,cur,afieldname,afieldtype,afieldsize,zfieldtoadd,impadd LOCAL zoldtable,zcurtable,impadd *********************************** ** determine whether number of fields in importfile = number of fields ** in currentfile if current table is larger than import table, add fields ** to import table from current table if import table is larger than current ** table, warn user and exit if fields in importfile and currentfile are the ** same are, just import the data. impadd = importdir + oldtable + '.DBF' IF ! FILE(impadd) && If prior code doesn't find tables to be imported THIS.omessage.INFORM('Table ' + impadd + ' does not exits. ' + ; 'Continuing to import other tables.') RETURN ELSE IF ! USED(curtable) SELECT 0 USE (curtable) SHARED ENDIF IF ! USED(oldtable) SELECT 0 USE (impadd) SHARED ENDIF old = FCOUNT(oldtable) cur = FCOUNT(curtable) zoldtable = "'" + oldtable + "'" zcurtable = "'" + curtable + "'" DO CASE CASE old < cur ** Remove records from current table IF USED(curtable) SELECT (curtable) USE ENDIF SET SAFETY OFF USE (curtable) IN 0 EXCLUSIVE SELECT (curtable) IF THIS.Zappend = .F. ZAP ENDIF USED(curtable) SET SAFETY ON ** Open old table exclusively for adding fields IF USED(oldtable) SELECT (oldtable) USE ENDIF USED(oldtable) SELECT 0 USE (impadd) EXCLUSIVE SELECT (curtable) = AFIELDS(aMyStruc, (curtable)) && structure array DO WHILE old < cur old = old + 1 afieldname = aMyStruc(old,1) afieldtype = aMyStruc(old,2) afieldsize = aMyStruc(old,3) afielddeci = aMyStruc(old,4) IF afielddeci # 0 zfieldtoadd = afieldname + [ ] + afieldtype + [(] + ; TRANSFORM(afieldsize) + [,]+TRANSFORM(afielddeci) + [)] ELSE zfieldtoadd = afieldname + [ ] + afieldtype + [(] + ; TRANSFORM(afieldsize) + [)] ENDIF SELECT (oldtable) ALTER TABLE (oldtable) ADD COLUMN &zfieldtoadd ENDDO SELECT (curtable) INSERT INTO (curtable) SELECT * FROM (oldtable) CASE old > cur ** Notify user number of import fields exceeds number of fields ** in current table and the process can not continue. THISFORM.omessage.INFORM('The number of import fields ' + ; 'exceeds number of fields in '+ curtable +'. Import failed.' + ; ' Data may have been corrupted. Import backup.') RETURN CASE old = cur ** Removes records from current table then imports data from ** import table to current table IF USED(curtable) SELECT (curtable) USE ENDIF SET SAFETY OFF USE (curtable) IN 0 EXCLUSIVE SELECT (curtable) IF THIS.Zappend = .F. ZAP ENDIF USED(curtable) SET SAFETY ON SELECT (curtable) INSERT INTO (curtable) SELECT * FROM (oldtable) ENDCASE THIS.cmdatamanager.updateall((curtable)) SELECT (curtable) USE USE (curtable) IN 0 SHARED SELECT (oldtable) USE ENDIF