Access Report 不允许我引用 VBA 中的字段,除非它本身就在报告中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3944691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:11:39  来源:igfitidea点击:

Access Report won't allow me to refer to a field in VBA unless it's on the report in its own right

ms-accessvba

提问by hawbsl

So, in an Access Form or Report a good way to show something dynamic on the screen that is more complex than =[UnitPrice]*[Quantity]is to drop into VBA.

因此,在 Access Form 或 Report 中,一种在屏幕上显示动态内容的好方法比=[UnitPrice]*[Quantity]放入 VBA更复杂。

e.g. in this simplified example, the underlying table for this Report has two fields ShowTaxand TaxRate. By making a TextBox's control source =GetTaxInfoI get to introduce some complexity in VBA:

例如,在这个简化的例子中,这个报告的基础表有两个字段ShowTaxTaxRate。通过制作 TextBox 的控件源,=GetTaxInfo我可以在 VBA 中引入一些复杂性:

Public Function GetTaxInfo() As String

    Dim result As String
    If Me!ShowTax = 0 Then
        result = "Tax included @ " & Me!TaxRate

    Else
        result = ""
    End If

    GetTaxInfo = result

End Function

OK, this works ... so long as I have a field somewhere else that refers to TaxRate. Otherwise it just prints #Error. It's as if it needs to preloadthe field before it can be used in VBA code. It isn't the end of the world because I can have a bunch of fields in the report all set to not be visible, but it's untidy.

好的,这行得通……只要我在其他地方有一个字段指的是 TaxRate。否则它只会打印#Error. 就好像它需要预先加载该字段,然后才能在 VBA 代码中使用它。这不是世界末日,因为我可以将报告中的一堆字段都设置为不可见,但它不整洁。

So is it the case that you can't refer to a field in VBA code backing a reportunless you have already referred to the field in the conventional way as a field baked into the report?

那么是不是您不能引用支持报表的 VBA 代码中的字段除非您已经以传统方式将该字段引用为报表中的字段?

I can't remember encountering this limitation before. Or is it that I have a corrupt report? (I have tried the usual compact/repair, export/reimport the Report etc)

我不记得以前遇到过这个限制。还是因为我有一份腐败的报告?(我尝试了通常的压缩/修复,导出/重新导入报告等)

Edit:

编辑:

the weird thing is ... now it's working again. And - I'm pretty sure - there is no control in the report. which is why I was thinking it was a corruption in the report.

奇怪的是......现在它又开始工作了。而且 - 我很确定 - 报告中没有控制权。这就是为什么我认为这是报告中的腐败。

回答by JeffO

You'll need a control on the form/report.

您需要对表单/报告进行控制。

If this is too messy, you could put the function in a Module and use in the RecordSource (based on a query). No sense burying all this logic in a report when it could be used in other places as well.

如果这太乱了,您可以将该函数放在模块中并在 RecordSource 中使用(基于查询)。当报告也可以在其他地方使用时,将所有这些逻辑都埋在报告中是没有意义的。

Public Function GetTaxInfo(ShowTax as Boolean, TaxRate as Single) As String 

    Dim result As String 
    If ShowTax = 0 Then 
        result = "Tax included @ " & TaxRate 

    Else 
        result = "" 
    End If 

    GetTaxInfo = result 

End Function 

Then the control is set to this field in this report and others.

然后将控件设置到此报表和其他报表中的此字段。