3

我有VBA代碼在SQL-Server 2008中運行查詢。它運行良好並顯示我需要的表。執行此代碼是在這裏:通過VBA傳遞查詢的返回值

Set db = CurrentDb 
Set qdf = db.QueryDefs("MyStoredProcedure") 

qdf.SQL = "exec [WCNS_Ops].[dbo].MyStoredProcedure [plus a bunch of parameters]" 
DoCmd.OpenQuery "MyStoredProcedure" 

它顯示此表:

Picture of the table the stored procedure returns

我的問題是:如何以編程方式將這些值VBA代碼,而不顯示錶?

回答

3

下面的代碼是未經測試,但應該讓你在正確的方向指出:

Set db = CurrentDb 

Set qdf = db.QueryDefs("MyStoredProcedure") 
qdf.ReturnsRecords = True 
qdf.SQL = "exec [WCNS_Ops].[dbo].MyStoredProcedure [plus a bunch of parameters]" 

With qdf.OpenRecordset(dbOpenSnapshot) 'could also be dbOpenDynaset, etc. ' 
    Do Until .EOF 
     Debug.Print !firstid 
     Debug.Print !lastid 
     .MoveNext 
    Loop 
End With 
+0

工作完美。非常感謝。 – 2011-02-09 13:32:39

1

所有你需要做的就是執行查詢,其輸出設置爲一個記錄。關閉我的頭頂像這樣的東西

Dim dbCon as new ADODB.Connection 
Dim rst as new ADODB.Recordset 
Dim cmd as new ADODB.Command 

dbCon.ConnectionString=」Your Connection String」 
with cmd 
    .comandtype=adCmdStoredProc 
    .commandtext=」Your SP name」 
    .Parameters.Append .CreateParameter("@Pram1", adVarChar, adParamInput, 50, 「WhatEver」) 
    .ActiveConnection=dbCon 
    .NamedParameters = True 
    Set rst = .Execute 
end with 

with rst 
    if .EOF=false then 
     myVar=!Column1 
    end if 
end with 

rst.close 
dbcon.close 
set cmd=nothing