title: Trouble with text of very long Teradata query
body: <p>I am using the following function to perform a Teradata query from EXCEL 2007:</p>
  
  <pre><code>    Function Get_Query_Results(Rng As Range, Location As String, var As String, UID As String, PWD As String) As Long
      On Error GoTo TroubleWithTeradata
          Rng.Select
          With ActiveSheet.ListObjects.Add(SourceType:=0, Source:="ODBC;DSN=Server12;UID=" &amp; UID &amp; ";PWD=" &amp; PWD &amp; ";", Destination:=Rng).QueryTable
              .CommandText = var
              .RowNumbers = False
              .FillAdjacentFormulas = False
              .PreserveFormatting = True
              .RefreshOnFileOpen = False
              .BackgroundQuery = True
              .RefreshStyle = xlInsertDeleteCells
              .SavePassword = False
              .SaveData = True
              .AdjustColumnWidth = True
              .RefreshPeriod = 0
              .PreserveColumnInfo = True
              .Refresh BackgroundQuery:=False
          End With
          Get_Query_Results = LastInCol(Columns(Location)) - 1
          Exit Function
      TroubleWithTeradata:
          Get_Query_Results = -1
      End Function
  </code></pre>
  
  <p>This query puts its results in a region whose "North West" corner is some cell specified by <em>Rng</em>, and determines how many records were returned by getting the row number of the last record in column number <em>Location</em>, which is what the function <em>LastInCol</em> (not listed here) returns. If the query doesn't fail but returns no records (i.e., only a header row), the number of records returned is 0. If the function does indeed fail, the number of records returned is -1.</p>
  
  <p>The text of the Teradata query itself is contained in string <em>var</em>. And herein lies the problem. I have been using this function successfully for years. But  now I need to build a new query that makes the variable <em>var</em> exceed the EXCEL VBA limit of 32767 characters.</p>
  
  <p>I really am not sure what limits are being exceeded here. Certainly that of the length of the variable <em>var</em>, but there's also what the QueryTable parameter <em>.CommandText</em> can contain.</p>
  
  <p>How can I get around these limits?</p>