【问题标题】:MS Access - Disable tagged controls based on combobox valueMS Access - 根据组合框值禁用标记控件
【发布时间】:2019-01-20 23:30:29
【问题描述】:

我想根据我在组合框中输入的值启用/禁用所有标记为“隐藏我”的字段。

如果comboxox 为yes,则应启用字段,如果没有则应禁用字段。

这里有一个working example 可以玩,这是我的代码,目前它不起作用:

Private Sub myAction_AfterUpdate()

Dim frm As Form
Dim ctl As Control

Set frm = Forms!frmMyForm

    For Each ctl In frm.Controls

        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then
            If ctl.Tag = "hideMe" Then
                If Me.myAction = yes Then
                    ctl.Enabled = True
                End If
            Else
                ctl.Enabled = False
            End If
        End If
    Next
End Sub

【问题讨论】:

  • 上面和附件中的代码完全一样。确保不要在 myAction 组合上放置“hideMe”标签。
  • @Santosh 谢谢。是的,那是故意的。 myAction 组合未标记。

标签: ms-access


【解决方案1】:

控制标签包含不需要的字符串(见下图)以及hideMe 标签。
下图在即时窗口上输出了Debug.Print ctl.Tag,以供快速参考。

将控制标签设置为hideMe并运行此代码。

Option Compare Database
Private Sub myAction_AfterUpdate()

Dim frm As Form
Dim ctl As Control

Set frm = Forms!frmMyForm

    For Each ctl In frm.Controls

        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Then


            If ctl.Tag = "hideMe" Then
                If Me.myAction = "yes" Then
                    ctl.Enabled = True
                 Else
                     ctl.Enabled = False
                 End If
            End If
        End If
    Next
End Sub

代码错误地与 hideMe 一起复制

【讨论】:

  • 谢谢@Santosh。这个确实有效。但是,您知道我的代码有什么问题吗?试图从我的错误中吸取教训。 :) 我从 stackoverflow.com/questions/13175021/… 改编了我的代码
  • 是的,看起来你不小心复制了整个代码以及控制标签下的 hideMe 标签
  • 我不明白,抱歉。 ;p 我将“myAction”组合框的标签属性留空,并将其他 5 个字段的标签属性设置为“hideMe”,如下所示。 imgur.com/a/VKzAKK8 不应该是这样吗?
  • 转到文本框控件=>打开属性=>其他选项卡=>将光标设置在IME Sentence Mode倒数第二个属性=>按tab=>光标将移动到标记属性=>复制文本并粘贴到记事本上。
  • 现在知道了。哦。非常感谢!
【解决方案2】:

你错过了一个空格并比较了一个字符串

If ctl.Tag = "hide Me" Then
    If Me!myAction.Value = "Yes" Then

【讨论】:

  • 谢谢,我更新了我的代码,但这不起作用。这些项目被标记为“hideMe”,没有空格imgur.com/a/VKzAKK8,我的操作值应该是“yes”(或“no”,或用户想要输入的任何其他值)。
  • Yes 不是字符串而是未知变量。 "Yes" 是一个字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
相关资源
最近更新 更多