简体   繁体   中英

Access Sql parameterized query assistance

I'm still new to the access world and I require your assistance once again

i call this stored procedure on 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)

i use the folowing code in access to call the 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

but i get the error user-defined type not defined

What am i doing wrong??

Well, if you save a pass-through query, then this code should work:

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

And try using SSMS and typing this in:

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

So try this from SSMS, and if it works, then my sample VBA code will work (it is assumed you saved a PT query in Access).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM