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

北战南征 提交于 2019-12-12 04:28:30

问题


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

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

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


回答1:


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

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

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




回答2:


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:

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



来源:https://stackoverflow.com/questions/5581035/access-display-textbox-control-in-sub-report-when-it-has-no-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!