Results 1 to 15 of 15

VBA: Save data from SQL queery in an array.

Threaded View

  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?

    Sub test()
    Dim DSN As String
    DSN = "Provider= ... ;Data Source= ... ;Initial Catalog= ... ;Integrated Security= ...;"
    ' I cannot share the name of databases and so on.
    Dim Query1 As String
    Dim Res1 As New ADODB.Recordset
      Query1 = "select 1,2"
      Set Conn = New ADODB.Connection
      Conn.Open (DSN)
      
      Set Res1 = Conn.Execute(Query1)
     
      Sheet1.Range("A1").CopyFromRecordset Res1
    
      Res1.Close
      Conn.Close
    End Sub
    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

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. [SOLVED] 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