首页 > 解决方案 > VBA 列出 MS Access 中所有打开的对象

问题描述

我想知道 Access VBA 是否可以列出当前 Access 实例中所有打开的文档选项卡。我有可以检查每个 Access 对象并报告它是否已加载的代码。它很快,但感觉像是一种倒退的方法。它也无法列出未保存的对象,例如新查询。此外,我无法分辨哪些对象正在显示选项卡。弹出窗口没有选项卡。

是否有可以列出所有选项卡式文档的 VBA 例程?

访问选项卡式文档图像

Public Sub ListAllOpenObjects()
' This will list all open Access objects.  It will not list
' objects that are new that have not be saved, like Query1.

    Dim aob As AccessObject

    With CurrentData
        ' "Tables"
        For Each aob In .AllTables
            If aob.IsLoaded Then
                Debug.Print aob.name
            End If
        Next aob

        ' "Queries"
        For Each aob In .AllQueries
            If aob.IsLoaded Then
                Debug.Print aob.name
            End If
        Next aob
    End With

    With CurrentProject
        ' "Forms"
        For Each aob In .AllForms
            If aob.IsLoaded Then
                Debug.Print aob.name
            End If
        Next aob

        ' "Reports"
        For Each aob In .AllReports
            If aob.IsLoaded Then
                Debug.Print aob.name
            End If
        Next aob

        ' "Pages"
        For Each aob In .AllDataAccessPages
            If aob.IsLoaded Then
                Debug.Print aob.name
            End If
        Next aob

        ' "Macros"
        For Each aob In .AllMacros
            If aob.IsLoaded Then
                Debug.Print aob.name
            End If
        Next aob

        ' "Modules"
        For Each aob In .AllModules
            If aob.IsLoaded Then
                Debug.Print aob.name
            End If
        Next aob

    End With

End Sub

标签: vbams-access

解决方案


表单和报告有一个快捷方式,因为Application.Forms包含所有打开的表单,Application.Reports包含打开的报告(无论它们是弹出窗口还是不可见)。

由于用户不应该能够打开表或查询,这在生产中就足够了。

如果您在开发过程中需要该信息,则必须循环相应的集合并检查每个集合的IsLoaded.

作为替代方案,您应该能够通过它们的句柄检测打开的选项卡,但这需要广泛使用(和知识)WinAPI 函数,如FindWindowGetClassName。这可用于提高您的 WinAPI 技能,但并不比循环集合更优雅。


推荐阅读