Connection string for Access to call SQL Server stored procedure

后端 未结 1 1372
傲寒
傲寒 2020-12-20 09:13

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 c

相关标签:
1条回答
  • 2020-12-20 09:39

    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
    
    0 讨论(0)
提交回复
热议问题