+ Reply to Thread
Results 1 to 12 of 12

.CopyFromRecordset into variable array rather than to worksheet?

Hybrid View

BellyGas .CopyFromRecordset into... 09-02-2014, 10:11 AM
Norie Re: .CopyFromRecordset into... 09-02-2014, 10:15 AM
BellyGas Re: .CopyFromRecordset into... 09-02-2014, 10:35 AM
Norie Re: .CopyFromRecordset into... 09-02-2014, 10:56 AM
BellyGas Re: .CopyFromRecordset into... 09-03-2014, 06:31 AM
Norie Re: .CopyFromRecordset into... 09-03-2014, 06:34 AM
BellyGas Re: .CopyFromRecordset into... 09-03-2014, 06:54 AM
Norie Re: .CopyFromRecordset into... 09-03-2014, 06:55 AM
BellyGas Re: .CopyFromRecordset into... 09-03-2014, 07:10 AM
BellyGas Re: .CopyFromRecordset into... 09-03-2014, 07:20 AM
Norie Re: .CopyFromRecordset into... 09-03-2014, 07:24 AM
BellyGas Re: .CopyFromRecordset into... 09-03-2014, 07:40 AM
  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?

    
    Dim NSV(1 To 52) As Double
    Dim cnt As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim DBpath As String
    Dim sSQL As String
    
    Sheets("Data").Select
    Cells.Select
    Selection.ClearContents
    
    DBpath = Range("Menu!G19").Value
    Set cnt = New ADODB.Connection
    
    #If VBA7 Then
    cnt.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & DBpath & ";"
    #Else
    cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & DBpath & ";"
    #End If
    
    Set rst = New ADODB.Recordset
    
    sSQL = "SELECT * FROM SalesValues;"
    
    rst.Open sSQL, cnt
    If Not rst.EOF Then
       For n = 1 To rst.Fields.Count
         ActiveSheet.Cells(1, n).Value = rst.Fields(n - 1).Name
       Next n
       Range("Data!A2").CopyFromRecordset rst
    End If
    rst.Close
    cnt.close
    
    For Sweep = 1 To 52
        NSV(Sweep) = Cells(2, Sweep + 1).Value
    Next Sweep

  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.
    Dim arr As Variant
    
    ' other code
    
        arr = rst.GetRows()

  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.

    
    msgbox Arr    ' results in "Type mismatch"
    msgbox Arr(1)  ' results in "Subscript out of range"

  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?


    Dim Products as Variant
    
    sSQL = "SELECT DISTINCT product FROM Production_Forecast;"
    rst.Open sSQL, cnt
    Products = rst.GetRows()
    rst.Close
    MsgBox UBound(Products)
    Msgbox Products(0)
    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:

    
    rst.Open sSQL, cnt
    If Not rst.EOF Then
       Range("Data!C1").CopyFromRecordset rst
    End If
    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. Replies: 0
    Last Post: 06-10-2006, 10:10 AM
  5. 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