有效利用 MS Access 中的计算
这是我觉得我应该已经知道的事情,但不要...
在 MS Access 中,如果查询由 n 个计算字段组成(例如 calc_1、calc_2、.... calc_ n),但我只想在特定的表单或报告中使用它们的子集 - 例如 calc_x、calc_y 和 calc_z - Access 将计算所有运行SELECT calc_x, calc_y, calc_z FROM myquery
时进行n次计算,然后返回我想要的,或者它是否足够聪明,只计算calc_x em>、calc_y 和 calc_z?
就我而言,我使用的是 Access 2003,但想必所有版本的答案都是相同的。
This is the kind of thing I feel I should already know, but don't...
In MS Access, if a query consists of n calculated fields (say calc_1, calc_2, .... calc_n) but I only want to use a subset of them in a particular form or report - say calc_x, calc_y and calc_z - would Access calculate all n calculations when running SELECT calc_x, calc_y, calc_z FROM myquery
and then return the ones I want, or would it be smart enough to only calculate calc_x, calc_y and calc_z?
In my case I'm using Access 2003 but presumably the answer is the same for all versions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
访问是智能的,不会计算不可见的字段。
尝试使用 Access 2003
Access is smart and don't calculate not-visible fields.
Tried with Access 2003
和@David 一样,我一开始也不相信@Andreas 的回答。所以我自己测试了一下如下。我创建了以下函数:
然后我创建了一个名为“Dummy”的表,其中包含一个名为“ID”的字段。我创建了一个表单并使用以下内容作为表单的 RecordSource:
我添加了一个文本框控件,其 ControlSource 为
ShowInForm
。然后我打开表单并在立即窗口中得到这个:
然后我回到 RecordSource 并在数据表视图中预览它并得到这个:
我不确定为什么“ShowInForm”表达式在表单中被计算三次,但是似乎很清楚未使用的字段“HideFromForm”不会被评估。
为了解决@HansUp的评论,我返回并保存了一个名为“Qry”的查询:
然后将表单RecordSource更改为:
当我打开表单时,这产生了与之前相同的结果(即3行
1 ShowInForm)。有趣的是,当我在数据表视图中打开 RecordSource 时,我得到了以下信息:
换句话说,它对 ShowInForm 字段求值两次。据推测,一次在“Qry”中,另一次在 RecordSource 查询中。
最终结果仍然是对@Andreas答案的确认。
Like @David, I did not believe @Andreas's answer at first. So I tested it myself as follows. I created the following function:
Then I created a table named "Dummy" with a single field named "ID". I created a form and used the following as the form's RecordSource:
I added a single textbox control with a ControlSource of
ShowInForm
.I then opened the form and got this in the immediate window:
I then went back to the RecordSource and previewed it in Datasheet view and got this:
I'm not sure why the "ShowInForm" expression is evaluated three times in the form, but it seems pretty clear that the unused field, "HideFromForm", does not get evaluated.
To address @HansUp's comment, I went back and saved a query named "Qry":
Then changed the form RecordSource to:
This produced the same result as before when I opened the form (ie, 3 lines of
1 ShowInForm
). Interestingly, when I opened the RecordSource in datasheet view I got this:In other words, it evaluated the ShowInForm field twice. Presumably, once in "Qry" and again in the RecordSource query.
The end result is still a confirmation of @Andreas's answer.
如果您在报表或表单记录源的 SELECT 子句中包含计算字段,它将在检索时为每一行计算它们。
如果您将它们排除在 SELECT 之外并仅将计算包含在报表/表单上控件的 ControlSource 属性中,那么您所说的是正确的。
此外,如果您对计算进行任何排序/分组或对其设置条件,则将计算所有行。
因此,在此记录源中:
...对于检索的每一行,无论结果是否在表单或报表中使用,都将执行这两个计算。
如果要将计算延迟到最后可能的时刻,请不要将它们包含在 SQL 记录源中,而只需将它们用作显示计算的控件的 ControlSource。然而,这有一个缺点,在许多情况下,您会在屏幕上看到计算结果。
编辑:
这似乎可能不正确,但我觉得这里发生的事情并没有被 @mwolfe02 的答案完全解释。我将其留在这里以供进一步讨论。
If you include the calculated fields in the SELECT clause of your report or form's recordsource, it will calculate them for each row AS IT IS RETRIEVED.
If you leave them out of the SELECT and include the calculations only in the ControlSource properties of controls on your report/form, then what you say is true.
Also, if you do any sorting/grouping on the calculations or put criteria on them, all rows will be calculated.
Thus, in this recordsource:
...for each row that is retrieved, both calculations will be executed regardless of whether or not the result is used in the form or report.
If you want to delay calculations to the last possible moment, do NOT include them in the SQL recordsource, but just use them as the ControlSources of the controls that are displaying the calculations. However, this has the downside that you'll see the calculations painting onscreen in many cases.
EDIT:
It seems this may not be correct, but I feel there's something going on here that is not completely explained by @mwolfe02's answer. I'll just leave this here for further discussion.
我把这样的计算放在服务器端......计算列很棒。
我想 Access 终于在 2007 年获得了这个功能,我不知道没有它人们是如何生存的。
I put calculations like this on the server side.. computed columns are awesome.
I think that Access finally got this feature in 2007, I don't know how people ever survived without it.