Solved

Sql Return multiple results to multiple worksheets

Posted on 2013-05-13
2
265 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

733 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