Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Unanswered: Multiple Resultsets through Excel via VBA

    Sql Server 2005 /Excel 2003

    I tried returning top & bottom results & then importing all 4 sets through VBA. However, only the first set is returned in Excel. How can I return all 4 sets in a single worksheet? Could I call another stored procedure by using another strsql or is an 2nd separate call okay to import the full data on a separate sheet?

    create table #tellerRank
    ( tnum int
    ,bank varchar (4)
    ,loc varchar (6)
    ,description varchar(20)
    ,inv money
    ,roi money
    )
    insert into #tellerRank values ('10054','E102','E10201', 'BA', '10034.25', '200.57')
    insert into #tellerRank values ('10068','S115','S1105', 'WM', '10238.25', '168.89')
    insert into #tellerRank values ('10174','N202','N20206', 'WF', '17534.25', '267.94')
    insert into #tellerRank values ('16754','W902','W90203', 'NY', '13584.25', '245.72')
    insert into #tellerRank values ('12646','S302','E30204', 'CH', '11682.25', '186.20')
    insert into #tellerRank values ('13579','N113','N11301', 'BA', '16742.35', '210.09')
    insert into #tellerRank values ('13550','N113','N11302', 'WM', '19456.88', '250.38')
    insert into #tellerRank values ('10354','E478','E47803', 'WF', '12575.59', '207.65')
    insert into #tellerRank values ('10088','S101','E10104', 'WF', '11791.58', '178.49')
    insert into #tellerRank values ('10234','E102','E10204', 'NB', '13575.60', '225.99')
    insert into #tellerRank values ('10567','E102','E10203', 'BA', '12847.78', '201.62')

    --ranked worksheet
    select top 10 percent tnum, bank, loc, description
    ,inv
    ,row_number() over (order by inv desc) as [inv_Rank]
    ,roi
    ,row_number() over (order by roi desc) as [roi_Rank]
    from #tellerRank


    select top 10 percent tnum, bank, loc, description
    ,inv
    ,row_number() over (order by roi desc) as [roi_Rank]
    ,roi
    ,row_number() over (order by inv desc) as [inv_Rank]
    from #tellerRank



    select top 10 percent tnum, bank, loc, description
    ,inv
    ,row_number() over (order by roi) as [roi_Rank]
    ,roi
    ,row_number() over (order by inv) as [inv_Rank]
    from #tellerRank

    select top 10 percent tnum, bank, loc, description
    ,inv
    ,row_number() over (order by inv) as [inv_Rank]
    ,roi
    ,row_number() over (order by roi) as [roi_Rank]
    from #tellerRank
    -------------
    Sub TestStoredProcedure()


    Dim strSql As String
    Dim objConn As New ADODB.Connection
    Dim Rank As New ADODB.Recordset
    Dim cmd As New ADODB.Command

    'Set objConn = New ADODB.Connection
    objConn.CommandTimeout = 1200
    objConn.Open "Provider=sqloledb.1;data source=pomv;Initial catalog=rankqry;Integrated Security = SsPI;"
    'objConn.Open "Provider=sqloledb.1;data source=YourServerHere;Initial catalog=" & UseDatabase & ";Integrated Security = SsPI;"

    'Set Rank = New ADODB.Recordset
    'On Error GoTo Errhandler
    'objConn.CommandTimeout = 1200

    strSql = "exec rankprocedure '" & Worksheets("Main").Range("b3") & "','" & Worksheets("Main").Range("b4") & "'"
    'strSql = "exec rankprocedure '6/1/09','6/1/09'"

    'MsgBox (strSql)

    'Set Rank = objConn.Execute(strSql)
    Rank.Open strSql, objConn, adOpenStatic, adLockReadOnly

    With cmd
    Set .ActiveConnection = objConn
    .CommandText = "rankprocedure "
    .CommandType = adCmdStoredProc
    .Prepared = True
    End With
    Set Rank = cmd.Execute
    Rank.NextRecordset



    'Worksheets("Data").Range("A2").CopyFromRecordset Rank

    ' rankS.Close
    'Set Rank = Nothing
    'objConn.Close
    'Set objConn = Nothing


    End Sub


    Sub ExeAll()
    ClearDataSheet
    TestStoredProcedure
    TestStoredProcedure2
    RefrshPivots
    End Sub
    Last edited by ackrite55; 02-01-10 at 11:54.

  2. #2
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    yes. I am not too short.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  3. #3
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by ackrite55 View Post
    However, only the first set is returned in Excel.
    No, you only retrieve the first set. Check out the NextRecordset() method.
    ---
    "It does not work" is not a valid problem statement.

  4. #4
    Join Date
    Jan 2010
    Posts
    2
    The run-time error states that the procedure expects parameter'@startdate, which was not supplied. I have tried variations & a few loops, but each of them produced an error. The current error highlights the 'Set Rank=cmd.execute''

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •