【发布时间】:2012-04-09 17:37:34
【问题描述】:
我需要开发Access表单来传递参数来调用Sql Server存储过程。并且需要显示输出。 例如:我有两个参数“开始日期”和“结束日期” 从 Access 表单我需要传递这些日期并执行 Sql server 存储过程和输出应该显示..
请帮助我...请逐步帮助我,我是此 Access 应用程序的新手
【问题讨论】:
标签: sql-server-2008 ms-access-2007
我需要开发Access表单来传递参数来调用Sql Server存储过程。并且需要显示输出。 例如:我有两个参数“开始日期”和“结束日期” 从 Access 表单我需要传递这些日期并执行 Sql server 存储过程和输出应该显示..
请帮助我...请逐步帮助我,我是此 Access 应用程序的新手
【问题讨论】:
标签: sql-server-2008 ms-access-2007
这取决于您的存储过程输出类型,但基本上,假设您想将存储过程的结果 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
【讨论】:
几年前我在访问中做过类似的事情。您需要查看通过 ODBC 查询。这将允许您从 Access 执行 SQL 存储过程。
请看这个链接: http://support.microsoft.com/kb/303968
我不确定您将如何获得传递的参数,但我相信如果您在谷歌上搜索“通过 odbc 查询访问存储过程参数”,您会发现一些提示。抱歉,我对具体细节的记忆有些模糊。
【讨论】: