可见属性在vba中工作不一致

时间:2018-04-16 02:20:43

标签: vba ms-access

我有一个重置按钮,应隐藏和禁用其表单上的某些按钮,但它不起作用。我尝试将相同的ctl.visible = falsectl.enabled = false编码应用到同一个子网中的其他控件(包括按钮),这一切都有效 - 它只是针对这些按钮而拒绝。没有错误出现,它只是不会改变可见或已启用的属性。我也在访问的属性列表中手动更改了它们的属性,这也有效。启用和取消隐藏它们的单独子工作正常。

我需要隐藏和禁用的按钮标有" spin"。它们在下面的重置子底部处理。整个子目录是为上下文提供的,以防任何一个负责我的问题。

Private Sub btnReset_Click()

Dim ctl As Control

For Each ctl In Screen.ActiveForm
If ctl.Tag Like "*lckbtn*" Then
    ctl.Enabled = True
    ctl.Visible = True
ElseIf ctl.Tag Like "*lcktxt*" Then
    ctl.Enabled = False
    ctl.Locked = True
    ctl.BackColor = RGB(236, 236, 236)
On Error Resume Next
    ctl.Value = ""
ElseIf ctl.Name = "PointsRemaining" Then
    ctl.ForeColor = RGB(140, 140, 140)
    ctl.Value = 27
    End If
If ctl.Tag Like "*array*" Then
    ctl.ForeColor = RGB(140, 140, 140)
    End If
    Next ctl
If ctl.Tag Like "*spin*" Then
    ctl.Enabled = False
    ctl.Visible = False
    End If

Array1 = 15
Array2 = 14
Array3 = 13
Array4 = 12
Array5 = 10
Array6 = 8

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个

Option Explicit

Private Sub btnReset_Click()
    Dim ctl As Control

    For Each ctl In Screen.ActiveForm
        Select Case True
            Case InStr(1, ctl.Tag, "lckbtn", vbTextCompare)
                ctl.Enabled = True
                ctl.Visible = True
            Case InStr(1, ctl.Tag, "lcktxt", vbTextCompare)
                ctl.Enabled = False
                ctl.Locked = True
                ctl.BackColor = RGB(236, 236, 236)
                On Error Resume Next
                ctl.Value = ""
            Case ctl.Name = "PointsRemaining"
                ctl.ForeColor = RGB(140, 140, 140)
                ctl.Value = 27
            Case InStr(1, ctl.Tag, "array", vbTextCompare)
                ctl.ForeColor = RGB(140, 140, 140)
            Case InStr(1, ctl.Tag, "spin", vbTextCompare)
                ctl.Enabled = False
                ctl.Visible = False
        End Select
    Next ctl
    Array1 = 15
    Array2 = 14
    Array3 = 13
    Array4 = 12
    Array5 = 10
    Array6 = 8
End Sub

您的代码存在一些问题

  • 缩进是非常误导的
  • If ctl.Tag Like "*spin*" Then位于For循环之外
    • 当执行点到达此行时,ctl未设置(错误)
    • 您还有On Error Resume Next隐藏了上述错误
    • On Error Resume Next是一个过程级处理程序(不仅适用于以下行)
  • 如上所述,
  • Like区分大小写(由于之前的问题,还没有达到这一点)

这是你的代码有适当的缩进(它表明最后If在循环之外)

Private Sub btnReset_Click()
    Dim ctl As Control

    For Each ctl In Screen.ActiveForm
        If ctl.Tag Like "*lckbtn*" Then
            ctl.Enabled = True
            ctl.Visible = True
        ElseIf ctl.Tag Like "*lcktxt*" Then
            ctl.Enabled = False
            ctl.Locked = True
            ctl.BackColor = RGB(236, 236, 236)
On Error Resume Next
            ctl.Value = ""
        ElseIf ctl.Name = "PointsRemaining" Then
            ctl.ForeColor = RGB(140, 140, 140)
            ctl.Value = 27
        End If
        If ctl.Tag Like "*array*" Then
            ctl.ForeColor = RGB(140, 140, 140)
        End If
    Next ctl
    If ctl.Tag Like "*spin*" Then   '<-- outside of the For loop (error as ctl is not set)
        ctl.Enabled = False
        ctl.Visible = False
    End If
    Array1 = 15
    Array2 = 14
    Array3 = 13
    Array4 = 12
    Array5 = 10
    Array6 = 8
End Sub