有效利用 MS Access 中的计算

发布于 2024-11-19 14:19:48 字数 400 浏览 2 评论 0原文

这是我觉得我应该已经知道的事情,但不要...

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

随梦而飞# 2024-11-26 14:19:48

访问是智能的,不会计算不可见的字段。
尝试使用 Access 2003

Access is smart and don't calculate not-visible fields.
Tried with Access 2003

很快妥协 2024-11-26 14:19:48

和@David 一样,我一开始也不相信@Andreas 的回答。所以我自己测试了一下如下。我创建了以下函数:

Function Watch(Val, Optional CalledFrom As String = "")
    Debug.Print Val, CalledFrom
    Watch = Val
End Function

然后我创建了一个名为“Dummy”的表,其中包含一个名为“ID”的字段。我创建了一个表单并使用以下内容作为表单的 RecordSource:

SELECT Watch([ID],"ShowInForm") AS ShowInForm, 
       Watch([ID],"HideFromForm") AS HideFromForm 
FROM Dummy;

我添加了一个文本框控件,其 ControlSource 为 ShowInForm

然后我打开表单并在立即窗口中得到这个:

 1            ShowInForm
 1            ShowInForm
 1            ShowInForm

然后我回到 RecordSource 并在数据表视图中预览它并得到这个:

 1            ShowInForm
 1            HideFromForm

我不确定为什么“ShowInForm”表达式在表单中被计算三次,但是似乎很清楚未使用的字段“HideFromForm”不会被评估。


为了解决@HansUp的评论,我返回并保存了一个名为“Qry”的查询:

SELECT Watch([ID],"ShowInForm") AS ShowInForm, 
       Watch([ID],"HideFromForm") AS HideFromForm
FROM Dummy;

然后将表单RecordSource更改为:

Select ShowInForm FROM Qry

当我打开表单时,这产生了与之前相同的结果(即3行1 ShowInForm)。有趣的是,当我在数据表视图中打开 RecordSource 时,我得到了以下信息:

 1            ShowInForm
 1            ShowInForm

换句话说,它对 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:

Function Watch(Val, Optional CalledFrom As String = "")
    Debug.Print Val, CalledFrom
    Watch = Val
End 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:

SELECT Watch([ID],"ShowInForm") AS ShowInForm, 
       Watch([ID],"HideFromForm") AS HideFromForm 
FROM Dummy;

I added a single textbox control with a ControlSource of ShowInForm.

I then opened the form and got this in the immediate window:

 1            ShowInForm
 1            ShowInForm
 1            ShowInForm

I then went back to the RecordSource and previewed it in Datasheet view and got this:

 1            ShowInForm
 1            HideFromForm

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":

SELECT Watch([ID],"ShowInForm") AS ShowInForm, 
       Watch([ID],"HideFromForm") AS HideFromForm
FROM Dummy;

Then changed the form RecordSource to:

Select ShowInForm FROM Qry

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:

 1            ShowInForm
 1            ShowInForm

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.

堇色安年 2024-11-26 14:19:48

如果您在报表或表单记录源的 SELECT 子句中包含计算字段,它将在检索时为每一行计算它们。

如果您将它们排除在 SELECT 之外并仅将计算包含在报表/表单上控件的 ControlSource 属性中,那么您所说的是正确的。

此外,如果您对计算进行任何排序/分组或对其设置条件,则将计算所有行。

因此,在此记录源中:

  SELECT Field1/Field2 As Ratio1, Field3/Field4 As Ratio2, FIeld1, Field2, Field3, Field4
  FROM MyTable;

...对于检索的每一行,无论结果是否在表单或报表中使用,都将执行这两个计算。

如果要将计算延迟到最后可能的时刻,请不要将它们包含在 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:

  SELECT Field1/Field2 As Ratio1, Field3/Field4 As Ratio2, FIeld1, Field2, Field3, Field4
  FROM MyTable;

...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.

峩卟喜欢 2024-11-26 14:19:48

我把这样的计算放在服务器端......计算列很棒。

我想 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文