+ Reply to Thread
Results 1 to 15 of 15

VBA: Save data from SQL queery in an array.

  1. #1
    Registered User
    Join Date
    07-13-2016
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    27

    VBA: Save data from SQL queery in an array.

    The following code is connecting to SQL and running a SQL queery. I works just fine. How do I store the data generated in the SQL queery to an Array in VBA?

    Please Login or Register  to view this content.
    The code above is working perfectly and the values from SQL queery is being printed in A1 and B1. Now suppose I don't want to print it in my sheets but I want to store it in an VBA array. What do I do?
    Last edited by kbdb; 07-18-2017 at 08:23 AM. Reason: Solved

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA: Save data from SQL queery in an array.

    Hi

    Please use code tags when posting code.

    You can use GetRows thus
    Please Login or Register  to view this content.
    Note that the array is transposed from the way CopyFromRecordset produces it.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    07-13-2016
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    27

    Re: VBA: Save data from SQL queery in an array.

    Quote Originally Posted by xlnitwit View Post
    Hi

    Please use code tags when posting code.

    You can use GetRows thus
    Please Login or Register  to view this content.
    Note that the array is transposed from the way CopyFromRecordset produces it.
    It acutally doesn't solve my problem. I don't think TheArray is saved as an array. Later I try to refer to TheArray(2) but it wont be recognized

    Then I add the following
    Please Login or Register  to view this content.
    Then there is a compile error: Can't assign to array. TheArray is clearly not am array.
    Last edited by kbdb; 07-18-2017 at 08:34 AM.

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA: Save data from SQL queery in an array.

    It will be a 2 dimensional array (If you look in the locals window I'm pretty sure you'll see an array) so you need to supply both dimensions when extracting data from it.

  5. #5
    Registered User
    Join Date
    07-13-2016
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    27

    Re: VBA: Save data from SQL queery in an array.

    Quote Originally Posted by xlnitwit View Post
    It will be a 2 dimensional array (If you look in the locals window I'm pretty sure you'll see an array) so you need to supply both dimensions when extracting data from it.
    How can extract data then? Suppose I want to create a new variable wich is the mean. How do I calculate that by reffering to data from TheArray?
    Last edited by kbdb; 07-18-2017 at 08:44 AM.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA: Save data from SQL queery in an array.

    Quote Originally Posted by kbdb View Post
    Then I add the following
    Please Login or Register  to view this content.
    Then there is a compile error: Can't assign to array. TheArray is clearly not am array.
    It is an array, you just apparently aren't familiar with VBA arrays. You cannot assign one array to another unless the receiving array is dynamic and of matching type.

    To extract a value from a 2 dimensional array, you must, as I mentioned, provide an index for both dimensions- e.g.
    Please Login or Register  to view this content.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: VBA: Save data from SQL queery in an array.

    How would you do it with a one-dimensional array?
    If posting code please use code tags, see here.

  8. #8
    Registered User
    Join Date
    07-13-2016
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    27

    Re: VBA: Save data from SQL queery in an array.

    Quote Originally Posted by xlnitwit View Post
    It is an array, you just apparently aren't familiar with VBA arrays. You cannot assign one array to another unless the receiving array is dynamic and of matching type.

    To extract a value from a 2 dimensional array, you must, as I mentioned, provide an index for both dimensions- e.g.
    Please Login or Register  to view this content.
    msgbox theArray(1, 1) is failing. It says: Subscript out of range.

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA: Save data from SQL queery in an array.

    Do you know how to use the Locals window in the VB editor? It will tell you the type, size and shape of the array.

  10. #10
    Registered User
    Join Date
    07-13-2016
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    27

    Re: VBA: Save data from SQL queery in an array.

    Quote Originally Posted by xlnitwit View Post
    Do you know how to use the Locals window in the VB editor? It will tell you the type, size and shape of the array.
    I just googled it so I do now. It says TheArray is of type Variant. This webpage says the same: https://msdn.microsoft.com/en-us/lib.../ff194427.aspx because we have used GetRows

  11. #11
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA: Save data from SQL queery in an array.

    Yes, and what size is it?

  12. #12
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: VBA: Save data from SQL queery in an array.

    kb, run your code with a break point set after the array has been populated. Then view the 'locals' window, scroll down to the array and click the + sign, it'll then show you all entries in the array with their address in the array. You'll also find that a variant array indexes itself from 0, not 1. Therefore, if you're using a variant array as a one dimensional array 1,1 won't exist. 1,0 might....or possibly 0,1. See the locals window to find out.

  13. #13
    Registered User
    Join Date
    07-13-2016
    Location
    Stockholm
    MS-Off Ver
    2013
    Posts
    27

    Re: VBA: Save data from SQL queery in an array.

    Quote Originally Posted by xlnitwit View Post
    Yes, and what size is it?
    Under type it says: Variant/Variant(0 to 1, 0 to 0)

  14. #14
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA: Save data from SQL queery in an array.

    If it's (0 to 1, 0 to 0) then you can't use (2, 2) as the indices. You only have:
    (0, 0)
    (1, 0)
    available.

  15. #15
    Valued Forum Contributor
    Join Date
    03-24-2014
    Location
    England
    MS-Off Ver
    Excel 2003 - 2016
    Posts
    575

    Re: VBA: Save data from SQL queery in an array.

    EDIT: disregard, posted in error

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Save Ranges in Array
    By h0ps in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2014, 07:57 AM
  2. If Condition is True, Save Data in Array, and Copy Array Data to other Worksheet
    By skelly8117 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-10-2013, 10:18 AM
  3. Save whole rows into an array
    By berlin1403 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-10-2013, 08:18 AM
  4. Save array of worksheets to csv
    By tonupgirl in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2010, 06:00 AM
  5. Save row to array
    By Jeroen1000 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-16-2010, 03:18 AM
  6. find each of the items in an array and save result in another array
    By lif in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-27-2006, 08:54 PM
  7. Can an addin save an array or UDT's to workbooks
    By John in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-30-2006, 02:20 PM

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.6.0 RC 1