vba 动态调整报表详细信息区域的大小

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

Dynamically resize a report Detail area

ms-accessvbams-access-2003

提问by FrustratedWithFormsDesigner

Is it possible to resize the Detail area of a report dynamically, in MS Access?

是否可以在 MS Access 中动态调整报告的详细信息区域的大小?

I have a report and the Detail region has 2 rows, I'd like one of them to be "optional" - when there is no data it should not display and the Detail region should only be as tall as the top row of data.

我有一个报告,详细信息区域有 2 行,我希望其中的一行是“可选的”——当没有数据时,它不应该显示,并且详细信息区域应该只与数据的顶行一样高。

I have code like this:

我有这样的代码:

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    Dim reduceHeight As Integer
    reduceHeight = Me.Label83.Height


    If IsNull(Me.data_1) Then
        Me.data_1.Visible = False
        Me.Label83.Visible = False
    Else
        Me.data_1.Visible = True
        Me.Label83.Visible = True
    End If

    If IsNull(Me.data_1) 
        Detail.Height = Detail.Height - reduceHeight
    End If

End Sub

And it works as far as makign the label and text box conditionally visible, but I can't get the Detail region to shrink when the bottom line is hidden. I didset the CanShrinkproperty for the Detail to True, but it doesn't shrink.

它可以使标签和文本框有条件地可见,但是当隐藏底线时,我无法缩小细节区域。我确实CanShrinkDetail的属性设置为True,但它不会缩小。

回答by Igor Turman

Set CanShrinkfor your controls too (e.g. textboxes):

也为您的控件设置CanShrink(例如文本框):

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

回答by Clon

Hi: I understand that you have labels. Since labels cannot shrink, you'll need to write some code.

你好:我知道你有标签。由于标签无法缩小,因此您需要编写一些代码。

Dynamically playing with reports is a tricky task. When you change a section's height or width all the controls must keep inside the new area, otherwise you'll have problems. When you move a control, it must keep inside the section's area, otherwise you'll have problems. Also, you should disable Autoshrink for the section.

动态地处理报告是一项棘手的任务。当您更改部分的高度或宽度时,所有控件都必须保留在新区域内,否则您会遇到问题。当您移动控件时,它必须保持在该部分的区域内,否则您会遇到问题。此外,您应该禁用该部分的自动收缩。

Now, this is an example. You should modify it to meet your requirements. Here goes the code for the report:

现在,这是一个例子。您应该修改它以满足您的要求。这是报告的代码:

Option Compare Database
Option Explicit

Private twipsPerLine As Integer     ' The height of a line in your report
Private detailHeight As Integer     ' The height of your detail section
Private vPos As Integer             ' The vertical position of the control
                                'following the one you want to hide

Private Sub Report_Open(Cancel As Integer)
    ' Set the values
    vPos = data_2.Top
    twipsPerLine = data_2.Top - data_1.Top
    detailHeight = Me.Detail.Height
End Sub

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

    If (IsNull(Me.data_1.Value)) Then
        ' First, you hide the controls
        Me.data_1.Visible = False
        Me.data_1_label.Visible = False
        ' Then, you set the position of the rest of the controls (up)
        data_2_label.Move data_2_label.Left, vPos - twipsPerLine
        data_2.Move data_2.Left, vPos - twipsPerLine
        ' Finally, you shrink the detail section height
        Me.Detail.Height = detailHeight - twipsPerLine
    Else
        ' First, you show the controls
        Me.data_1.Visible = True
        Me.data_1_label.Visible = True
        ' Then, you reset the section height
        Me.Detail.Height = detailHeight
        ' Finally, you reset the position of the rest of the controls
        data_2_label.Move data_2_label.Left, vPos
        data_2.Move data_2.Left, vPos
    End If

End Sub

This approximation gives you complete control over your report and works even if you have labels, images or whatever on it.

这种近似使您可以完全控制您的报告,即使您有标签、图像或其他任何内容也能正常工作。

enter image description here

在此处输入图片说明