+ Reply to Thread
Results 1 to 5 of 5

Thread: VBA array from SQL query

  1. #1
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    VBA array from SQL query

    hi,

    how do i pass an sql query into an array in vba

    i have this so far:
    rst.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
    however this seems to error with subscript out of range

    thanks

    Jonathan
    Last edited by Jollyfrog; 10-27-2010 at 11:57 AM.

  2. #2
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: VBA array from SQL query

    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

    Dim idt As Variant
    ...
    If rst.EOF = False Then idt = Application.Transpose(rst.GetRows)
    No need to ReDim
    Last edited by DonkeyOte; 10-27-2010 at 10:48 AM. Reason: added EOF back

  3. #3
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: VBA array from SQL query

    ah yes i remember now! please excuse my forgetfulness

    next question!..

    how do i pass it back into a query..
    format something like:

    rst.Open "Select MAX([Subscriber %]) as pc from Package WHERE [ID] = " & idt & " and 
    [Digital/Analogue] = 'Digital' and Operator = 'WARNER' and country = '" & cntry & "'", conn
    or should i post this as a new thread?

    thanks

  4. #4
    Forum Moderator DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Suffolk, UK
    MS-Off Ver
    2002, 2007 & 2010
    Posts
    21,423

    Re: VBA array from SQL query

    It might be that you could just use a SubQuery directly ?

    To answer your question... you could use Join

    "WHERE [ID] IN (" & Join(Application.Transpose(idt),",") & ") AND ..."
    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 ('" & 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

  5. #5
    Registered User
    Join Date
    09-23-2010
    Location
    London, England
    MS-Off Ver
    2007
    Posts
    97

    Re: VBA array from SQL query

    wow!.. thats exactly what i need,

    Thankyou for such a concise and well written answer!

    Jonathan

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0