Access) (報表物件

Report 物件是指特定的 Microsoft Access 報表。

註解

Report物件是Reports集合的成員,這是所有目前開啟之報表的集合。 Reports 集合內個別報表是從零開始編製索引。 參照Reports 集合中的個別Report物件,方法是依名稱參照報表,或參照集合內的索引。 如果報表名稱包含空格,名稱必須圍繞括號 ([])。

語法 範例
報表reportname 報告!OrderReport
報表![報表名稱] Reports![OrderReport]
表 (「reportname」) 報告 (「OrderReport」)
表 (索引) Reports(0)

注意事項

[!注意事項] 報表 的每個物件已 Controls 集合,其中含有報表上的所有控制項。 透過隱含或明確參照 Controls 集合,在報表上參照 件。 如果您隱含地參照 Controls 集合,則程式碼會執行地更快速。 下列範例顯示兩個可能會參考 NewData控制項 在報表上的方式。

' Implicit reference. 
Reports!OrderReport!NewData
' Explicit reference. 
Reports!OrderReport.Controls!NewData

範例

下列範例示範如何使用報表的 NoData 事件,防止報表在沒有顯示任何資料時開啟。

Private Sub Report_NoData(Cancel As Integer)

    'Add code here that will be executed if no data
    'was returned by the Report's RecordSource
    MsgBox "No customers ordered this product this month. " & _
        "The report will now close."
    Cancel = True

End Sub

下列範例會示範如何將浮水印新增至報表會在列印之前使用 Page事件。

Private Sub Report_Page()
    Dim strWatermarkText As String
    Dim sizeHor As Single
    Dim sizeVer As Single

#If RUN_PAGE_EVENT = True Then
    With Me
        '// Print page border
        Me.Line (0, 0)-(.ScaleWidth - 1, .ScaleHeight - 1), vbBlack, B
    
        '// Print watermark
        strWatermarkText = "Confidential"
        
        .ScaleMode = 3
        .FontName = "Segoe UI"
        .FontSize = 48
        .ForeColor = RGB(255, 0, 0)

        '// Calculate text metrics
        sizeHor = .TextWidth(strWatermarkText)
        sizeVer = .TextHeight(strWatermarkText)
        
        '// Set the print location
        .CurrentX = (.ScaleWidth / 2) - (sizeHor / 2)
        .CurrentY = (.ScaleHeight / 2) - (sizeVer / 2)
    
        '// Print the watermark
        .Print strWatermarkText
    End With
#End If

End Sub

下列範例會示範如何將其值為基礎的控制項的 BackColor屬性設定。

Private Sub SetControlFormatting()
    If (Me.AvgOfRating >= 8) Then
        Me.AvgOfRating.BackColor = vbGreen
    ElseIf (Me.AvgOfRating >= 5) Then
        Me.AvgOfRating.BackColor = vbYellow
    Else
        Me.AvgOfRating.BackColor = vbRed
    End If
End Sub

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    ' size the width of the rectangle
    Dim lngOffset As Long
    lngOffset = (Me.boxInside.Left - Me.boxOutside.Left) * 2
    Me.boxInside.Width = (Me.boxOutside.Width * (Me.AvgOfRating / 10)) - lngOffset
    
    ' do conditional formatting for the control in print preview
    SetControlFormatting
End Sub

Private Sub Detail_Paint()
    ' do conditional formatting for the control in report view
    SetControlFormatting
End Sub

下列範例會示範如何設定報表以顯示進度列的格式。 此範例使用兩個矩形控制項、 boxInsideboxOutside、 建立 AvgOfRating的值為基礎的進度列。 只有當報表在 預覽列印] 模式中開啟或列印時的進度列是可見的。

Private Sub Report_Load()
    If (Me.CurrentView = AcCurrentView.acCurViewPreview) Then
        Me.boxInside.Visible = True
        Me.boxOutside.Visible = True
    Else
        Me.boxInside.Visible = False
        Me.boxOutside.Visible = False
    End If
End Sub

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    ' size the width of the rectangle
    Dim lngOffset As Long
    lngOffset = (Me.boxInside.Left - Me.boxOutside.Left) * 2
    Me.boxInside.Width = (Me.boxOutside.Width * (Me.AvgOfRating / 10)) - lngOffset
    
    ' do conditional formatting for the control in print preview
    SetControlFormatting
End Sub

事件

方法

屬性

請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應