更新事件后无法在MS Access中切换标签可见性

时间:2016-07-24 19:41:17

标签: vba ms-access

我目前有一个更新后事件,它循环显示表单中的文本框,并切换它们是否编号低于某个阈值数字的可见性。

Dim i As Long    
        Dim iMin As Long    
        Dim iMax As Long    
        iMin = 1   
        iMax = Me.txtMeasure.Value   
        With Me   
            For i = iMin To iMax   
                              .Controls("V" & i).Visible = True   
            Next i   
        End With

文本框名为V1, V2, V3...V110等。“与每个文本框对应的标签标记为lblV1lblV2lblV3lblV110等“。默认情况下,文本框和标签默认为不可见,以切换其可见性。该代码适用于文本框,但无法用于标签。我注意到,当标签看起来不可见时,切换到设计视图然后再返回到窗体视图,标签突然显得可见。有没有办法调试它,以便它们同时显示?

2 个答案:

答案 0 :(得分:0)

尝试此操作,它会循环显示控件,如果它是textboxlabel,您可以使用它们执行所需操作,我已在此代码中将它们设置为Visible。< / p>

Dim con As Control
Dim i As Long
Dim iMin As Long
Dim iMax As Long
Dim textBoxArr
Dim j As Long
        iMin = 1
        iMax = Me.txtMeasure.Value
     textBoxArr = Array("V1", "V2", "V3", "V4", "V5", "V6", "V7")
    For Each con In Me.Controls    
        If TypeName(con) = "TextBox" Or TypeName(con) = "Label" Then
          For j = 0 To UBound(textBoxArr)
            If con.Name = textBoxArr(j) Or con.Name = "lbl" & textBoxArr(j) Then
                con.Visible = True: Exit For
            End If
          Next j
        End If
    Next con

答案 1 :(得分:0)

您只能在代码中添加一行。

        Dim i As Long    
        Dim iMin As Long    
        Dim iMax As Long    
        iMin = 1   
        iMax = Me.txtMeasure.Value   
        With Me   
            For i = iMin To iMax   
                              .Controls("V" & i).Visible = True  
                              .Controls("lbl" & i).Visible = True 
            Next i   
        End With