safcftm117
MIS
I have a function which is querying an external database. It is called by another program which creates the where clause which is then passed dynamically into the SQL query.
The WHERE clause is passed in as a string parameter and the function works perfectly until the string exceeds a certain length (around 150 characters). When the WHERE clause exceeds this length the program fails with a Type Mismatch error.
My understanding is that a string can contain over 1bn characters so I'm unsure as to why this error is occurring.
My function is:
Sub Query_sub(wc)
'
'
'
'
Dim WhereClause
WhereClause = wc
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=e-Work Test;Description=Metastorm Data Source;UID=ework;APP=Microsoft Office 2003;;DATABASE=e-work;Trusted_Con" _
), Array("nection=Yes")), Destination:=Range("A1"))
Range("A1") = WhereClause
.CommandText = Array( _
"SELECT *" & Chr(13) & "" & Chr(10) & "FROM ""e-work"".dbo.Generic_Matrix Generic_Matrix" & Chr(13) & "" & Chr(10) & "" & WhereClause & "" & Chr(13) & "" & Chr(10) & "ORDER BY Sequence" _
)
.Name = "Query from e-Work Test"
.FieldNames = True
.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
End Sub
An example of the where clause is:
WHERE (BoM_Level IN ('0')) AND (Grp IN ('PHANTM')) AND (Issue_Loc IN ('3005')) AND (CC IN ('6382')) AND (Supplier IN ('PRD000051')) AND (Material IN ('080M50')) AND (Res_Code IN ('12A4','12A4')) AND (Unit_Code IN ('C0310','C0310')) AND (Model IN ('PC138US-8 WE SUB','PC160LC-8','PC190NLC-8','PC210LCD-8','PC210LCD-8')) AND (Commodity IN ('8539293000')) AND (Lev1_BoM IN ('X950138170')) AND (Imm_Lev_BoM IN ('0102481020')) AND (Cur IN ('JPY','JPY')) AND (Ctry IN ('IDN'))
The WHERE clause is passed in as a string parameter and the function works perfectly until the string exceeds a certain length (around 150 characters). When the WHERE clause exceeds this length the program fails with a Type Mismatch error.
My understanding is that a string can contain over 1bn characters so I'm unsure as to why this error is occurring.
My function is:
Sub Query_sub(wc)
'
'
'
'
Dim WhereClause
WhereClause = wc
ActiveWorkbook.Worksheets.Add
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DSN=e-Work Test;Description=Metastorm Data Source;UID=ework;APP=Microsoft Office 2003;;DATABASE=e-work;Trusted_Con" _
), Array("nection=Yes")), Destination:=Range("A1"))
Range("A1") = WhereClause
.CommandText = Array( _
"SELECT *" & Chr(13) & "" & Chr(10) & "FROM ""e-work"".dbo.Generic_Matrix Generic_Matrix" & Chr(13) & "" & Chr(10) & "" & WhereClause & "" & Chr(13) & "" & Chr(10) & "ORDER BY Sequence" _
)
.Name = "Query from e-Work Test"
.FieldNames = True
.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
End Sub
An example of the where clause is:
WHERE (BoM_Level IN ('0')) AND (Grp IN ('PHANTM')) AND (Issue_Loc IN ('3005')) AND (CC IN ('6382')) AND (Supplier IN ('PRD000051')) AND (Material IN ('080M50')) AND (Res_Code IN ('12A4','12A4')) AND (Unit_Code IN ('C0310','C0310')) AND (Model IN ('PC138US-8 WE SUB','PC160LC-8','PC190NLC-8','PC210LCD-8','PC210LCD-8')) AND (Commodity IN ('8539293000')) AND (Lev1_BoM IN ('X950138170')) AND (Imm_Lev_BoM IN ('0102481020')) AND (Cur IN ('JPY','JPY')) AND (Ctry IN ('IDN'))