【问题标题】:how to execute Sql server stored procedure using Ms-Access form by passing parameters如何通过传递参数使用Ms-Access表单执行Sql server存储过程
【发布时间】:2012-04-09 17:37:34
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    这取决于您的存储过程输出类型,但基本上,假设您想将存储过程的结果 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

    【讨论】:

    • 我想使用访问表单将参数从访问传递到 sql server 存储过程,并且应该在 Access 中获得输出。请在详细步骤中提供帮助
    • 上面的代码有一些问题。 db 被定义了两次。查询拼写错误“CreateQeuryDef”。我认为 ReturnRecords 也应该是 ReturnsRecords
    • @ChrisNevill 代码已随您的 cmets 更新。感谢您指出问题。
    【解决方案2】:

    几年前我在访问中做过类似的事情。您需要查看通过 ODBC 查询。这将允许您从 Access 执行 SQL 存储过程。

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

    我不确定您将如何获得传递的参数,但我相信如果您在谷歌上搜索“通过 odbc 查询访问存储过程参数”,您会发现一些提示。抱歉,我对具体细节的记忆有些模糊。

    【讨论】:

    • 但是如何从 Access 表单连接 sql server 2008 以调用存储过程并传递参数。执行此操作的步骤是什么。我使用 2 个测试框(开始日期、结束日期)和一个按钮创建了表单。然后接下来如何将参数传递给Sql server stared 过程并获取输出
    • 您需要一个 ODBC 连接才能访问 SQL 转到此链接并阅读它accessexperts.net/blog/2011/07/29/…
    • 我在 controlpanel-> 管理工具中创建了 odbc 连接。我无法通过从 Access 传递参数并获取输出来弄清楚如何在 sql server 中移动到执行 Sp
    • 请试着指导我,我被吊死了
    • 首先你能创建一个到 SQL 服务器的 ODBC 连接吗?您是否尝试创建连接字符串?有关更多示例,请参阅此链接 support.microsoft.com/kb/892490 我在运行 Ubuntu 时无法访问我的计算机,因此目前无法直接指导您。您需要先使用 ODBC 创建连接,然后您可以通过此连接将查询传递到 SQL 服务器。
    猜你喜欢
    • 2015-06-18
    • 2017-09-06
    • 2013-09-18
    • 2021-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多