Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import Data from Excel to SQL Server using Ado

Status
Not open for further replies.

Dima09

Programmer
Mar 18, 2009
5
US
Hi All!

Have anyone ever import data from Excel to SQL Server using ado?

Here is the code that gives me an error: [OLE/DB Provider returned message: Unspecified error]

Dim lngRecsAff As Long
Dim strSQL As StringSet

Conn = New ADODB.Connection
Conn.Open DbConnection()

strSQL = "Select * INTO Purchaser FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', 'Data Source=C:\book.xlsx','SELECT * FROM [Sheet1$]')"

Conn.Execute strSQL, lngRecsAff, adExecuteNoRecords

What is missing here?
 
is the excel sheet open when you run this command?
 
I have a run time error for creating duplicate values. How do I have the ADO reveiw each record and if the ID already exists, continue or skip to add the others? Here is the code:

DoCmd.TransferSpreadsheet acLink, 8, "lnkImport", varFileName, True, "Sheet1!a1:g1000"

Set Conn = New ADODB.Connection
Conn.Open DbConnection()

strSQL = "INSERT INTO Purchaser ( LastName, PurchaserID ) SELECT Patient, Patient FROM lnkImport WHERE DOB Is Not Null"

Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top