Report 物件 (Access)Report object (Access)

Report 物件是指特定的 Microsoft Access 報表。A Report object refers to a particular Microsoft Access report.

註解Remarks

Report物件是**Reports** 集合,也就是所有目前開啟的報告集合的成員。A Report object is a member of the Reports collection, which is a collection of all currently open reports. Reports 集合內個別報表是從零開始編製索引。Within the Reports collection, individual reports are indexed beginning with zero. 您可以參閱下列的方式參照至報表的名稱或索引參照它集合內的 Reports 集合中個別的 Report 物件。You can refer to an individual Report object in the Reports collection either by referring to the report by name, or by referring to its index within the collection. 如果報表名稱包含空格,名稱必須圍繞括號 ([])。If the report name includes a space, the name must be surrounded by brackets ([ ]).

語法Syntax 範例Example
ReportsreportnameReports!reportname Reports !用Reports!OrderReport
Reports![報表名稱]Reports![report name] Reports![OrderReport]Reports![Order Report]
報告("reportname")Reports("reportname") Reports("OrderReport")Reports("OrderReport")
報告(索引)Reports(index) Reports(0)Reports(0)

注意

[!注意事項] 報表 的每個物件已 Controls 集合,其中含有報表上的所有控制項。Each Report object has a Controls collection, which contains all controls on the report. 您可以在報表上的控制項,請參閱 [依用隱喻或明確地參照 Controls 集合。You can refer to a control on a report either by implicitly or explicitly referring to the Controls collection. 您的程式碼將會隱含地參照 Controls 集合如果更快。Your code will be faster if you refer to the Controls collection implicitly. 下列範例顯示兩個可能會參考 NewData控制項 在報表上的方式。The following examples show two of the ways you might refer to a control named NewData on a report called OrderReport.

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

範例Example

下列範例會示範如何使用報表的NoData事件來防止報表開啟時要顯示沒有資料。The following example shows how to use the NoData event of a report to prevent the report from opening when there is no data to be displayed.

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事件。The following example shows how to use the Page event to add a watermark to a report before it is printed.

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屬性設定。The following example shows how to set the BackColor property of a control based on its value.

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

下列範例會示範如何設定報表以顯示進度列的格式。The following example shows how to format a report to show progress bars. 此範例使用兩個矩形控制項、 boxInsideboxOutside、 建立 AvgOfRating的值為基礎的進度列。The example uses a pair of rectangle controls, boxInside and boxOutside, to create a progress bar based on the value of AvgOfRating. 只有當報表在 預覽列印] 模式中開啟或列印時的進度列是可見的。The progress bars are visible only when the report is opened in Print Preview mode or it is printed.

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

事件Events

方法Methods

屬性Properties

請參閱See also

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.