* [URL unfurl="true"]http://www.tek-tips.com/viewthread.cfm?page=1&qid=951781#postform[/URL]
CLOSE DATA ALL
LOCAL cMyPath
cMyPath = "C:\Documents and Settings\Darrell\My Documents\"
CREATE CURSOR Donors (Donor c(64), DonorId i)
CREATE CURSOR Donations ( ;
DonorId i, ;
Envelope N(6,0), ;
DonationDate d, ;
Donation N(8,2), ;
CheckNum c(10) ;
)
=GetDonorData(cMyPath,"Testdonor.xls")
PROCEDURE GetDonorData
LPARAM cSrcPath, cXlsName
* cSrcPath - Path to spreadsheet
* cXlsName - Name of spreadsheet
* Perform error checking on parameters here...
* ....
LOCAL nWkArea, cTblName
nWkArea = SELECT() && Save current work area
cTblName = JUSTSTEM(cXlsName)
* It's assumed that the fields will be of the following types:
* Column Name Description Assumed Type
* A Envelope N
* B Donor / Date C
* C Donation N or Y(currency)
* D Check Number C
CREATE CURSOR DonorImport ( ;
Envelope N(6,0), ;
Donor_Date c(64), ;
Donation N(8,2), ;
CheckNum c(10) ;
)
APPEND FROM (cSrcPath+cXlsName) TYPE XLS
LOCAL bEnvChange, nCurntEnv, cCurntDonor
LOCAL dDonationDate, nDonation, cCheckNum
LOCAL nDonorId
nDonorId = 0
SCAN
bEnvChange = DonorImport.Envelope <> 0
nCurntEnv = IIF(bEnvChange,DonorImport.Envelope,nCurntEnv)
IF bEnvChange
cCurntDonor = XLSCharCleanse(DonorImport.Donor_Date)
nDonorId = nDonorId + 1
ELSE
dDonationDate = CTOD(XLSCharDateCleanse(DonorImport.Donor_Date))
ENDIF
nDonation = DonorImport.Donation
cCheckNum = XLSCharNumberCleanse(DonorImport.CheckNum)
* Write record to Parent and child table here.
* Or you might perform some type of lookup...
IF bEnvChange
INSERT INTO Donors (Donor, DonorID) VALUES (cCurntDonor, nDonorId)
ELSE
INSERT INTO Donations (DonorId, Envelope, DonationDate, Donation, CheckNum) VALUES ;
(nDonorId, nCurntEnv, dDonationDate, nDonation, cCheckNum)
ENDIF
ENDSCAN
USE IN "DonorImport"
SELECT (nWkArea)
ENDPROC
* Spread sheets can add some funky chars to character columns
* that won't get stripped by alltrim(), so clean them out using
* something similiar to these functions...
FUNCTION XLSCharDateCleanse
LPARAM vArg
vArg = ALLT(vArg)
LOCAL nLen, i, cNextChar, cReturn
cReturn = ""
nLen = LEN(vArg)
FOR i = 1 TO nLen
cNextChar = SUBSTR(vArg,i,1)
IF cNextChar $ "1234567890/"
cReturn = cReturn + cNextChar
ENDIF
NEXT
RETURN cReturn
ENDFUNC
FUNCTION XLSCharCleanse
LPARAM vArg
vArg = ALLT(vArg)
LOCAL nLen, i, cNextChar, cReturn
cReturn = ""
nLen = LEN(vArg)
FOR i = 1 TO nLen
cNextChar = SUBSTR(vArg,i,1)
* Allow punctuation in text - oh well...
IF BETWEEN(cNextChar,CHR(32),CHR(126))
cReturn = cReturn + cNextChar
ENDIF
NEXT
RETURN cReturn
ENDFUNC
FUNCTION XLSCharNumberCleanse
LPARAM vArg
vArg = ALLT(vArg)
LOCAL nLen, i, cNextChar, cReturn
cReturn = ""
nLen = LEN(vArg)
FOR i = 1 TO nLen
cNextChar = SUBSTR(vArg,i,1)
* Allow only numbers
IF ISDIGIT(cNextChar)
cReturn = cReturn + cNextChar
ENDIF
NEXT
RETURN cReturn
ENDFUNC