Solved

Sql Return multiple results to multiple worksheets

Posted on 2013-05-13
2
263 Views
Last Modified: 2013-05-13
Morning Experts,

I connect excel to sql through using vba and executing a stored procedure.  In the past I have only allowed the Stored procedure to return one data set and then copy it to a single worksheet.  Now I was wondering if there is a way to deal with a stored procedure that returns 3 data sets that would put them on sheets (data1, data2, data3).  


Sub Data_Sheet()

    Dim cn As New ADODB.Connection
    Dim comm As New ADODB.Command
    Dim rs As New ADODB.Recordset
    Dim dbConnectStr As String
    Dim strSheet As String
    Dim strRange As String
    Dim vName As String
    Dim vBDate As Date
    Dim vEDate As Date

    Application.ScreenUpdating = False

    ' Connection to SQL Server
    UID = "blah"
    Password = "pwrd"
    Srv = "connection"


    strSheet = "Data1"
    strRange = "A2"
    vName = ActiveWorksheet("test").Range("A2").Value
    vData = Workbooks("Loop and open files in directory").Worksheets("Sheet1").Range("A2").Value

    


    'dbConnectStr
    cn.ConnectionString = "Driver={SQL Server};" & _
                          "Server=" & Srv & ";" & _
                          "User Id=" & UID & ";" & _
                          "Password=" & Password
     cn.ConnectionTimeout = 0    ' Set Connection to not Time Out


    comm.CommandType = adCmdText
    comm.CommandText = "EXEC SP " & " " & "'" & vName & "'" & "," & "'" & vdata & "'"    'Execute Stored Procedure

    Set comm.ActiveConnection = cn
    rs.ActiveConnection = cn

    comm.CommandTimeout = 0
    rs.Open comm

    'Clear Previous data

    Sheets(strSheet).Select
    Range(strRange).Select
    Range(strRange, "P100").Select
    Selection.ClearContents
    Range(strRange).Select


    Sheets(strSheet).Range(strRange).CopyFromRecordset rs  'copy the records
    
    Call FormulaFill

    rs.Close    
   
 cn.Close

    Application.ScreenUpdating = True
    '
    MsgBox "Done"
   
End Sub

Open in new window


Thanks,
Montrof
0
Comment
Question by:montrof
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 500 total points
ID: 39161447
Once you have output the first recordset, use
set rs = rs.nextrecordset

Open in new window

output that to the relevant sheet and then repeat. :)
0
 
LVL 1

Author Closing Comment

by:montrof
ID: 39161464
awesome!!! Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now