none
Access VBA Reportのプロパティ設定について RRS feed

  • 質問

  • Access2016に於いてVBAによりReportの高さHeightをセットする方法を教えてください。

    因みに、

    デザインビューのプロパティシート「詳細」では「高さ」によってセットすることができます。

    また、

    Dim r As Report
    Set r = Application.CreateReport
    r.Width = 567 * 18
    r.Height = 567 * 11.4

    としてみると、Widthはセットできましたが、Heightのセットはできませんでした。

    よろしくお願いします。

    2017年8月17日 6:35

回答

  • ページヘッダ,ページフッタ,詳細といったセクションの高さを変えてその合計を高さとすることは可能です。
    #詳細で繰り返しが発生すれば合計の高さが変わりますが

    Option Compare Database
    Option Explicit
    
    Sub Test()
         Const width As Double = 567 * 18
         Const height As Double = 567 * 11.4
    
         Dim r As Report
         Set r = Application.CreateReport
         r.width = width
         
         Dim col As New Collection
         Dim sec As Section
         Dim sections() As Variant
         sections = Array(AcSection.acHeader, AcSection.acFooter, AcSection.acPageHeader, AcSection.acPageFooter, AcSection.acGroupLevel1Header, AcSection.acGroupLevel1Footer, AcSection.acGroupLevel2Header, AcSection.acGroupLevel2Footer)
         
         
         Dim hTemp As Double 'ページヘッダ以外の高さの合計
         Dim i As Integer
         For i = LBound(sections) To UBound(sections)
            Dim e As Long
            On Error Resume Next
            Set sec = r.Section(sections(i))
            e = Err.Number
            On Error GoTo 0
            If (e = 0) Then
                If (sec.Visible) Then
                      hTemp = hTemp + sec.height
                      Call col.Add(sections(i))
                End If
            End If
         Next
    
        If (height < hTemp) Then '詳細部の高さがマイナスになってしまう場合
            Dim v As Variant
            For Each v In col
                Set sec = r.Section(v)
                sec.height = 0 '詳細以外のセクションの高さを0に
            Next
            hTemp = 0
        End If
        
        Set sec = r.Section(AcSection.acDetail)
        sec.height = height - hTemp '詳細部の高さを設定
    
    End Sub

    個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)

    • 回答としてマーク bolbox 2017年8月17日 11:48
    2017年8月17日 9:26
  • gekkaきんありがとうございました。おかげで解決しました。
    • 回答としてマーク bolbox 2017年8月17日 11:48
    2017年8月17日 10:57

すべての返信

  • ページヘッダ,ページフッタ,詳細といったセクションの高さを変えてその合計を高さとすることは可能です。
    #詳細で繰り返しが発生すれば合計の高さが変わりますが

    Option Compare Database
    Option Explicit
    
    Sub Test()
         Const width As Double = 567 * 18
         Const height As Double = 567 * 11.4
    
         Dim r As Report
         Set r = Application.CreateReport
         r.width = width
         
         Dim col As New Collection
         Dim sec As Section
         Dim sections() As Variant
         sections = Array(AcSection.acHeader, AcSection.acFooter, AcSection.acPageHeader, AcSection.acPageFooter, AcSection.acGroupLevel1Header, AcSection.acGroupLevel1Footer, AcSection.acGroupLevel2Header, AcSection.acGroupLevel2Footer)
         
         
         Dim hTemp As Double 'ページヘッダ以外の高さの合計
         Dim i As Integer
         For i = LBound(sections) To UBound(sections)
            Dim e As Long
            On Error Resume Next
            Set sec = r.Section(sections(i))
            e = Err.Number
            On Error GoTo 0
            If (e = 0) Then
                If (sec.Visible) Then
                      hTemp = hTemp + sec.height
                      Call col.Add(sections(i))
                End If
            End If
         Next
    
        If (height < hTemp) Then '詳細部の高さがマイナスになってしまう場合
            Dim v As Variant
            For Each v In col
                Set sec = r.Section(v)
                sec.height = 0 '詳細以外のセクションの高さを0に
            Next
            hTemp = 0
        End If
        
        Set sec = r.Section(AcSection.acDetail)
        sec.height = height - hTemp '詳細部の高さを設定
    
    End Sub

    個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)

    • 回答としてマーク bolbox 2017年8月17日 11:48
    2017年8月17日 9:26
  • gekkaきんありがとうございました。おかげで解決しました。
    • 回答としてマーク bolbox 2017年8月17日 11:48
    2017年8月17日 10:57