|
Question : Import a CSV file to multiple tables
|
|
I am needing some guidance on how to best handle the below situation.
Using EDI via CSV files, I am needing a means to do the following:
1. Import CSV files into an Access database - example data is below. 2. Move the data into different tables - based on the RECORD TYPE.
Each CSV file represents an order. Each CSV file is one order. The CSV files will reside in a common directory on a file server - could (and likely) will be the same server that the Access Database will reside. The length / size of the CSV file varies - there could be one item in the order or there could be 100 items. The records within the CSV file will vary - some orders will have all record types where some orders will not have every record type. The name of the CSV file is never the same. Each row in the CSV file represents a RECORD TYPE (see below example).
Below is an example of one of the CSV files.
"ORDER","5321839",01/21/2005,0.00,"",0.00,"","","","","",4730 "ORDER_EDI","PO",850,"004010",253,161,"0250","P","ZZ","CVS","CVS","12","8325959138","8325959138" "ORDER_CUSTOMER",1057,"CVS","","","","","","","" "ORDER_VENDOR",2084,"Ocusoft Inc","","","Dept #7","PO Box 4918","Houston","TX","77210-4918" "ORDER_SHIPPING","F","","","","","","","","","","","","","","","",0.00 "ORDER_ADDRESS","ST","Ship To","001786664O101","9","DUNS+4","","","","","","","","" "ORDER_TERMS","ITD","01","Basic",30,,0.00,0,,"3","Invoice Date","","",,0.00,0.00,0.00,0.00,0.00,"" "ORDER_DATE_TIME","DTM",02/11/2005,,"002","Delivery Requested","","" "ORDER_NOTE","BUYER NAME OR DEPARTMENT: TRACY BLAIS" "ORDER_NOTE","AMOUNT [BAP] REPORTED BY SENDER: 9600.00" "ORDER_REF_NUM","2981","VR","Vendor ID Number","" "ITEM",0,"","05042805355","UI","UPC Consumer Package Code (1-5-5)",34.0000,"CA",70.80,"","","","","" "ITEM_DETAILS",0.00,0.00,0.00,24,0.00,"",0,"" "ITEM_ID","05042805355","UI","UPC Consumer Package Code (1-5-5)" "ITEM_ID","247881","PI","Purchasers Item Code" "ITEM_NOTE","PERIOD AMOUNT [01] DISCRETE QUANTITY: 2407 ZZ" "ITEM",1,"","05042805356","UI","UPC Consumer Package Code (1-5-5)",26.0000,"CA",84.00,"","","","","" "ITEM_DETAILS",0.00,0.00,0.00,24,0.00,"",0,"" "ITEM_ID","05042805356","UI","UPC Consumer Package Code (1-5-5)" "ITEM_ID","247885","PI","Purchasers Item Code" "ITEM_NOTE","PERIOD AMOUNT [01] DISCRETE QUANTITY: 2184 ZZ" "ITEM",2,"","05042805357","UI","UPC Consumer Package Code (1-5-5)",12.0000,"CA",108.00,"","","","","" "ITEM_DETAILS",0.00,0.00,0.00,24,0.00,"",0,"" "ITEM_ID","05042805357","UI","UPC Consumer Package Code (1-5-5)" "ITEM_ID","247887","PI","Purchasers Item Code" "ITEM_NOTE","PERIOD AMOUNT [01] DISCRETE QUANTITY: 1296 ZZ" "ITEM",3,"","31571810330","UI","UPC Consumer Package Code (1-5-5)",26.0000,"CA",142.80,"","","","","" "ITEM_DETAILS",0.00,0.00,0.00,24,0.00,"",0,"" "ITEM_ID","31571810330","UI","UPC Consumer Package Code (1-5-5)" "ITEM_ID","235873","PI","Purchasers Item Code" "ITEM_NOTE","PERIOD AMOUNT [01] DISCRETE QUANTITY: 3713 ZZ" "ORDER_END",4
Greatly appreciate your input on how to best tackle this problem... Please ask questions - I am sure I didn't cover everything you will need to get a proper solution.
Saladart
|
|
Answer : Import a CSV file to multiple tables
|
|
OK. Your biggest problem here is that the lines in the CSV file have different numbers of commas, depending on what the line is for, ITEM_DETAILS, ORDER_SHIPPING etc. So you are going to have no end of trouble using TransferText or any other standard import method: the reason being that you cannot have a table with variable numbers of fields in each row.
What you are going to have to do is write a bunch of little routines that will process each of the different kinds of lines in the file. Then you will need a loop that will open the file and continue reading lines, one at a time until it hits the ORDER_END line.
You don't mention which version of Access you are using (or I did not see it), but assuming that it's Access 2000 or greater you will be able to use the Split() function to parse the contents of each line.
Basically your import routine will look like this
Sub ImportCSVOrder(strFilePath as string) ' strFilePath is the complete path to the file you want to read Dim fHandle as Long Dim strLine as String Dim strLineContents(25) ' I don't see more than 24 commans on one line Dim nOrderID as Long
fHandle - FreeFile Open strFilePath for Input as fHandle While Not EOF(fHandle) Line Input #fHandle, strLine strLineContents = Split(strLine,",") ' the array strLineContents now has each of the parts of the line in the elements of the array ' now check the first element of the array to see what kind of data we have Select Case strLineContents(0) Case """ORDER""" ' we are looking for the double quotes in the string as well nOrderID = AddNewOrder(strLineContents) Case """ORDER_EDI""" Call OrderEdi(strLineContents) ' etc. etc. Case """ORDER_END""" Call EndOfOrder(strLineContents) End Select Wend Close #fHandle End Sub
Each of the routines that handles the lines will take the array strLineCOntents as a parameter (a variant) and so each routine will 'know' what the various parts of the lines are for and do whatver is necessary to create records in tables, update the data etc.
e.g.
Function AddNewOrder(strLineContents As Variant) As Long ' creates a new order record, plugs some data into the record and returns the ' primary key value for the caller to use.
Dim nOrderID as Long nOrderID = Val(strLineContents(1)) ' pseudo code ' add a record to the orders table ' plug in the order ID to the table ' and the date etc. ' then return the primay key of the orders table records AddNewOrder = TheOrderTable!ID End Function
It may be a touch tedious to write all those routines for the different line type, but none of them should be particularly difficult. In doing it this way you have achieved what should always be the programmers goal: split the problem up into managable pieces. As long as each *type* of line consistently has the same number of data elements you should havel little trouble.
Lambert
|
|
|
|