title: Application Defined or Object Defined Error on QueryTable
body: <p>I'm trying to take data from a table in SQL and export it to a worksheet using VBA.</p>
  
  <p>I have been unable to figure out how to do this without creating a new data connection each time. </p>
  
  <p>Here is what I have been doing as of now:</p>
  
  <p>I am putting the SQL query into a cell and giving the variables names like '#ID' and "Date". In the macro, I am replacing the data within those variables with the actual information. That has not given me any problems thus far as the query runs without issue when using SQL. To export the data from SQL to my worksheet, I am using code that works in other workbooks; however when I run it in this workbook, I am getting the error "Application Defined or Object Defined Error". Here is the full macro: </p>
  
    
  
  <pre class="lang-vb prettyprint-override"><code>Sub CurrentBreakout()
  
  Dim SQLString As String
  Dim ID As String
  Dim Date As String
  Dim year As String
  Dim daycount As String
  
  SQLString = Range("CurrentBreakout") 'This is the range where the query is stored 
  ID = Range("ID")
  Date = Range("CurrMonth")
  year = Range("CurrYear")
  daycount = Range("CurrDayCount")
  
  SQLString = Replace(SQLString, "#ID", ID)
  SQLString = Replace(SQLString, "#Date", Date)
  SQLString = Replace(SQLString, "#CurrDayCount", daycount)
  SQLString = Replace(SQLString, "#CurrYear", year)
  
  
   With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
     "ODBC;DSN=____________;UID=________;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=________;DATABASE=_______;" _
          , Destination:=Range("CurrCon")).QueryTable
          .CommandText = SQLString
          .RowNumbers = False
          .FillAdjacentFormulas = False
          .PreserveFormatting = True
          .RefreshOnFileOpen = False
          .BackgroundQuery = True
          .RefreshStyle = xlInsertDeleteCells
          .SavePassword = False
          .SaveData = True
          .AdjustColumnWidth = True
          .RefreshPeriod = 0
          .PreserveColumnInfo = True
          .ListObject.DisplayName = "Table_CurrCon"
          .Refresh BackgroundQuery:=False
    End With
  
  End Sub
  </code></pre>
  
  <p>I am getting the error at ".Refresh BackgroundQuery:=False". This is probably not the best way to complete this task, but it is the only way I know.</p>