简体   繁体   English

从 MS Access 执行 SQL 服务器存储过程

[英]Execute a SQL Server stored procedure from MS Access

I use MS Access 2013 and SQL Server 2012. I have connected my SQL Server database to MS Access.我使用 MS Access 2013 和 SQL Server 2012。我已将 SQL 服务器数据库连接到 MS Access。 I connect to SQL Server via SQL Server Authentication.我通过 SQL 服务器身份验证连接到 SQL 服务器。 I want to execute a stored procedure with a value entered into a textbox in one of my forms.我想执行一个存储过程,其中一个值输入到我的 forms 之一的文本框中。 I have been trying to do it for ages now but nothing that I found on this website works for me.我已经尝试了很多年了,但是我在这个网站上找到的任何东西都不适合我。 Can anyone please help me and give me some tips as to how to write a basic VBA code to execute the procedure?谁能帮助我并给我一些关于如何编写基本的 VBA 代码来执行该过程的提示? Please help!!!请帮忙!!!

Probably the most straightforward way is to create a temporary pass-through query using a DAO.QueryDef object. 可能最直接的方法是使用DAO.QueryDef对象创建临时传递查询。 If you have an existing linked table in Access then you can use its .Connect property (ODBC connection information). 如果在Access中有现有链接表,则可以使用其.Connect属性(ODBC连接信息)。 All you need to do is set the .SQL property of the QueryDef to call (EXEC) the stored procedure, like this: 您需要做的就是设置QueryDef的.SQL属性来调用(EXEC)存储过程,如下所示:

Option Compare Database
Option Explicit

Private Sub Command2_Click()
    Dim cdb As DAO.Database, qdf As DAO.QueryDef
    Set cdb = CurrentDb
    Set qdf = cdb.CreateQueryDef("")
    ' get .Connect property from existing ODBC linked table
    qdf.Connect = cdb.TableDefs("dbo_myContacts").Connect
    qdf.sql = "EXEC dbo.addContact N'" & Replace(Me.Text0.Value, "'", "''") & "'"
    qdf.ReturnsRecords = False
    qdf.Execute dbFailOnError
    Set qdf = Nothing
    Set cdb = Nothing
End Sub

So, for example, if the Text0 text box contains Thompson then the QueryDef will execute 因此,例如,如果Text0文本框包含Thompson则QueryDef将执行

EXEC dbo.addContact N'Thompson'

and if the text box contains O'Rourke then the QueryDef will execute 如果文本框包含O'Rourke则QueryDef将执行

EXEC dbo.addContact N'O''Rourke'

After Trial and error this one worked for me 经过试验和错误,这个适合我

 > Private Sub UpdateItems_Click()
    >     Dim cdb As DAO.Database, qdf As DAO.QueryDef
    >     Set cdb = CurrentDb
    >     Set qdf = cdb.CreateQueryDef("")
    >     ' get .Connect property from existing ODBC linked table
    >     qdf.Connect = cdb.TableDefs("dbo_AccesLinkedTable").Connect
    >     qdf.SQL = "EXEC dbo.YourStoreProcedure"
    >     qdf.ReturnsRecords = False
    >     qdf.Execute dbFailOnError
    >     Set qdf = Nothing
    >     Set cdb = Nothing
    > 
    >  MsgBox "Records Updated!"
    > 
    > End Sub

Thanks for your help.谢谢你的帮助。 Unfortunately my query does not work不幸的是我的查询不起作用

The name of the stored procedure in the SQL-DB is "dbo.Inventory_del_dataset" SQL-DB 中存储过程的名称是“dbo.Inventory_del_dataset”

and this is what it should do: DELETE FROM Inventory where Key_Set = '320207'这就是它应该做的:DELETE FROM Inventory where Key_Set = '320207'

When I run the procedure in the SQL, it works correctly.当我在 SQL 中运行该程序时,它可以正常工作。

now I have created a pass-through query in the Access-DB with the following content:现在我在 Access-DB 中创建了一个传递查询,其内容如下:

currentdb.QueryDefs("dbo.Inventory_del_dataset").Execute currentdb.QueryDefs("dbo.Inventory_del_dataset").Execute

when I run the pass-through query in Access now, I get the following message:当我现在在 Access 中运行传递查询时,我收到以下消息:

[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect Syntax near 'dbo.Inventory_del_dataset' (#102) [Microsoft][用于 SQL 服务器的 ODBC 驱动程序 17][SQL Server]'dbo.Inventory_del_dataset' (#102) 附近的语法不正确

do you have another idea what I'm doing wrong?你有另一个想法我做错了什么吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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