Connection string for Access to call SQL Server stored procedure
Asked Answered
S

1

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.

Subinfeudate answered 6/9, 2013 at 18:19 Comment(0)
I
6

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
Interact answered 7/9, 2013 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.