2

我有一個存儲過程需要參數,但不返回任何內容。我如何從MS Access中調用它?如何從MS Access VBA中調用SQL Server存儲過程

編輯 - 隨着Remou的幫助下,我找到了工作:

Set cnn = New adodb.Connection 
cnn.ConnectionString = 
    "driver=SQL Server;server=SERVER\SERVER;database=a_db;Trusted_Connection=Yes" 

cnn.Open cnn.ConnectionString 

Set cmd = New adodb.Command 
cmd.ActiveConnection = cnn 
cmd.CommandType = adCmdStoredProc 
cmd.CommandText = "stprMoveDataSet" 

Set param = cmd.CreateParameter 
       ("DataSetID", adInteger, adParamInput, 6, stDataSet) 
cmd.Parameters.Append param 
Set param = cmd.CreateParameter 
       ("Destination", adChar, adParamInput, 2, stDestination) 
cmd.Parameters.Append param 

rs.CursorType = adOpenStatic 
rs.CursorLocation = adUseClient 
rs.LockType = adLockOptimistic 
rs.Open cmd 
+0

你可以對一個連接上運行,也可以使用直通http://support.microsoft.com/kb/303968 – Fionnuala

+0

GRRR,這不起作用,有什麼想法? – aSystemOverload

+0

它告訴我'不能執行選擇查詢',它使我感到困惑,因爲它不是一個選擇查詢 – aSystemOverload

回答

4

這裏有一個大綱,可以幫助。稍後我可能會回到更多細節。它不是具體到你的情況,但它可能讓你去:)

Public dbconn As ADODb.Connection 
Dim cmd As New ADODb.command 
Dim rs As New ADODb.Recordset 
Dim param As New ADODb.Parameter 

Set dbconn = New ADODb.Connection 
dbconn.ConnectionString = GetServerCon 

dbconn.Open dbconn.ConnectionString 

Set cmd = New ADODb.command 
cmd.ActiveConnection = dbconn 
cmd.CommandType = adCmdStoredProc 
cmd.CommandText = "insert_user" 

Set param = cmd.CreateParameter(_ 
    "username", adVarChar, adParamInput, 50, Environ("UserDomain") & "dddddddd\" _ 
    & Environ("Username")) 
cmd.Parameters.Append param 

rs.CursorType = adOpenStatic 
rs.CursorLocation = adUseClient 
rs.LockType = adLockOptimistic 
rs.Open cmd 
+0

感謝Remou,我會檢查它並讓你知道;) – aSystemOverload

相關問題