Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am writing a macro that will run from an excel workbook and export out one of the ListObjects to my access file.

I have this VBA code at the moment:

Sub AccessImport()

' Create connection
Dim Path As String
Dim conn As Object
Dim connectstr As String
Dim recordset As Object
Dim strSQL As String

Path = "P:\CALIBRE-YSP Implementation\11 General\CDM Database"

Set conn = CreateObject("ADODB.Connection")

connectstr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & Path & "\CDM_Database_DataOnly.mdb;"

strSQL = "SELECT * INTO DeliverablesLivesheet FROM [Excel 8.0;HDR=YES;DATABASE=P:\CALIBRE-YSP Implementation\11 General\CDM Database\CDMv2 - Development.xlsm].[DeliverablesImport];"


conn.Open connectstr
Set recordset = conn.Execute(strSQL)

recordset.Close
Set recordset = Nothing

conn.Close
Set conn = Nothing

End Sub

It is supposed to take a ListObject from Excel then transfer the data to a new access table, overwriting the old one.

It throws an error when it tries to execute the SQL:

strSQL = "SELECT * INTO DeliverablesLivesheet FROM [Excel 8.0;HDR=YES;DATABASE=P:\CALIBRE-YSP Implementation\11 General\CDM Database\CDMv2 - Development.xlsm].[DeliverablesImport];"

So I guess something is wrong with the SQL?

I can't seem to work it out though

Thanks

share|improve this question
It seems odd that strSQL = "..." would throw an error. At that point, the string hasn't been submitted to the db engine, so it wouldn't matter whether or not it's a valid SQL statement. As long as it's any valid string, it shouldn't throw an error. What is the full error message? – HansUp Apr 3 at 5:22
Whoops what I meant was it throws an error on execute cause of the SQL string. I'll amend the Q – KillerSnail Apr 3 at 5:41

2 Answers

strSQL="insert into DeliverablesLivesheet Select * FROM [Excel 8.0;HDR=YES;DATABASE=P:\CALIBRE-YSP Implementation\11 General\CDM Database\CDMv2 - Development.xlsm].[DeliverablesImport];"
share|improve this answer
I want to overwrite the existing table. This string also throws an error on: Select * FROM [Excel 8.0;HDR=YES;DATABASE=P:\CALIBRE-YSP Implementation\11 General\CDM Database\CDMv2 - Development.xlsm].[DeliverablesImport];) – KillerSnail Apr 3 at 5:53
If u wanna overwrite the table first delete the values from table and then insert this query.. – user2230817 Apr 3 at 11:13
try the statement nw... – user2230817 Apr 3 at 11:22
The DB is not letting me write the data. I wrote a string to clear the existing data. that works. I get runtime error '-2147217911 (80040e09)' i tried to add the code: conn.Mode = adModeReadWrite but that doesn't seem to do anything. – KillerSnail Apr 4 at 6:17

Ok I didn't realise that the connection string works like:

[Excel 8.0;HDR=YES;DATABASE={address of file}].[{sheet name}]

I had the name of the ListObject in there.

Now I get stuck with "Database or object is read only.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.