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

时间:2011-07-29 18:29:07

标签: ms-access vba ms-access-2003

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

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

我有这样的代码:

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

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

2 个答案:

答案 0 :(得分:4)

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

enter image description here enter image description here

答案 1 :(得分:4)

嗨:我知道你有标签。由于标签不能缩小,您需要编写一些代码。

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

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

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

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

enter image description here