Arsenal176
MIS
Hi
I am sorry , this may be a very stupid question , but I have no VBA knowldge . I have spent the last two weeks developing an excel based application and that is it. I have to query using any unique value in the list aa2:aa58
The values in this list may not be unique . If I just search using 4 values in the select its ok
but I get an invalid type eror if I search will all the values I want
I know this is probably bad code but here is how I do it
Dim a As Variant
Dim b As Variant
Dim c As Variant
etc for all values
a = Range("aa2").Value
b = Range("aa3").Value
c = Range("aa4").Value
d = Range("aa5").Value
etc
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DRIVER=SQL Server;SERVER=***;UID=***;APP=Microsoft® Query;WSID=***;DATABASE=mydb;Trusted_Connec" _
), Array("tion=Yes")), Destination:=Range("AC1"))
.Sql = Array( _
"select * FROM mydb.dbo.table1 table1 WHERE table1.ID in (" & a & ", " & b & ", " & c & ", " & d & ",, " & e & ", " & f & ", " & g & ", " & h & ")" _
)
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
But if I use all the 57 variables as I may want I get the error
Can I use some function to get a unique list or a loop that will do it in irder and bring my results back to the next blank row under the last results
Or could someone advise why the error occurs
Any insights would be much appreciated
Thanks in advance
I am sorry , this may be a very stupid question , but I have no VBA knowldge . I have spent the last two weeks developing an excel based application and that is it. I have to query using any unique value in the list aa2:aa58
The values in this list may not be unique . If I just search using 4 values in the select its ok
but I get an invalid type eror if I search will all the values I want
I know this is probably bad code but here is how I do it
Dim a As Variant
Dim b As Variant
Dim c As Variant
etc for all values
a = Range("aa2").Value
b = Range("aa3").Value
c = Range("aa4").Value
d = Range("aa5").Value
etc
With ActiveSheet.QueryTables.Add(Connection:=Array(Array( _
"ODBC;DRIVER=SQL Server;SERVER=***;UID=***;APP=Microsoft® Query;WSID=***;DATABASE=mydb;Trusted_Connec" _
), Array("tion=Yes")), Destination:=Range("AC1"))
.Sql = Array( _
"select * FROM mydb.dbo.table1 table1 WHERE table1.ID in (" & a & ", " & b & ", " & c & ", " & d & ",, " & e & ", " & f & ", " & g & ", " & h & ")" _
)
.FieldNames = True
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = True
.SaveData = True
End With
But if I use all the 57 variables as I may want I get the error
Can I use some function to get a unique list or a loop that will do it in irder and bring my results back to the next blank row under the last results
Or could someone advise why the error occurs
Any insights would be much appreciated
Thanks in advance