Isaac Reefman
Isaac Reefman

Reputation: 597

Visible property working inconsistently in vba

I have a reset button that should hide and disable certain buttons on its form, but it's not working. I've tried applying the same ctl.visible = false and ctl.enabled = false coding to other controls (including buttons) in the same sub, and it all works - it's just for these buttons it refuses to. No errors come up, it just doesn't change either the visible or enabled properties. I've also manually changed their properties in the properties list in access, which works too. And a separate sub that enables and un-hides them works fine.

The buttons I need to hide and disable are tagged with "spin". They're dealt with at the bottom of the Reset sub below. The whole sub is provided for context, in case any of it is responsible for my issue.

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

Upvotes: 1

Views: 620

Answers (1)

paul bica
paul bica

Reputation: 10715

Try this


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

There are a few issues with your code

  • Indentation is quite misleading
  • If ctl.Tag Like "*spin*" Then is outside the For loop
    • When execution point gets to this line, ctl is not set (error)
    • You also have On Error Resume Next which hides the above error
    • On Error Resume Next is a procedure-level handler (not only for the following line)
  • Like, as noted, is case sensitive (didn't get to this point yet because of the previous issue)

This is your code with proper indentation (it shows that the last If is outside the loop)


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

Upvotes: 1

Related Questions