简体   繁体   English

Ms-Access打开源自SQL Server存储过程的查询

[英]Ms-Access Open a Query Sourced from SQL Server stored procedure

I have a SQL Server stored procedure which takes some parameters and returns records. 我有一个SQL Server存储过程,它接受一些参数并返回记录。 The front end application is MS Access 2003. I have a form with some filter controls and an execute button. 前端应用程序是MS Access 2003.我有一个带有一些过滤器控件和一个执行按钮的表单。

When a user clicks the button, I want my VBA code to call the stored procedure with the provided parameters and then display the results in a grid using Docmd.OpenQuery or Docmd.OpenTable . 当用户单击该按钮时,我希望我的VBA代码使用提供的参数调用存储过程,然后使用Docmd.OpenQueryDocmd.OpenTable在网格中显示结果。

Currently, I'm using a pass-through query to open a recordset with the results of the stored procedure, then looping through the recordset to insert each record into a temporary table, which I then open using DoCmd.OpenTable . 目前,我正在使用传递查询来打开包含存储过程结果的记录集,然后循环遍历记录集以将每个记录插入临时表,然后使用DoCmd.OpenTable打开该表。

This seems unnecessarily complicated. 这似乎不必要地复杂化。 I would like to avoid the temporary table completely and simply display the records in a grid using DoCmd.OpenQuery if possible. 我想完全避免临时表,如果可能的话,只需使用DoCmd.OpenQuery在网格中显示记录。 If that is not possible, I would like to find a way to insert the records from the recordset into the temp table in a single step, instead of looping the recordset. 如果那是不可能的,我想找到一种方法将记录集中的记录一步插入临时表,而不是循环记录集。

EDIT: I had previously tried opening the pass-through query using DoCmd.OpenQuery as suggested, but had received "Run time error 3270 - Property not found" when the DoCmd.OpenQuery line was executed. 编辑:我之前尝试使用DoCmd.OpenQuery按照建议打开传递查询,但在执行DoCmd.OpenQuery行时收到“运行时错误3270 - 找不到属性”。 This led me to believe that opening the pass through directly was not possible. 这让我相信直接打开通行证是不可能的。 Here is a snippet of that version of my code: 这是我的代码版本的片段:

sql = "EXEC dbo.rptContractorBidSummary " & IIf(frmClosedProjectWindow.Value = 1, "1", IIf(frmClosedProjectWindow.Value = 2, "2", "NULL"))
Set qdef = CurrentDb.QueryDefs("qryContractorBidSummary")
qdef.Connect = "ODBC;DRIVER=SQL Server;SERVER=" & Cconst.SERVER_NAME & ";DATABASE=" & stDatabase & ";UID=" & stUsername & ";PWD=" & stPassword
qdef.sql = sql
qdef.ODBCTimeout = 1000
qdef.ReturnsRecords = True
Set qdef = Nothing
DoCmd.OpenQuery "qryContractorBidSummary"

EDIT: I was finally able to get this approach to work. 编辑:我终于能够使用这种方法。 The code is fine. 代码很好。 The problem turned out to be that I had not set the permissions on the stored procedure correctly in the back end. 问题是我没有在后端正确设置存储过程的权限。 Thanks for the help! 谢谢您的帮助!

You should be able to open a saved pass-through query with DoCmd.OpenQuery . 您应该能够使用DoCmd.OpenQuery打开已保存的传递查询。 If you need to specify parameter values before opening the query you could modify the .SQL property of its QueryDef object, eg, something like this (untested): 如果你需要在打开查询之前指定参数值,你可以修改其QueryDef对象的.SQL属性,例如,像这样(未经测试):

Set cdb = CurrentDb
Set qdf = cdb.QueryDefs("yourPassThroughQuery")
qdf.SQL = "EXEC yourStoredProcedure " & yourParameterValue
Set qdf = Nothing
DoCmd.OpenQuery "yourPassThroughQuery"

Why don't you just open the passthru with docmd.openquery ?? 你为什么不用docmd.openquery打开passthru? I use that regularly. 我经常使用它。 You can also set the passthru as the source of a form or a report. 您还可以将passthru设置为表单或报表的来源。 Just before calling it you alter its .SQL to match your criteria. 在调用它之前,你改变它的.SQL以符合你的标准。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM