首页 > 解决方案 > 访问 VBA 以打开/激活数据库表

问题描述

我有一个开放的 MS Access 数据库,左侧显示了 20 个表,并且打开了两个选项卡,显示了两个表的内容。我正在尝试将 vba 代码编写为 1)选择/激活已打开的选项卡之一,以及 2)为左侧尚未打开的表之一打开一个新选项卡。

我看过很多示例,但它们都在谈论表单,而不是我想在主 Access 表显示中使用的选项卡控件/选项卡。到目前为止,这是我的代码 - 我可以获得表的名称,但我不知道如何在 Access 显示中打开和激活表的选项卡。

Sub ActivateCommandsTable()
 ' activate or open a tab for the Commands table
 Dim tbl As AccessObject, db As Object
 Set db = Application.CurrentData

 ' Search for open AccessObject objects in AllTables collection.
 For Each tbl In db.AllTables

 If tbl.IsLoaded = True Then
    ' Print name of the table
    Debug.Print tbl.name
    If tbl.name = "Commands" Then

        ' I need some code here to activate/open the table tab

        Exit Sub
    End If
 End If

 Next tbl
End Sub

标签: vbams-access

解决方案


只需使用DoCmd.OpenTable.

DoCmd.OpenTable tbl.Name


推荐阅读