I am using the following function to perform a Teradata query from EXCEL 2007:
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=" & UID & ";PWD=" & PWD & ";", 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
This query puts its results in a region whose "North West" corner is some cell specified by Rng, and determines how many records were returned by getting the row number of the last record in column number Location, which is what the function LastInCol (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.
The text of the Teradata query itself is contained in string var. 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 var exceed the EXCEL VBA limit of 32767 characters.
I really am not sure what limits are being exceeded here. Certainly that of the length of the variable var, but there's also what the QueryTable parameter .CommandText can contain.
How can I get around these limits?