[英]How do you pass parameters into an inner WHERE clause of an Access query if the dependent report needs to be loaded from multiple forms?
My question is very similar to this one, and I would have followed up by commenting on the latter but I don't have enough rep yet to comment, so... 我的问题与这个问题非常相似,我会跟进评论后者,但我还没有足够的代表发表评论,所以...
What if I want my parameterized report to be able to be loaded by various different forms? 如果我希望参数化报告能够以各种不同的形式加载,该怎么办? Having a reference to a form directly in the query precludes this, does it not?
直接在查询中引用表单会排除这种情况,不是吗? It seems like yucky tight coupling to me.
看起来好像与我紧密相连。
Is there no way to feed parameters into a query without having the query making reference to a form? 如果没有查询引用表单,是否无法将参数提供给查询? (And, like in the linked question above, the relevant WHERE clause in embedded in a nested SELECT, so feeding a WHERE clause into DoCmd doesn't do the trick.)
(并且,与上面的链接问题一样,嵌入在嵌套SELECT中的相关WHERE子句,因此将WHERE子句提供给DoCmd并不起作用。)
Look at the FilterName and WhereCondition arguments of the OpenReport method. 查看OpenReport方法的FilterName和WhereCondition参数。 Alternatively, you could set the report's RecordSource to a SQL string that you build using the report's OpenArgs;
或者,您可以将报表的RecordSource设置为使用报表的OpenArgs构建的SQL字符串; when you call OpenReport, you pass the names of the form and of the controls that you want to use for parameters to the OpenArgs parameter, and then you have access to this data through the report's OpenArgs property.
当您调用OpenReport时,将表单的名称和要用于参数的控件的名称传递给OpenArgs参数,然后您可以通过报表的OpenArgs属性访问此数据。
I have a former colleague who assiduously avoided using SQL strings for RecordSource, because "compiled queries are faster". 我有一位前同事,他非常谨慎地避免在RecordSource中使用SQL字符串,因为“编译的查询更快”。 However, I've never found a case where the difference in speed was enough to notice.
但是,我从来没有发现速度差异足以引起注意的情况。
what are you coding this in C# ..?
if so have a Switch statement perhaps to check the form type
or what ever it is you are passing and
dynamically build the where clause based on that should not be to difficult
you can still use code to do this assuming you know VB
or create a Macro do utilize the vb script code behind
Here's the solution I've arrived at. 这是我到达的解决方案。 It has some flaws but it lets me store my queries in the usual way in Access while at the same time keeping references to specific forms out of my queries:
它有一些缺陷,但它允许我以通常的方式在Access中存储我的查询,同时保持对我的查询中的特定表单的引用:
I've created a module called Param, and it has the following code: 我创建了一个名为Param的模块,它具有以下代码:
Option Compare Database
Private m_params As Collection
Public Function ParamLookup(paramName As String) As Variant
If Not m_params Is Nothing Then
ParamLookup = m_params(paramName)
End If
End Function
Public Function FeedParamsTo(reportName As String, theParams As Collection)
Set m_params = theParams
If Application.CurrentProject.AllReports(reportName).IsLoaded Then
DoCmd.Close acReport, reportName
End If
DoCmd.OpenReport reportName, acViewPreview
End Function
Then in the form that contains my input controls, I have, for example a button with the following click handler: 然后在包含我的输入控件的表单中,我有一个带有以下单击处理程序的按钮:
Private Sub Button_Click()
Dim params As Collection
Set params = New Collection
params.Add Me!AnInputControl, "param1"
params.Add Me!AnotherInputControl, "param2"
Param.FeedParamsTo "nameOfSomeReport", params
End Sub
Finally, in the query that the report uses I can refer to the parameters param1 and param2 using the ParamLookup function, like so: 最后,在报告使用的查询中,我可以使用ParamLookup函数引用参数param1和param2,如下所示:
SELECT * FROM someTable WHERE someColumn = ParamLookup("param1") AND someOtherColumn = ParamLookup("param2")
Some drawbacks: 一些缺点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.