vba 禁用 MS Access 表单“详细信息”部分中的所有控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13175021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:19:12  来源:igfitidea点击:

Disable all controls in 'details' section of a MS Access form

vbams-accessms-access-2010

提问by Jay

I am looking for some command which will disable all controls present in the 'details' section of an MS Access Form. One way is to disable each and every control independently. I am looking for a control-independent way which would disable all the controls present in the details section of MS Access Form.

我正在寻找一些命令,该命令将禁用 MS Access 表单的“详细信息”部分中存在的所有控件。一种方法是独立禁用每个控件。我正在寻找一种独立于控件的方式,它将禁用 MS Access Form 的详细信息部分中存在的所有控件。

This feature might be useful, if the form is to be enabled based on some login credentials and the fields for entering credentials is placed in the header section of the form. So, as soon as user does login, all the controls in that form are enabled. Uplon logout, they shall be disabled.

如果要基于某些登录凭据启用表单,并且用于输入凭据的字段位于表单的标题部分,则此功能可能很有用。因此,一旦用户登录,该表单中的所有控件都会启用。Uplon 注销,他们将被禁用。

I tried searching for it but it seems the same is not supported. Or no one had such a requirement before.

我尝试搜索它,但似乎不支持它。或者之前没有人有这样的要求。

Please help me if anyone knows.

如果有人知道,请帮助我。

回答by Fionnuala

A form has various properties that match this requirement: Allow Edits, Allow Additions and Allow Deletions. Allow Edits works for unbound controls, it even works on unbound forms.

表单具有符合此要求的各种属性:允许编辑、允许添加和允许删除。允许编辑适用于未绑定的控件,它甚至适用于未绑定的表单。

If you have a requirement to disable certain controls, you can set the tab property for these controls to a string and then iterate through the form's control collection to disable those controls.

如果您需要禁用某些控件,您可以将这些控件的 tab 属性设置为字符串,然后遍历表单的控件集合以禁用这些控件。

Dim frm As Form
Dim ctl As Control

Set frm = Forms!MyOpenForm

For Each ctl In frm.Controls

    If ctl.ControlType <> acLabel And ctl.ControlType <> acTabCtl Then
        If ctl.Tag = "AdminHide" Then
            If varWho = "Authorized" Then
                ctl.Visible = True
            Else
                ctl.Visible = False
            End If
        End If

    End If

Next

回答by MarredCheese

You can disable all of the controls in the detail section directly without having to bother with tags:

您可以直接禁用详细信息部分中的所有控件,而不必费心使用标签:

Dim ctrl As Control
For Each ctrl In Detail.Controls
    If (TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox)
        ctrl.Enabled = False
    End If
Next

Similarly, you can get at the controls in the header and footer using FormHeader.Controlsand FormFooter.Controls.

同样,您可以使用FormHeader.Controls和获取页眉和页脚中的控件FormFooter.Controls

I usually prefer to use ctrl.Locked = Trueinstead of ctrl.Enabled = Falsesince users can still copy text from locked controls and use text filters on them, but that's up to you.

我通常更喜欢使用ctrl.Locked = True而不是ctrl.Enabled = False因为用户仍然可以从锁定的控件复制文本并对其使用文本过滤器,但这取决于您。