+ Reply to Thread
Results 1 to 12 of 12

.CopyFromRecordset into variable array rather than to worksheet?

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

    .CopyFromRecordset into variable array rather than to worksheet?

    I'm using the following to pull data from an Access database to Excel, however, rather than pulling it all to an empty sheet and then loading each cells value into my array, is it possible to pull data directly to the array?

    Please Login or Register  to view this content.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    Not with CopyFromRecordSet, use GetRows instead.
    If posting code please use code tags, see here.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    Be a pal and gimme a clue how? MS help isn't being very helpful on the GetRows method. Seems there's a big difference between ADO getrows and DAO getrows which has been confusing me for the last 15 minutes.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    It's ADO you are using so I'm not sure why the confusion.

    Anyway, to put the results of a recordset in a 2-d array with GetRows this is all you need.
    Please Login or Register  to view this content.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    I'm missing something cos when I try to read back the array all I get is errors.

    Please Login or Register  to view this content.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    It's a 2-dimensional (2-d) array, also you can't use a message box to display the entire contents of an array.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    How about just 1?


    Please Login or Register  to view this content.
    It's not reading anything into the array, msgbox Ubound(Products) always displays '0' and msgbox Products(0) results in a subscript out of range.

    I know for sure the SQL statement is returning data as if I use:

    Please Login or Register  to view this content.
    it works just fine.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    It's always a 2-dimensional array, one dimension for fields and one for values if you like.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    Imagine you're talking to a complete noob.

    How do I access each individual value in the array?
    Why does Ubound(Products) always display '0'? I thought Ubound is supposed to return the highest position in the array?

    It should be reading in 350 odd product codes...

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    Disregard.

    Got it. For a non programmer to visualise loading 1 data field into a 2 column array.....well it's weird.

    However, Products(0,whatever) is working.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    Oops, should have mentioned that the array is 0 indexed just like the Fields collection.

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

    Re: .CopyFromRecordset into variable array rather than to worksheet?

    Thanks for the help

+ 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. Replies: 0
    Last Post: 02-04-2013, 02:28 PM
  2. Variable File Reference and Variable Table Array in VBA VLOOKUP
    By Gingeiko in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2011, 11:08 PM
  3. Set array= range, and using a variable name for a worksheet
    By Keruck in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-18-2008, 11:55 AM
  4. [SOLVED] CopyFromRecordset will fail if the recordset array data such as hierarchical recordsets ???
    By Mike in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-10-2006, 10:10 AM
  5. [SOLVED] For each syntax using a worksheet array variable
    By Ralph Heidecke in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2005, 08:06 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