sql-server-2008 - 如何通过传递参数使用Ms-Access形式执行Sql服务器存储过程

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

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

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

最佳答案

这取决于您的存储过程输出类型,但基本上,假设您想将存储过程 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-2008 - 如何通过传递参数使用Ms-Access形式执行Sql服务器存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9868362/

相关文章:

sql - 使用随机字段更新表

sql - 数据库不存在。确保名称输入正确

SQL CTE 和 ORDER BY 影响结果集

sql - 将 LIMIT 和 OFFSET 应用于 MS SQL Server 2008 查询

excel - 将各种文件大小转换为字节

vba - 通过 VBA 中的 Excel 查询从 Access 执行查询

sql - 我想显示所有具有指定列名的表

sql - 检查更新是否已在 ACCESS 中进行

ms-access - 如何在 Access 2007 的宏中自动执行已保存的导入?

sql - 将两个 Access 表中的值相乘