Reading from a csv, writing into a db

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
ZeitenWanderer
Pro Scripter
Posts: 68
Joined: Wed Dec 07, 2005 7:13 am

Reading from a csv, writing into a db

Post by ZeitenWanderer » Mon Mar 24, 2014 8:27 pm

I receive a csv-file from Amazon.de, which lists all the orders I have received within a day or so:

payments-status order-id order-item-id payments-date payments-transaction-id item-name listing-id sku price shipping-fee quantity-purchased total-price purchase-date batch-id buyer-email buyer-name recipient-name ship-address-1 ship-address-2 ship-city ship-state ship-zip ship-country special-comments upc ship-method sales-channel VAT
304-7835558-4072357 03092879065371 24.03.2014 11:01:59 Europe/Berlin Hauptkatalog der Original Ersatzteile für McCormick - Nr. 6 H. Grasmäher, Pfe... 0323OO8PU8D TEC11810N 20 3 1 23 24.03.2014 11:01:59 Europe/Berlin [email protected] FirstName LastName FirstNameAgain LastNameAgain Street StreetNumber City Zip DE standard


Now I would like to read this file and afterwards write the values into my Db-file (Access). Beginner that I am, I checked the forum here and found a script, which allows a user to enter an ID-value from the local database. Afterwards it returns which Company name belongs too it. Now the user could change that name and this change would be written into the local db. I made it run for me (cost me two hours ...:-))


VBSTART
Function GetTitelName(ArtID)

Dim SQLString

set MyDB = CreateObject("ADODB.Connection")
MyDB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DARP_DB.mdb"

SQLString = "select * from tblBuchdaten where ArticleID = '" & ArtID & "'"

set rsTitel = MyDB.Execute(SQLString)

If Not rsTitel.EOF then
GetTitelName = rsTitel.Fields("Titel")
Else
GetTitelName = "Not Found"
End if

MyDB.Close

End Function

Sub ChangeName(ArtID,NewName)


Dim SQLString

set MyDB = CreateObject("ADODB.Connection")
MyDB.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=DARP_DB.mdb"

SQLString = "Update tblBuchdaten Set Titel = '" & NewName & "' Where (ArticleID = '" & ArtID & "')"
set rsTitel = MyDB.Execute(SQLString)

MyDB.Close

End Sub

VBEND

Input>ArtID,Bitte Artikelnummer eingeben:
VBEval>GetTitelName("%ArtID%"),Titel
MessageModal>Der Artikel ist : %CRLF% %CRLF% %Titel%

Input>NewName,Enter New Name:,Titel
VBRun>ChangeName,%ArtID%,%NewName%


The question: I want to automate the process. Instead of entering a single ID, I need to read all the IDs listed in the csv-file (that is the row named SKU in the csv-file in the beginning) and lower the quantity by 1 in my local database. Any hint appreciated.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Mar 25, 2014 6:50 am

I would use CSVFileToArray or DBQuery to read the CSV file into an array. Then loop through it. For each value use DBQuery with an INSERT query to write the values to your database.

The VBScript code you posted above is not needed.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts