vba 访问:当子报表没有数据时在子报表中显示文本框控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5581035/
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:58:17  来源:igfitidea点击:

Access: Display Textbox Control In Sub-report when it has No Data

ms-accessvbams-access-2007

提问by Rick

In a subreport I created a sub on detail_format event that will display a text when there is no data returned.

在子报表中,我在 detail_format 事件上创建了一个子报表,当没有返回数据时,它将显示一个文本。

‘Code in sub-report
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

If Me.Report.HasData Then
    Me.Label43.Visible = True
    Me.txtNotEntered.Visible = False
Else
    Me.Label43.Visible = True
    Me.txtNotEntered.Visible = True
End If

End Sub

It works fine on the subreport when run alone. When I run the main report it doesn't trigger.

单独运行时,它在子报表上运行良好。当我运行主报告时,它不会触发。

I added the same code in the main report to see if it would work. It runs through the lines of code but still cannot see the txtNotEntered textbox control.

我在主报告中添加了相同的代码,看看它是否有效。它通过代码行运行,但仍然看不到 txtNotEntered 文本框控件。

‘Code in main report
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

If Me!rptResults_Comments.Report.HasData Then
    Me!rptResults_Comments.Report.Label43.Visible = True
    Me!rptResults_Comments.Report.txtNotEntered.Visible = False
Else
    Me!rptResults_Comments.Visible = True
    Me!rptResults_Comments.Report.Label43.Visible = True
    Me!rptResults_Comments.Report.txtNotEntered.Visible = True
End If

End sub

I am using MS Access 2003.

我正在使用 MS Access 2003。

回答by mwolfe02

Since the subreport is bound to the main report, the subreport itself will not be shown if there is no data to connect it to the main report. You can see this better by setting the background color of the subreport's detail section to red, or any other non-white color.

由于子报表绑定到主报表,如果没有数据将其连接到主报表,子报表本身将不会显示。通过将子报表的详细信息部分的背景颜色设置为红色或任何其他非白色,您可以更好地看到这一点。

One workaround is to move your txtNotEnteredcontrol to the main report and put it "under" the subreport control (using Send to Back). Then set your subreport control's Can Shrink property to True.

一种解决方法是将您的txtNotEntered控件移至主报表并将其置于子报表控件的“下方”(使用“置后”)。然后将子报表控件的 Can Shrink 属性设置为 True。

Then, when there is data in the subreport you will see the subreport and it will cover the txtNotEnteredcontrol. When there is no data, the subreport will shrink out of the way and you will be able to see the txtNotEnteredcontrol.

然后,当子报表中有数据时,您将看到子报表,它将覆盖txtNotEntered控件。当没有数据时,子报表将缩小,您将能够看到txtNotEntered控件。

One advantage to this approach is that it requires no code. Just leave the txtNotEnteredVisible property set to True. The shrinking of the subreport will take care of revealing it when appropriate.

这种方法的一个优点是它不需要代码。只需将txtNotEnteredVisible 属性设置为 True。子报表的缩小将在适当的时候显示出来。

回答by TheOtherTimDuncan

In addition to the answer from mwolfe02, I would suggest instead of using a label and VB code use a textbox with the following expression:

除了 mwolfe02 的答案之外,我建议不要使用标签和 VB 代码,而是使用具有以下表达式的文本框:

=IIf([HasData],"","No data found")

I use this on all my reports. An expression like

我在所有报告中都使用它。像这样的表达

=IIf([rptResults_Comments].[Report].[HasData],","No data found")

should work as well. I didn't test the latter expression, so I'm not sure if [Report] is needed or not.

也应该工作。我没有测试后一个表达式,所以我不确定是否需要 [Report]。