Solved

Sql Return multiple results to multiple worksheets

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

729 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