hi,
how do i pass an sql query into an array in vba
i have this so far:
however this seems to error with subscript out of rangerst.Open "Select [Package ID] as ids from ChannelBrandPackageLink WHERE [Channel Brand] = '" & Cells(r, 1) & "'", conn RecordCount = rst.RecordCount ReDim idt(RecordCount) idt = rst!ids rst.Close
thanks
Jonathan
Last edited by Jollyfrog; 10-27-2010 at 11:57 AM.
Whether you need the Array or not was I think covered previously ?
(the recordset is to all intents and purposes an Array in it's own right)
However, if you want to, I'd repeat the code given previously
No need to ReDimDim idt As Variant ... If rst.EOF = False Then idt = Application.Transpose(rst.GetRows)
Last edited by DonkeyOte; 10-27-2010 at 10:48 AM. Reason: added EOF back
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
ah yes i remember now! please excuse my forgetfulness
next question!..
how do i pass it back into a query..
format something like:
or should i post this as a new thread?rst.Open "Select MAX([Subscriber %]) as pc from Package WHERE [ID] = " & idt & " and [Digital/Analogue] = 'Digital' and Operator = 'WARNER' and country = '" & cntry & "'", conn
thanks![]()
It might be that you could just use a SubQuery directly ?
To answer your question... you could use Join
If [ID] is not a numeric type then you would need to adjust the above to encase each item within ' eg:"WHERE [ID] IN (" & Join(Application.Transpose(idt),",") & ") AND ..."
if you have apostrophe's within the ID's themselves then that would require addressing"WHERE [ID] IN ('" & Join(Application.Transpose(idt),"','") & "') AND ..."
"WHERE [ID] IN ('" & Replace(Replace(Join(Application.Transpose(idt),"^,^"),"'","''"),"^","'") & "') AND ..."
Last edited by DonkeyOte; 10-27-2010 at 11:36 AM. Reason: added final example re: IDs with apostrophe's
My Recommended Reading:
Volatility
Sumproduct & Arrays
Pivot Intro
Email from XL - VBA & Outlook VBA
Function Dictionary & Function Translations
Dynamic Named Ranges
wow!.. thats exactly what i need,
Thankyou for such a concise and well written answer!
Jonathan
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks