列出报告及其描述

获取报告名称列表的各种方法: 询问
SELECT [Name] FROM MsysObjects
    WHERE ([Type] = -32764)
或者VBA
Dim rpt As AccessObject
Dim dB As Object

On Error GoTo Error_Handler

Set dB = Application.CurrentProject
For Each rpt In dB.AllReports

   Debug.Print rpt.Name
Next rpt
报告可以在“属性”(右键单击报告对象)下具有“描述”,但我无法使用代码进行访问。 我希望列表框显示与实际报告名称关联的用户友好报告名称。我正试图避免在此时创建一​​个单独的表来管理它。     
已邀请:
CurrentProject是一个ADO对象,我不知道如何从ADO中做你想做的事。您可以使用DAO来检索Description属性。
? CurrentDb.Containers("Reports").Documents("rptFoo").Properties("Description")
Foo Report
由于Description是用户定义的属性,因此在为其分配值之前它不存在。因此,下一行会触发rptLinks的错误3270(找不到属性),因为它没有分配描述。
? CurrentDb.Containers("Reports").Documents("rptLinks").Properties("Description")
你可以捕获这个错误。或者看看你是否可以使用Allen Browne的HasProperty功能 一种完全不同的方法是使用report_name和friendly_name字段创建tblReports。您必须维护该表,但工作负载应大致相当于在报表对象上维护Description属性。然后,您可以使用表上的简单SELECT作为列表框的RowSource。 更新:您还可以使用自定义函数从MSysObjects中进行SELECT,以返回每个报告的描述。
Public Function ReportDescription(ByVal pName As String) As String
    Dim strReturn As String
    Dim strMsg As String

On Error GoTo ErrorHandler

    strReturn = _
        CurrentDb.Containers("Reports").Documents(pName).Properties("Description")

ExitHere:
    On Error GoTo 0
    ReportDescription = strReturn
    Exit Function

ErrorHandler:
    Select Case Err.Number
    Case 3270 'Property not found.'
        'strReturn = "(no Description)"'
        '* no Description -> just echo report name *'
        strReturn = pName
    Case Else
        strMsg = "Error " & Err.Number & " (" & Err.description _
            & ") in procedure ReportDescription"
        MsgBox strMsg
        strReturn = vbNullString
    End Select
    GoTo ExitHere
End Function
修改原始查询以使用该功能。
SELECT
    [Name] AS report_name,
    ReportDescription([Name]) AS friendly_name
FROM MsysObjects
WHERE ([Type] = -32764);
    

要回复问题请先登录注册