Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sql Return multiple results to multiple worksheets

Posted on 2013-05-13
2
Medium Priority
?
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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…

618 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