我需要开发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/