2017-02-23 165 views
0

我還是新的接入世界,我再次需要你的幫助訪問SQL參數化查詢援助

我呼籲這個SQL存儲過程

ALTER PROCEDURE [dbo].[sp_StockMovement] 
    @type CHAR(1), 
    @itmcd VARCHAR(25), 
    @descr VARCHAR(60), 
    @jsbtch VARCHAR(25), 
    @poprj VARCHAR(20), 
    @spldpt VARCHAR(50), 
    @frmdt CHAR(10), 
    @todt CHAR(10) 
AS 
DECLARE @SQLMain VARCHAR(500), @SQLCriteria VARCHAR(450), @SQLOrder VARCHAR(50), @SQL VARCHAR(1000), 
    @count INT 

SET @count = 0 
SET @SQLCriteria = 'WHERE ' 
SET @SQLOrder = 'ORDER BY stdate, jscode, jsbatch, poproject;' 

IF @itmcd LIKE '%.%' AND @itmcd NOT LIKE '%-%' 
    BEGIN 
     SELECT @itmcd = CONVERT(CHAR(10), prd_jscode) FROM dbo.tbl_Products WHERE prd_jscodeold = @itmcd; 
    END 


IF @type = 'R' 
BEGIN 
    SET @SQLMain = 'SELECT CASE WHEN ISNUMERIC(str_jscode) = 1 ' + 
          'THEN STUFF(STUFF(STUFF(str_jscode, 9, 0, '' ''), 6, 0, '' ''), 4, 0, '' '') ' + 
          'ELSE str_jscode END AS jscode, ' + 
         'str_description AS descr, str_jsbatch AS jsbatch, str_qty AS qty, str_unit AS unit, ' + 
         'str_ponumber AS poproject, str_supplier AS spldept, str_invoice AS invoice, ' + 
         'str_splbatch AS splbatch, CONVERT(CHAR(10), str_date, 111) AS stdate, ' + 
         'str_user AS stuser ' + 
        'FROM dbo.vw_StockReceived ' 


(There is more code but this is the main part so to speak) 

我使用如下因素代碼的訪問致電STOR PROC

Private Sub Command3_Click() 

Dim rs As ADODB.Recordset 
Dim cn As ADODB.Connection 
Dim cmd As ADODB.Command 


Set cn = New ADODB.Connection 

cn.ConnectionString = "driver={sql server};server=xxx.xxx.x.xxx;Database=JSLogistics;UID=JSLogist;PWD=JSL$35p" 
cn.Open 

    Set cmd = New ADODB.Command 

     cmd.ActiveConnection = cn 
     cmd.CommandText = "dbo.sp_StockMovement " 
     cmd.CommandType = adCmdStoredProc 
     cmd.NamedParameters = True 

     cmd.Parameters.Append = .CreateParameter("@type", adChar, adParamInput, 1, "R") 
     cmd.Parameters.Append = .CreateParameter("@itmcd", adVarChar, adParamInput, 25, "_NA_") 
     cmd.Parameters.Append = .CreateParameter("@descr", adVarChar, adParamInput, 60, "_NA_") 
     cmd.Parameters.Append = .CreateParameter("@jsbtch", adVarChar, adParamInput, 25, "_NA_") 
     cmd.Parameters.Append = .CreateParameter("@poprj", adVarChar, adParamInput, 20, "_NA_") 
     cmd.Parameters.Append = .CreateParameter("@spldpt", adVarChar, adParamInput, 50, "_NA_") 
     cmd.Parameters.Append = .CreateParameter("@frmdt", adChar, adParamInput, 10, "2017/02/01") 
     cmd.Parameters.Append = .CreateParameter("@todt", adChar, adParamInput, 10, "2017/02/02") 

     cmd.Execute 

    Set rs = New ADODB.Recordset 
    With rs 
     .CursorLocation = adUseClient 
     .CursorType = adOpenStatic 
     .LockType = adLockReadOnly 
     .Open cmd 
    End With 
    Set Me!lstJobQuickSearch.Recordset = rs 
    Me.lstJobQuickSearch.Requery 

Set cmd = Nothing 

End Sub 

,但我得到沒有定義的錯誤 用戶定義類型

我在做什麼錯?

+1

哪裏出現錯誤? –

回答

0

好吧,如果你保存一個傳遞查詢,那麼這段代碼應該工作:

Dim rst  As DAO.Recordset 

With CurrentDb.QueryDefs("qryPassR") 
    .SQL = "dbo.sp_StockMovement 'R','_NA_','_NA_','_NA_','_NA_','_NA_','2017/02/01','2017/02/01'" 
    .ReturnsRecords = True 
    Set rst = .OpenRecordset() 
End With 

並嘗試使用SSMS和打字:

dbo.sp_StockMovement 'R','_NA_','_NA_','_NA_','_NA_','_NA_','2017/02/01','2017/02/01' 

所以從SSMS試試這個,如果它工作,那麼我的示例VBA代碼將起作用(假定您在Access中保存了PT查詢)。