vba 更改 Microsoft Access 报表组标题的可见性

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

Change the Visibility of Group Headers Of a Microsoft Access Report

ms-accessvbaonloadms-access-2010

提问by m3z

I'm using Access 2010 to create several reports and i can't figure out how to hide a group heading if its empty.

我正在使用 Access 2010 创建多个报告,但我不知道如何隐藏组标题(如果为空)。

Imagine I have a table such as (n.b. i know nothing about cars, its just an example):

想象一下,我有一个表格,例如(注意我对汽车一无所知,这只是一个例子):

H1  | H2       | H3     | H4       | DATA1   | DATA2   | DATA3
car | chassis  | engine | pistons  | data_a1 | data_a2 | data_a3
car | chassis  | engine | pistons  | data_b1 | data_b2 | data_b3
car | chassis  | engine | pistons  | data_c1 | data_c2 | data_c3
car | chassis  | engine | cylinder | data_a1 | data_a2 | data_a3
car | chassis  | engine | cylinder | data_b1 | data_b2 | data_b3
car | interior |        | seats    | data_a1 | data_a2 | data_a3
car | interior |        | seats    | data_b1 | data_b2 | data_b3

I have got 3 group headers of columns H1,H2 and H3 in that order. The problem i have is, when H3 contains no text (i.e. "" (I think access evaluates that as null)) i want to hide the group header for H3 or at least make it so it takes no space on the report. Currently wherever H3 is blank, the report still contains a blank row.

我有 3 个 H1、H2 和 H3 列的组标题。我遇到的问题是,当 H3 不包含任何文本(即“”(我认为访问将其评估为空))时,我想隐藏 H3 的组标题,或者至少将其设置为不占用报告空间。目前,只要 H3 为空白,报告仍包含一个空白行。

I've tried this and similar in GroupHeader3's Paint and or Print events

我已经在 GroupHeader3 的 Paint 和或 Print 事件中尝试过这个和类似的

If IsNull([H3]) Then
    Me.GroupHeader3.Height = 0
    Me.GroupHeader3.BackColor = vbRed
    Me.GroupHeader3.Visible = False
Else
    Me.GroupHeader3.Height = 5
    Me.GroupHeader3.BackColor = vbGreen
End If

The idea seems to work, that is if i comment out the Visible=False line i get a red or a green background in the right places, however it completely ignores the height option. If i put the Visible=False on it complains that it can't be put in the on Paint event.

这个想法似乎有效,也就是说,如果我注释掉 Visible=False 行,我会在正确的位置获得红色或绿色背景,但是它完全忽略了高度选项。如果我把 Visible=False 放在它上面会抱怨它不能放在 on Paint 事件中。

I've also tried putting similar code in GroupHeader2's paint event but to no avail. The closest I've got it just sits there and flickers.

我也尝试将类似的代码放在 GroupHeader2 的绘制事件中,但无济于事。我得到的最近的它只是坐在那里闪烁。

I'm kinda new to vba so i might be missing something obvious, but at the moment I'm not really sure how to proceed so any help would be much appreciated.

我对 vba 有点陌生,所以我可能会遗漏一些明显的东西,但目前我不确定如何进行,因此非常感谢任何帮助。

EDIT For clarity:

编辑为清楚起见:

The report is laid out a bit like so:

报告的布局有点像这样:

H1
    H2
          H3
               H4
                    DATA1       DATA2       DATA3

So with the example data above it would appear as:

因此,对于上面的示例数据,它将显示为:

car
    chassis
              engine
                      pistons
                              data_a1    data_a2    data_a3
                              data_b1    data_b2    data_b3
                              data_c1    data_c2    data_c3
                      cylinder
                              data_a1    data_a2    data_a3
                              data_b1    data_b2    data_b3
    interior

                      seats
                              data_a1    data_a2    data_a3
                              data_b1    data_b2    data_b3

Its the blank line (group header 3) between interior and seats that i want to avoid. Thanks

它是我想避免的内部和座位之间的空行(组标题 3)。谢谢

回答by Olivier Jacot-Descombes

You are showing us column headers not group headers. What sense does it make to change the hight of H3 if the other headers are still here? You can't save space that way.

您向我们展示的是列标题而不是组标题。如果其他标题还在这里,更改 H3 的高度有什么意义?你不能那样节省空间。

Probably you want to hide the whole column H3 by moving the columns at the right of H3 to the left by the width of H3. There is no easy way to do that. You will have to calculate the positions of the text fields and lables involved and change their "Left" position accordingly in a OnFormat event.

可能您想通过将 H3 右侧的列向左移动 H3 的宽度来隐藏整个列 H3。没有简单的方法可以做到这一点。您必须计算所涉及的文本字段和标签的位置,并在 OnFormat 事件中相应地更改它们的“左”位置。

EDIT:

编辑:

Use the Cancel parameter of the Format event procedure of the group headers:

使用组标题的 Format 事件过程的 Cancel 参数:

Private Sub GroupHeader0_Format(Cancel As Integer, FormatCount As Integer)
    Cancel = IsNull(Me!H0)
End Sub

Since the VBA code never runs in design mode, you will only see its effect in the print preview and when printing.

由于 VBA 代码永远不会在设计模式下运行,因此您只能在打印预览和打印时看到其效果。

回答by PhillipOReilly

This is an older post, but Mr. Jacot-Descomes' posthelped me figure out some conditional formatting for headers in Access. I needed a different header after the first page.

这是一篇较旧的帖子,但Jacot-Descomes 先生的帖子帮助我找出了 Access 中标题的一些条件格式。在第一页之后我需要一个不同的标题。

Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
  Dim reportPageNumber As Integer
  Dim pageHeaderHeight As Double

  pageHeaderHeight = Me.PageHeaderSection.Height

  reportPageNumber = Me.Page
  Me.PageHeaderSection.Height = 0
  PageHeaderSection.Visible = False

  If reportPageNumber > 1 Then
    Me.PageHeaderSection.Visible = True
    Me.PageHeaderSection.Height = pageHeaderHeight
  End If
End Sub