【问题标题】:Hide/Unhide controls on main form based on control's tag using on current event in continuous sub form隐藏/取消隐藏基于控件标签的主窗体上的控件,在连续子窗体中使用当前事件
【发布时间】:2021-04-27 02:54:21
【问题描述】:

第一次发帖。您能否建议如何在我的代码中修复此错误。我希望我能正确地提出这个问题。我有一个带有连续子表单的主表单。在名为 lngIteTypFK 的项目类型的子表单中有一个控件。我正在尝试使用当前事件的子表单来根据主表单控件标签使主表单上的未绑定控件隐藏或可见。如果标签的值等于项目类型的值,则可见。当我尝试运行以下代码时,出现错误“您输入的表达式对属性 Visible 的引用无效”:

Private Sub Form_Current()
On Error GoTo ErrorHandler

Dim frm As Form
Dim ctl As Control
Dim varlngIteTypFK As Long

Set frm = Forms!frmQot

varlngIteTypFK = Me.lngIteTypFK

For Each ctl In frm

    Select Case ctl.Tag
        Case Is = 0
            ctl.Visible = True 'error happens here
        Case Is = 1
            ctl.Visible = False
        Case Is = varlngIteTypFK
            ctl.Visible = True
        Case Else
            ctl.Visible = False
    End Select

Next ctl

ExitError:
     Exit Sub
ErrorHandler:
     MsgBox Err.Description
     Resume ExitError
End Sub

【问题讨论】:

  • 试过这个结构。我没有收到错误。
  • 感谢您的尝试。这真的让我很难过。
  • 我刚刚在一个新的数据库中尝试了这段代码,只有表单和代码,它运行良好。这里还有其他事情发生。如果我找到答案,我会报告。

标签: vba ms-access


【解决方案1】:

我已经解决了这个问题。父窗体上有空单元格。由于某种原因,错误是由循环通过控件时这些空单元格引起的。我添加了一行来测试控件是标签、文本框还是组合框。这解决了如下问题:

Option Compare Database
Option Explicit

Private Sub Form_Current()
'On Error GoTo ErrorHandler

Dim frm As Form
Dim ctl As Control
Dim varlngIteTypFK As Long

Set frm = Forms!frmQot

varlngIteTypFK = Me.lngIteTypFK

For Each ctl In frm
    If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Or ctl.ControlType = acLabel Then 'added this line
        Select Case ctl.Tag
            Case Is = 0
                ctl.Visible = True
            Case Is = 1
                ctl.Visible = False
            Case Is = varlngIteTypFK
                ctl.Visible = True
            Case Else
                ctl.Visible = False
        End Select
    End If
Next ctl

ExitError:
    Exit Sub
ErrorHandler:
    MsgBox Err.Description
    Resume ExitError
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    相关资源
    最近更新 更多