1

在 MS Access 2007 中,我遇到了一个问题,即由于打印机限制,不同的用户希望在报告标题上设置不同的颜色。以编程方式,我已经建立了一种方法来更改每个报告包含的报告和页眉的颜色;但是,大约一半的报告包含组级标题。我的问题是如何识别 .AllReports 集合中报告的组级标题实例?

Private Sub ChangeHeaderColor(ByVal blnIsObjectLoaded As Boolean, _
                              ByVal intCounter As Integer, _
                              ByVal strObjectName As String, _
                              ByVal strObjectType As AcObjectType, _
                              ByVal lngHexColor As Long)

On Error GoTo OpenAllReports_Error

Dim rpt As Report

If blnIsObjectLoaded = False Then
    DoCmd.OpenReport strObjectName, acDesign, , , acHidden
    If intCounter < Reports.Count Then
        If IsNull(Reports.Item(intCounter).Section(acGroupLevel1Header)) Then
            Set rpt = Reports.Item(intCounter)
            rpt.Section(acGroupLevel1Header).BackColor = lngHexColor
        End If
    End If
End If

...

问题是定义正确的 If 语句以仅标识具有 GroupLevel 标题的那些报告。除了捕获 2462 运行时错误之外,我不知道任何方法 - “您输入的部分编号无效。”

4

1 回答 1

0

我决定只捕获类似于为“检测对象是否存在”提供的解决方案的错误代码。如需更多信息,请参阅第 2 版 Access Cookbook 中的问题 7.1。

`Function ReportSectionExists(ByVal intIndex As Integer, ByVal intSection As Integer) As Boolean

Dim strName As String

On Error GoTo HandleErr

strName = Application.Reports.Item(intIndex).Section(intSection).Name

ReportSectionExists = True

HandleErr:
'No error, the Report.Section exists.
If Err.Number = 0 Then
    ReportSectionExists = True

'Error: The section number you entered is invalid.  The Report.Section does not exist.
ElseIf Err.Number = 2462 Then
    ReportSectionExists = False

'Unexpected error.
Else
    Dim ReportErr As Integer
    ReportErr = MsgBox(Err.Number & " - " & Err.Description & " The Section for this Report will be recoded as not availabele.  Do you want to continue?", vbYesNo)
    If ReportErr = 6 Then
        ReportSectionExists = False
    Else
        Exit Function
    End If
End If

End Function
于 2013-10-09T18:19:39.117 回答