使用直通参数访问调用存储过程

时间:2014-10-03 10:01:53

标签: sql-server vba ms-access stored-procedures parameter-passing

我正在使用链接到SQL Server 2012数据库后端的Access 2010前端。

在Access前端中,我有一个常规搜索屏幕,包含一个文本框,结果显示在列表框中。我希望文本框能够搜索多个字段,这些字段也需要是外卡。

所以,我的存储过程是这样的:

ALTER PROCEDURE [dbo].[SalesGeneralSearch]
   @Search nvarchar(50) = ''
AS
BEGIN
   SET NOCOUNT ON;

   SELECT 
      tblJobHead.JobNum, 
      tblCustomer.LastName, 
      tblCustomer.M3DealerCode, 
      tblCustomer.TradeRef, 
      tblCustomer.Postcode, 
      tblJobHead.[Item Number], 
      tblJobHead.Description, 
      tblStatus.[Desc] AS Status
   FROM 
      tblCustomer 
   INNER JOIN 
      (tblJobHead 
   INNER JOIN 
      tblStatus ON tblJobHead.Status = tblStatus.StatusID) ON tblCustomer.RepNum = tblJobHead.[Rep Num]
   WHERE 
      (((tblJobHead.JobNum) Like '%'+ @Search + '%')) 
       OR (((tblCustomer.LastName) Like '%' + @Search + '%')) 
       OR (((tblCustomer.M3DealerCode) Like '%' + @Search + '%')) 
       OR (((tblCustomer.TradeRef) Like '%' + @Search + '%')) 
       OR (((tblCustomer.Postcode) Like '%' + @Search + '%'))
   ORDER BY 
      tblJobHead.JobNum DESC;
END

好的,现在回到Access。在搜索屏幕上,我有用户输入的文本框,点击搜索的按钮和结果的列表框。在按钮的click事件中,我有以下代码:

Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim adString As Variant


Set cn = New ADODB.Connection

cn.ConnectionString = "DRIVER=SQL Server;SERVER=XXX;Database=XXX;Trusted_Connection=YES;"
cn.Open

    Set cmd = New ADODB.Command
    With cmd
        .ActiveConnection = cn
        .CommandText = "dbo.SalesGeneralSearch"
        .CommandType = adCmdStoredProc
        Set prm = .CreateParameter("@Search", adString, adParamInput)
        .Parameters.Append prm
        cmd.Execute
        prm.Value = Me.Search.Text
    End With

    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 prm = Nothing
Set cmd = Nothing

然而,当我点击搜索按钮时,我一直收到以下错误:

运行时错误3708参数对象定义不正确

并突出显示

.Parameters.Append prm

lstJobQuickSearch的行源也是传递查询,使用以下内容:

SELECT tblJobHead.JobNum, tblCustomer.LastName, tblCustomer.M3DealerCode, tblCustomer.TradeRef, tblCustomer.Postcode, tblJobHead.[Item Number], tblJobHead.Description, tblJobHead.FN, tblStatus.[Desc] AS Status
FROM tblCustomer INNER JOIN (tblJobHead INNER JOIN tblStatus ON tblJobHead.Status = tblStatus.StatusID) ON tblCustomer.RepNum = tblJobHead.[Rep Num]
ORDER BY tblJobHead.JobNum DESC;

我哪里出错了?任何帮助将不胜感激。

由于我只想返回值,使用传递查询和编码参数是否更有效?我不确定,我已经做了好几天了: - (

此致

迈克尔

2 个答案:

答案 0 :(得分:3)

我会使用DAO传递。假设您保存了pass-though查询,那么此代码将起作用:

  With CurrentDb.QueryDefs("qPass")
     .SQL = "exec SalesGeneralSearch " & strSearch
     Set Me.MyListBox.RowSource = .OpenRecordset
  End If

实际上没有必要发布这些大量的代码,这些代码只会被不诚实的开发人员盗用公司的计费时间,而上面的简单2行就足够了。

答案 1 :(得分:2)

问题是存储过程将输入参数声明为nvarchar(50),但在VBA中,使用ADODB.Parameter定义adString并且没有长度。问题:

    SQL Server上的
  1. nvarchar映射到ADO中的adVarWChar
  2. 字符串参数几乎总是需要定义(最大)长度
  3. 所以解决方法是将参数声明更改为

    Set prm = .CreateParameter("@Search", adVarWChar, adParamInput, 50)