• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 112
  • Last Modified:

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
0
Alwyn Schutte
Asked:
Alwyn Schutte
  • 2
  • 2
  • 2
1 Solution
 
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Dale FyeCommented:
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
 
Alwyn SchutteAuthor Commented:
Thanks a lot for the article- very insightfull
0
 
Dale FyeCommented:
OP appears to have abandoned question after indicating the article posted by Dale Fye was insightful.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now