Need a few pieces of VBA code to bulk insert a csv file into sql database

I have figured out a way to export data in csv format from an online database that we use for our member management system by using the API export call.
The problem arises when I import the data in Excel. Some of the fields delimit incorrectly when users have entered commas in the fields resulting in incorrect data in incorrect fields.
Another company that have done work for us on another project informed me that they do not save the file but export it directly to a database.
My question is as follows. I need code to write the csv file to a sql server express database.

My questions are inside the code as below
Sub Test_API_CSV_File()
'This is the part where the API csv file is downloaded- this part works already
Dim WinHttpReq As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.send

myURL = WinHttpReq.responseBody
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
'At this point I normally save the data as a csv file- this is where I want to bulk insert into a sql server
'Not sure if I can now open connection to sql server?
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "Provider=SQLOLEDB;Data Source=ALWYNS\SQLEXPRESS;Initial Catalog=Your membership;Integrated Security=SSPI;"
'Not sure if I can insert FROM WinHttpReq.responseBody? Will the code below automatically create the columns according to the names in the first row of the csv file? How will the column types be determined?

strSQLQuery = "BULK INSERT dbo.Customers " & _
"FROM WinHttpReq.responseBody " & _
"WITH (FIRSTROW = 1, FIELDTERMINATOR = ',', " & _
"ROWTERMINATOR = '\n', TABLOCK)"
With oComm
.ActiveConnection = locConnection
.CommandType = adCmdText
.CommandText = strSQLQuery
.Execute
Set conn = Nothing
End With
Else
End If
End Sub
Alwyn SchutteAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Just some comments..

First of all: Excel is not suitable for viewing CSV files due to its built-in "intelligence". Use a text editor like notepad or Notepad++ instead.

Some of the fields delimit incorrectly when users have entered commas in the fields resulting in incorrect data in incorrect fields.

This means, that your CSV export is broken. There is no general automatic way to tell what the correct content is. This would be only possible when having one text column.
0
Alwyn SchutteAuthor Commented:
Thanks for the comments.
Because another development used the same csv export through the API and inserted the fields correctly into a database I would still like to programmatically attempt to do it- instead of manually.
0
ste5anSenior DeveloperCommented:
Do you have acces with SSMS to your server? Then you should try it first there.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Dale FyeOwner, Developing Solutions LLCCommented:
You might want to take a look at my article on reading wide CSV files.  It relates to this issue in that I was having similar problems with the Bulk Insert process.  It includes  code for reading the text file and parsing the text and replacing the commas embedded in text fields with some other character.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alwyn SchutteAuthor Commented:
Thanks a lot for the article- very insightfull
0
Dale FyeOwner, Developing Solutions LLCCommented:
OP appears to have abandoned question after indicating the article posted by Dale Fye was insightful.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.