Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Importing a csv file programmatically
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00179381
Message ID:
00179702
Views:
14
> I want to get a comma seperated file in a table. I don't know the lengths of the fields coming in so I can't predefine a table to put them into.

Donald,

I just tackled a similar problem. What I needed was a generic import module to allow new users to import data from their existing system into our application. I designed an import form that has a page frame with several tabs:

1. Source tab (select the name/type of input file, append/replace, etc.)
2. A preview grid (based on a temporary cursor)
3. Map definition
4. Error checking (pre-process each record prior to append to catch any errors)

In the load method of the form, I build a temporary cursor with the logic below:

LOCAL lnCount, lcFields
DIMENSION laAlpha(26)

STORE "A" TO laAlpha(1)
STORE "B" TO laAlpha(2)
STORE "C" TO laAlpha(3)
STORE "D" TO laAlpha(4)
STORE "E" TO laAlpha(5)
STORE "F" TO laAlpha(6)
STORE "G" TO laAlpha(7)
STORE "H" TO laAlpha(8)
STORE "I" TO laAlpha(9)
STORE "J" TO laAlpha(10)
STORE "K" TO laAlpha(11)
STORE "L" TO laAlpha(12)
STORE "M" TO laAlpha(13)
STORE "N" TO laAlpha(14)
STORE "O" TO laAlpha(15)
STORE "P" TO laAlpha(16)
STORE "Q" TO laAlpha(17)
STORE "R" TO laAlpha(18)
STORE "S" TO laAlpha(19)
STORE "T" TO laAlpha(20)
STORE "U" TO laAlpha(21)
STORE "V" TO laAlpha(22)
STORE "W" TO laAlpha(23)
STORE "X" TO laAlpha(24)
STORE "Y" TO laAlpha(25)
STORE "Z" TO laAlpha(26)

DODEFAULT()

lcFields = "("
* Spreadsheet columns A - Z
FOR lnCount = 1 TO 26
lcFields = lcFields + laAlpha(lncount) + " C(25), "
NEXT

* Spreadsheet columns AA - AZ
FOR lnCount = 1 TO 26
lcFields = lcFields + "A" + laAlpha(lncount) + " C(25), "
NEXT

* Spreadsheet columns BA - BZ
FOR lnCount = 1 TO 26
lcFields = lcFields + "B" + laAlpha(lncount) + " C(25), "
NEXT

* Spreadsheet columns CA - CZ
FOR lnCount = 1 TO 26
lcFields = lcFields + "C" + laAlpha(lncount) + " C(25), "
NEXT

* Spreadsheet columns DA - DZ, WIDTH set to 80 for MEMO columns (Notes)
FOR lnCount = 1 TO 26
lcFields = lcFields + "D" + laAlpha(lncount) + " C(80), "
NEXT

* Spreadsheet columns EA - EY
FOR lnCount = 1 TO 25
lcFields = lcFields + "E" + laAlpha(lncount) + " C(25), "
NEXT

* Spreadsheet column EZ
* add one more without the comma, but add the closing bracket
lcFields = lcFields + "E" + laAlpha(lncount) + " C(25))"

CREATE CURSOR csrImport &lcFields

This will handle 150 fields with most defined at length of 25 and some at length of 80. I then APPEND FROM the users file to the cursor. They can view it in the grid and then map the columns to the correct fields in our application.

Kind Regards,

Mat
Matthew Kennedy
Decision Support Technology Inc.

"I try to take one day at a time, but sometimes several days attack me all at once." ~ Ashleigh Brilliant

New Jersey Dot Net User Group
Previous
Reply
Map
View

Click here to load this message in the networking platform