Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is that you're using VBA code IN the SQL statement. The following line will compile but fail upon execution:</p> <pre><code>Cn.Execute "INSERT INTO Project_Names IN 'D:\Tool_Database\Tool_Database.mdb' SELECT * FROM Worksheets("NewProj").Range("A2").Value" </code></pre> <p>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:</p> <pre><code>"SELECT * FROM [Sheet$]" </code></pre> <p>If you want a particular range then you'd try:</p> <pre><code>"SELECT * FROM [Sheet$A1:C20]" </code></pre> <p>There's a Microsoft article on this at: <a href="http://support.microsoft.com/kb/257819" rel="nofollow">http://support.microsoft.com/kb/257819</a> 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: <a href="http://technet.microsoft.com/en-us/library/ee692882.aspx" rel="nofollow">http://technet.microsoft.com/en-us/library/ee692882.aspx</a>.</p> <p>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.</p> <p>In fact, I just Googled it to double check and here's an article on the bug: <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;319998&amp;Product=xlw" rel="nofollow">http://support.microsoft.com/default.aspx?scid=kb;en-us;319998&amp;Product=xlw</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload