提问人:SamB 提问时间:1/26/2022 最后编辑:Andrew MortonSamB 更新时间:1/27/2022 访问量:36
添加参数时,MSACCES VBA 调用过程失败
msacces vba call procedure fails when parameter added
问:
我正在尝试自学存储过程的创建和回忆。搜索了 2 天后,我没有进一步解决以下问题。
我在 SQL Express 中有一个基本存储过程,如下所示:
GO
/****** Object: StoredProcedure [dbo].[spTestCustomers] Script Date: 26/01/2022 15:17:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
[dbo].[spTestCustomers]
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM [tblCustomers]
END
我可以在 MS Access 中使用以下 VB 正确调用它:
Private Sub cmdTest_Click()
'define query for ptSHCustomers
Dim SQL As String, qdf As DAO.QueryDef
Set qdf = CurrentDb.QueryDefs("ptSHCustomers")
qdf.SQL = "EXEC spTestCustomers"
Me.lstTest.Requery
End Sub
但是,当我添加参数失败时,SQL Server代码:
USE [WPSDb]
GO
/****** Object: StoredProcedure [dbo].[spTestCustomers] Script Date: 26/01/2022 15:25:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
[dbo].[spTestCustomers]
@Surname nvarchar(25)
AS
BEGIN
SET NOCOUNT ON;
SELECT * FROM [tblCustomers]
WHERE Surname like @Surname
END
我尝试从 MS Access 调用的代码:
Private Sub cmdTest_Click()
'define query for ptSHCustomers
Dim SQL As String, qdf As DAO.QueryDef
Dim VarCustSurname As String
VarCustSurname = "%"
Set qdf = CurrentDb.QueryDefs("ptSHCustomers")
qdf.SQL = "EXEC spTestCustomers ('" & VarCustName & "');"
Me.lstTest.Requery
End Sub
我已经尝试了许多在线指南中的代码更改,但都无济于事。谁能给我指出正确的方向?
答:
0赞
Andre
1/27/2022
#1
EXECUTE
不使用括号。
EXEC myProc @varParam, 'literal param';
和 VBA
qdf.SQL = "EXEC spTestCustomers '" & VarCustName & "';"
评论
0赞
SamB
1/27/2022
非常感谢,就像做梦一样。感觉很厚!
评论
VarCustSurname
cmdTest_Click()
qdf.SQL = "EXEC spTestCustomers ('" & VarCustName & "');"