简体   繁体   English

如果需要从多个表单加载依赖报表,如何将参数传递到Access查询的内部WHERE子句?

[英]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: 一些缺点:

  • It's a global module, so there is only one set of parameters able to be read at a time. 它是一个全局模块,因此一次只能读取一组参数。 If you load two reports, the parameters for the first report are overwritten and a refresh of the form may result in weird errors. 如果加载两个报告,则会覆盖第一个报告的参数,刷新表单可能会导致奇怪的错误。 Of course, it should be possible to extend the idea here to a class and have one instance created per form, but I don't need anything that fancy for the moment. 当然,应该可以将这里的想法扩展到一个类,并为每个表单创建一个实例,但我现在不需要任何花哨的东西。
  • If you open the report in Report view without using the button, and hence the FeedParamsTo method, (eg. from the Access Objects panel) then the ParamLookups fail and the report shows up with no records 如果在不使用按钮的情况下在报表视图中打开报表,从而打开FeedParamsTo方法(例如,从“访问对象”面板),则ParamLookup会失败并且报表显示没有记录

暂无
暂无

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

相关问题 如何在不相互依赖的where子句中添加多个参数 - How to add multiple parameters in where clause not dependent on each other 内部查询where子句取决于外部查询属性 - inner query where-clause dependent on outer query attribute 如何在Access表单中为Access报表上的嵌入式查询设置参数? - How do I set parameters for an embedded query on an Access report from an Access form? 如何基于内部查询添加where子句 - How to add where clause based on inner query MySql-如何与OR一起使用多个WHERE子句 - MySql -How do you use multiple WHERE clause with OR as well 如何在INNER JOIN之前执行WHERE子句 - How to do WHERE clause BEFORE INNER JOIN 如何在子查询的 WHERE 子句中使用来自 UNNEST 的多个值? - How do you use multiple values from UNNEST in WHERE clause of a subquery? Select 语句条件 WHERE 子句 - 如何在同一查询中添加不受 WHERE 子句影响的变量 - Select Statement Conditional WHERE Clause - How do you add a variable not affected by WHERE clause in same query 在Dapper Multiple Results中,当where子句取决于第一个查询的结果时,如何使第二行查询起作用? - In Dapper Multiple Results how do you get the second line query to work when the where clause depends upon the result of the first query? 如何从 SQL 服务器/SSMS 中的 VIEW 访问 UPDATE 查询的 OUTPUT 子句 - How do you access the OUTPUT clause of an UPDATE query from a VIEW in SQL Server / SSMS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM