As a continuation from my first thread located here:
http://www.mrexcel.com/forum/showthread.php?t=564802
My original issue was solved, but I ran into an unexpected problem which I do not know the answer to. I've done some searching, and there shouldn't be a limit on using an IN operator in SQL.
Code in original thread works, but only when using up to 14 values (in the range I specified). When I try to use this code on a range of values >=15, i get an type mismatch error?
I understand that generally a type mismatch error means it ran into an unexpected data format. I just don't understand what is wrong.
First, function that builds the string to be used with the SQL statement:
Secondly, the code:
Any help fixing this or telling me this is a limitation of how I am doing it would be really apprecaited!
http://www.mrexcel.com/forum/showthread.php?t=564802
My original issue was solved, but I ran into an unexpected problem which I do not know the answer to. I've done some searching, and there shouldn't be a limit on using an IN operator in SQL.
Code in original thread works, but only when using up to 14 values (in the range I specified). When I try to use this code on a range of values >=15, i get an type mismatch error?
I understand that generally a type mismatch error means it ran into an unexpected data format. I just don't understand what is wrong.
First, function that builds the string to be used with the SQL statement:
Code:
Function MakeSQL(rng As Range) As String
Dim oCell As Range
For Each oCell In rng.Cells
MakeSQL = MakeSQL & ", '" & oCell.Value & "'"
Next oCell
MakeSQL = "IN (" & Mid(MakeSQL, 3) & ")"
End Function
Secondly, the code:
Code:
Sub Macro1()
' Macro1 Macro
Dim PKMSID As String
Dim LastRow As Long
Dim wb As Workbook
Dim ws2 As Worksheet
Set wb = ThisWorkbook
Set ws2 = wb.Sheets("sheet2")
LastRow = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
PKMSID = InputBox("PKMS Login")
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DRIVER={iSeries Access ODBC Driver};UID=" & PKMSID & ";SIGNON=1;PKG=QGPL/DEFAULT(IBM),2,0,1,0,512;LANGUAGEID=ENU;DFTPKGLIB=QGPL;DBQ=Q" _
), Array("GPL WM0272PRDD;SYSTEM=PKMS2.US.CORP;")), Destination:= _
Range("$A$1")).QueryTable
.CommandText = Array( _
"SELECT PHPICK00.PHPKTN, PDPICK00.PDSTYL, PDPICK00.PDOPQT, PDPICK00.PDSTYD, PHPICK00.PHPSTF" & Chr(13) & "" & Chr(10) & "FROM CAPM01.WM0272PRDD." _
, _
"PDPICK00 PDPICK00, CAPM01.WM0272PRDD.PHPICK00 PHPICK00" & Chr(13) & "" & Chr(10) & "WHERE PDPICK00.PDPCTL = PHPICK00.PHPCTL AND ((PHPICK00.PHWH" _
, "SE='BNA') AND (PHPICK00.PHPSTF<='90') AND (PHPICK00.PHPKTN " & MakeSQL(ws2.Range("A1:A14")) & "))")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "TableDemand2"
.Refresh BackgroundQuery:=False
End With
End Sub
Any help fixing this or telling me this is a limitation of how I am doing it would be really apprecaited!