根据字段值隐藏表单选项卡

Hide a Form tab depending on the value of a field

范围内的问题非常简单。

问题: 想知道我是否能够根据 table 字段的值隐藏表单的选项卡。

我一直在阅读 2019 Access Bible,到目前为止,我仍然不清楚我将如何编写 VBA 模块以不断 运行ning。我在尝试的早期就问了这个问题,但希望我能问得足够好,以便抢先一步。

我还不是很了解VBA访问的执行模型。我之前有编码经验,但这将是我第一次访问。只是在寻找有关如何编写函数范围的帮助。你在下面看到我认为它应该是“主要”的东西,因为我希望它在表单视图中时 运行 。喜欢 我知道如何编写私有子函数来响应按钮点击。但不适用于表单背景中仅 运行s 的函数。

我想真正的问题是什么时候执行?正确的?有什么建议吗?

我在考虑以下内容。

Main function()
If Me.FieldProcess_Water = "1" Then
Me.TabProcess_Water.Visible = True
Else
Me.TabProcess_Water.Visible  = False
End If
End Sub

这需要一些设置来减少冗余代码,但应该可以满足您的要求。

首先,您需要使复选框名称和页面名称相似。在我的示例中,页面名称为 Tab_Name,复选框名称为 Name。例如。 Tab_Process 水和工艺用水。

然后在表单的 code-behind.

中创建一个名为 Form_Current() 的子项
Private Sub Form_Current()
    Dim chk As Control
    
    For Each chk In Me.Controls
        If chk.ControlType = acCheckBox Then
            If chk = False Then
               'Change TabCtl0 to whatever your's is called
                Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = False
            Else
                Me.TabCtl0.Pages("Tab_" & chk.Name).Visible = True
            End If
        End If
    Next
    
End Sub

这将遍历当前记录的复选框并切换其各自选项卡的可见性。

要让复选框在单击时更新选项卡:

Private Sub Form_Load()
    Dim chk As Control
    Dim prop As Variant
    For Each chk In Me.Controls
        If chk.ControlType = acCheckBox Then
           chk.OnClick = "=Check_Click()"
        End If
    Next
End Sub

这将为每个复选框分配一个命令。

    Dim caller As String
    caller = Me.ActiveControl.Name
    If Me.ActiveControl.Value = False Then
        Me.TabCtl0.Pages("Tab_" & caller).Visible = False
    Else
        Me.TabCtl0.Pages("Tab_" & caller).Visible = True
    End If

这将隐藏相关标签。