如何通过传递参数使用Ms-Access表单执行Sql server存储过程

时间:2012-03-26 07:45:54

标签: sql-server-2008 ms-access-2007

我需要开发Access表单来传递参数来调用Sql Server Stored procedure.and需要显示输出。 例如:我有两个参数“开始日期”和“结束日期”      从Access表单我需要传递这些日期并执行Sql server存储过程和输出应该显示..

请帮助我......请一步一步帮助我,我是This Access应用程序的新手

2 个答案:

答案 0 :(得分:3)

这取决于您的存储过程输出类型,但基本上,假设您要将存储过程myProc的结果显示到调试控制台,您可以执行以下操作:

Sub printMyProcResults(startDate as Date, endDate as Date)
    Dim db as DAO.Database
    Dim qf as DAO.QueryDef
    Dim rs as DAO.Recordset

    Set db = CurrentDb()        
    Set qf = db.CreateQueryDef("")
    ' Set to true if your stored procedure returns something, otherwise, '
    ' set to False '
    qf.ReturnsRecords = True

    ' You need to adjust this to whatever your SQL server instance name is '
    ' and whatever your database name is '
    ' This connection string will use the local machine's user credentials '
    ' to connect to the server, change as appropriate '
    qf.Connect = "ODBC;DRIVER=SQL Server;SERVER=MYSERVER;Trusted_Connection=Yes;DATABASE=MYDATABASE;"

    ' We construct the SQL to call the procedure. Update this to suit your '
    ' actual proc name '
    qf.SQL = "myStoredProc '" & Format(startDate, "dd mmm yyyy") & "'," & _
                          "'" & Format(endDate, "dd mmm yyyy") & "'" 

    ' Open the recordset to access the results '
    Set rs = qf.OpenRecordSet()

    ' Print the result to the debug console '
    ' Of course, you need to adapt this to your own case '
    Do While Not rs.EOF
       debug.print rs(0)
       rs.MoveNext
    Loop
    rs.Close
    ' Cleanup ' 
    Set rs = Nothing
    Set qf = Nothing
    Set db = Nothing
End Sub

对于连接字符串,您可能需要根据SQL Server的配置方式将其调整为您自己的设置:http://connectionstrings.com/sql-server-2008

答案 1 :(得分:0)

几年前我做了类似的访问。您需要查看传递ODBC查询。这将允许您从Access执行SQL存储过程。

请看这个链接: http://support.microsoft.com/kb/303968

我不确定你将如何获得通过的参数,但我相信如果你谷歌“通过odbc查询访问存储过程参数”你会发现一些提示。对不起,我的记忆对于确切的细节有点褪色。