title: Importing data from SQL database in Excel 2003 VS Excel 2010 (VBA)
body: <p>So, my company finally upgraded to MS Office 2010.  Up until now I've been working in 2003. I am a SQL programmer, and I constantly create reports in Excel that pull data from our database. Most of the time, I will create macros that accept parameters that the users will type into specific cells, alter the query, and then refresh it according to the parameters. </p>
  
  <p>Here is a really simple example:  </p>
  
  <ol>
  <li>In Excel 2003 I would open a new workbook.</li>
  <li>Click on "Data" then "Import External Data" then "New Database Query".  </li>
  <li>It then prompts you to choose a data source, so I would select the database I wanted to query from (which had been set up with an ODBC connection already).  </li>
  <li><p>I then cancel out of the Query Wizard windows and then when I'm in the Microsoft Query Editor, I just enter in my query. </p>
  
  <ul>
  <li><p>For simplicity, I'll be selecting <code>*</code> from a table called <code>Agents</code> which is just a list of the agents that work for the company and their <code>EmployeeIds</code>.</p></li>
  <li><p><code>select * from Agents</code></p></li>
  </ul></li>
  <li><p>Then I "x" out of the query editor, and a box pops up called "Import Data" where it asks where you want to put the data: in an existing worksheet? a new worksheet? etc. I just have it return the data in the existing worksheet starting in Cell <code>A2</code></p></li>
  </ol>
  
  <p>So then I write this simple macro in the Visual Basic Editor in a module for the workbook:</p>
  
  <pre><code>Sub Refresh()
  
  Dim oQuery as QueryTable
  Dim oAgent as String
  
  set oQuery = Sheet1.QueryTables(1)
  
  oAgent = Sheet1.Range("A1")
  
  oQuery.CommandText = "select * from Agents where Agent = '"+oAgent+"'"
  oQuery.Refresh
  
  End Sub
  </code></pre>
  
  <p>I create a button that runs this macro and stick it in <code>B1</code>. So the user opens the report, types a name into <code>A1</code>, hits the button and that Agent and their Id comes up in the table below. Really simple right? But I can't get this to work in Excel 2010.</p>
  
  <p>Here are my steps and the error that follows:</p>
  
  <ol>
  <li>I open Excel 2010, and go to the "Data" tab.  </li>
  <li>Under the section "Get External Data" I click on "From Other Sources" and select "From Mircrosoft Query" from the drop down.</li>
  <li>Then the Choose Data Source box pops up and it is basically the exact same as steps 3,4 and 5 above.</li>
  </ol>
  
  <p>Then I write the same macro, create the button and assign it to the marco, but when I click the button I get the following error:</p>
  
  <pre><code>Run-time error '9':
  Subscript out of range
  </code></pre>
  
  <p>I hit debug and the debugger highlights this line</p>
  
  <pre><code>Set oQuery = Sheet1.QueryTables(1)
  </code></pre>
  
  <p>I tried making this line more specific like so:</p>
  
  <pre><code>Set oQuery = WorkBooks("Book 1").Sheets("Sheet 1").QueryTables(1)
  </code></pre>
  
  <p>But I just get the same error.</p>
  
  <p>So basically what I need to know is how to do this type of thing in Excel 2010. But here is an interesting note: if I create this report in Excel 2003, save it as a .xls, then open it in 2010, it will work. I can even save a copy as a .xlsm then open that and it will work with this same macro. It's only when I create the report in 2010 that I can't get it to work. It seems for some reason that it just can't find the query to alter its command text and then refresh. Please help, I've been stuck on this for days!</p>