Connection string for Access to call SQL Server stored procedure

情到浓时终转凉″ 提交于 2019-11-30 09:07:33

问题


Using Access 2007, I want to call a stored procedure with one input parameter that returns a recordset.

Using ADODB, this is pretty straightforward except for the connection string. I want to be able to derive the server and database names from a particular table, which always points to the correct server and database. (I reconnect to development dbs from time to time for testing by relinking the 100 or so linked tables.)

Is there a way to get the server and database name from the tabledef without parsing the whole thing out? Is there a property? I haven't found one yet....

Final query is pretty simple: EXEC sp_DeleteProjects N'12,24,54' deletes projects 12, 24, and 54, and returns a recordset (single row) with the deleted record counts of the various child table entries.


回答1:


If you already have an Access linked table pointing to the SQL Server database then you can simply use its .Connect string with a DAO.QueryDef object to execute the Stored Procedure, as illustrated by the following VBA code:

Sub CallSP()
Dim qdf As DAO.QueryDef, rst As DAO.Recordset
Set qdf = CurrentDb.CreateQueryDef("")
qdf.Connect = CurrentDb.TableDefs("dbo_MyTable").Connect
qdf.SQL = "EXEC dbo.MyStoredProcedure"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
Debug.Print rst(0).Value
rst.Close
Set rst = Nothing
Set qdf = Nothing
End Sub


来源:https://stackoverflow.com/questions/18664139/connection-string-for-access-to-call-sql-server-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!