1

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.

4

1 回答 1

6

如果您已经有一个指向 SQL Server 数据库的 Access 链接表,那么您可以简单地使用它的.Connect字符串和一个DAO.QueryDef对象来执行存储过程,如下面的 VBA 代码所示:

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
于 2013-09-07T18:37:00.930 回答