+ Reply to Thread
Results 1 to 5 of 5

How to store query data into dynamic array?

  1. #1
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    How to store query data into dynamic array?

    I have the code which can successfully pull data from database, but my question is related to something else.

    I do NOT want to copy data to sheet Test, for two reasons:
    1) The return query data may have many records (exceeding maximum excel 2013 row limitation 1,048,576), which will cause the code bug
    2) I will need further process the data, it is very time consuming to loop through excel data.

    So I am thinking about sorting data in memory, processing data in memory should be much faster than processing data in spreadsheet. I will copy final data (after being processed) into spreadsheet. That being said, I do NOT want this line of code (ThisWorkbook.Sheets("Test").Cells(7, 5).CopyFromRecordset CVRs) at this point, I want to sort the data into an dynamic array, since I don't know how many records from CVRs.

    Maybe I need a line to code to count the number of returned records? If the number of records is k, then

    Please Login or Register  to view this content.
    How to count the number of record? How to store the records into the arrays?


    Thanks.



    Please Login or Register  to view this content.

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: How to store query data into dynamic array?

    Recordset is an array, and more versatile than VBA array. For an example, you can reference by column index or name.

    If you want to minimize duration of connection being open. Then you could...
    Please Login or Register  to view this content.
    Edit: Though I'd recommend doing computation on SQL side, leveraging more powerful server to do the heavy lifting (I assume data is housed in SQL server). You can do pretty much any folding/aggregation needed in query itself.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to store query data into dynamic array?

    Quote Originally Posted by CK76 View Post
    Recordset is an array, and more versatile than VBA array. For an example, you can reference by column index or name.

    If you want to minimize duration of connection being open. Then you could...
    Please Login or Register  to view this content.
    Oh, thanks for the information. For GetRows , I assume ar is multiple dimensional array. Since I need further process the data, how can I refer to specific fields (Field1, Field2, Field3) within the array?

    Let us say, I want to break it down to 3 small arrays for each field: ar1, ar2, ar3 . That being said ar(0) = ar1(0)+ ar2(0) + ar3(0); ar(1) = ar1(1)+ ar2(1) + ar3(1); .....

    How to print array into sheet? For example, how to copy/paste ar to sheets("Test").Cells(1,1)?

    Thanks.

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: How to store query data into dynamic array?

    Sample below, for filling array with single column data (using Field Index#, you could use it's name in place).
    Do note that .RecordCount will return different result depending on cursor type of Recordset object.
    I typically use... "adOpenStatic" This is specified in Recordset.Open method.
    Please Login or Register  to view this content.
    Last edited by CK76; 05-30-2018 at 10:02 AM.

  5. #5
    Forum Contributor
    Join Date
    09-10-2016
    Location
    USA
    MS-Off Ver
    Office 365
    Posts
    680

    Re: How to store query data into dynamic array?

    Quote Originally Posted by CK76 View Post
    Sample below, for filling array with single column data (using Field Index#, you could use it's name in place).
    Do note that .RecordCount will return different result depending on cursor type of Recordset object.
    I typically use... "adOpenStatic" This is specified in Recordset.Open method.
    Please Login or Register  to view this content.
    Thank you. I will try the code later.

+ 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. [SOLVED] store data in memory array and then paste to sheet all at once
    By JasperD in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-15-2015, 03:07 PM
  2. [SOLVED] Dynamic Changing Data, Based on Criteria Store in Array, Sum array and store in Cell
    By penbeacho in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-28-2015, 10:31 AM
  3. Can I store an array of data within a table?
    By krabine in forum Access Tables & Databases
    Replies: 3
    Last Post: 01-14-2015, 12:04 PM
  4. [SOLVED] How to store variable data into an array - help on arrays
    By mandukes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-15-2014, 09:24 AM
  5. Store data into an array if/based on specific conditions met
    By exlgh91 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-21-2013, 11:38 AM
  6. Can i add another dimension to my variant array and store additional data?
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-20-2012, 12:29 PM
  7. How Do You Take Data Off A Sheet and Store It In An Array
    By Raleigh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2006, 04:50 PM

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