简体   繁体   中英

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. I connect to SQL Server via SQL Server Authentication. I want to execute a stored procedure with a value entered into a textbox in one of my 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? Please help!!!

Probably the most straightforward way is to create a temporary pass-through query using a DAO.QueryDef object. If you have an existing linked table in Access then you can use its .Connect property (ODBC connection information). All you need to do is set the .SQL property of the QueryDef to call (EXEC) the stored procedure, like this:

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

EXEC dbo.addContact N'Thompson'

and if the text box contains O'Rourke then the QueryDef will execute

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"

and this is what it should do: DELETE FROM Inventory where Key_Set = '320207'

When I run the procedure in the SQL, it works correctly.

now I have created a pass-through query in the Access-DB with the following content:

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

when I run the pass-through query in Access now, I get the following message:

[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect Syntax near 'dbo.Inventory_del_dataset' (#102)

do you have another idea what I'm doing wrong?

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