1

I am trying to use the INSERT INTO command to write data into an Access 2007 file from a excel 2010 file. The data is contained in the "NewProj" worksheet in the Tool_Selector.xlsm excel file and it needs to be written to the "Tool_Database.mdb" file but unfortunately I have received several different errors.. This is what I currently have

Sub AddData()
  Dim Cn As ADODB.Connection
  Set Cn = New ADODB.Connection

  Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Tools_Dev\_Tool_Selector\Tool_Selector.xlsm;Extended Properties=Excel 8.0;" _
        & "Persist Security Info=False"

  Cn.Execute "INSERT INTO Project_Names IN 'D:\Tool_Database\Tool_Database.mdb' SELECT * FROM Worksheets("NewProj").Range("A2").Value"

  Cn.Close
  Set Cn = Nothing
End Sub

I got the code from this thread: Insert Data from Excel into Access using VBA

I just need to transfer the value of individual cells.

Thanks.

13
  • I know that this Cn.Execute "INSERT INTO Project_Names IN 'D:\Tool_Database\Tool_Database.mdb' SELECT * FROM Worksheets("NewProj").Range("A2").Value" is where is breaks... How can I select a specific cell or row of cells? Commented Feb 1, 2012 at 21:55
  • 2
    See if this helps: stackoverflow.com/questions/5122582/… Commented Feb 1, 2012 at 21:57
  • phenomenal, you're a good person. Commented Feb 1, 2012 at 22:04
  • When I run that code, I get runtime error 424 Object Required On this line: strSQL = "INSERT INTO Project_Names (Proj_Num) " & "VALUES (" & NewProj.[A1] & ")" Commented Feb 1, 2012 at 22:16
  • 1
    Try ?worksheets("newproj").[a1] I do not think you can refer to a worksheet by name as you have done. If that works, you can use that in oace of the existing reference, without the question mark, of course. BTW in Stackoverflow, if you prefix a person's name with @, they will get a message, otherwise they may miss it, unless they are the author of the post. Commented Feb 2, 2012 at 18:21

2 Answers 2

2

The problem is that you're using VBA code IN the SQL statement. The following line will compile but fail upon execution:

Cn.Execute "INSERT INTO Project_Names IN 'D:\Tool_Database\Tool_Database.mdb' SELECT * FROM Worksheets("NewProj").Range("A2").Value"

It fails because you're still in the SELECT statement when you make use the VBA functions of Worksheets(), .Range, and .Value. IIRC, you should be able to use SQL syntax to select a particular range from a sheet. The proper syntax for that should be:

"SELECT * FROM [Sheet$]"

If you want a particular range then you'd try:

"SELECT * FROM [Sheet$A1:C20]"

There's a Microsoft article on this at: http://support.microsoft.com/kb/257819 if you're looking for more information on using ADO with Excel. The Scripting Guys also wrote a decent article that helped me understand it a few years back: http://technet.microsoft.com/en-us/library/ee692882.aspx.

Hopefully that helps you solve your problem. However, I will leave you with one word of warning. I remember running into some really weird issues when querying an already open Excel spreadsheet when I was first using this code. If I was querying an Excel spreadsheet that was already open then I would run into an issue where memory would be leaked and Excel would eventually run out of memory to use. The only way to solve the problem was to close Excel entirely. It also didn't matter whether the ADO references were properly closed/cleared or not.

In fact, I just Googled it to double check and here's an article on the bug: http://support.microsoft.com/default.aspx?scid=kb;en-us;319998&Product=xlw.

Sign up to request clarification or add additional context in comments.

Comments

0

It sounds similar to a problem I once had when upgrading from office 2003 to 2007.

My solution was changing the provider in the connection string. Since you're using a modern office version I think it should be:

Provider=Microsoft.Ace.OLEDB.12.0;

You might want to look into that Extended property as well. I know that Excel 8.0 is a Excel '97 file. And Excel 2010 is 14.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.