让我们开始说明我的代码知识充其量是有限的。我一般都理解这些概念,但从来没有很好地实现它们。这就是说,这是一个落在我腿上的项目代码片段。
Option Compare Database
Private Sub Form_Current()
If IsNull(Me![OPT-78 (5900EL)]) Then
Me![OPT-78 (5900EL)].Visible = False
Me![OPT-78 (5900EL) Label].Visible = False
Else
Me![OPT-78 (5900EL)].Visible = True
Me![OPT-78 (5900EL) Label].Visible = True
End If
If IsNull(Me![OPT-78 (7900EL)]) Then
Me![OPT-78 (7900EL)].Visible = False
Me![OPT-78 (7900EL) Label].Visible = False
Else
Me![OPT-78 (7900EL)].Visible = True
Me![OPT-78 (7900EL) Label].Visible = True
End If
If IsNull(Me![OPT-152 (4310B)]) Then
Me![OPT-152 (4310B)].Visible = False
Me![OPT-152 (4310B) Label].Visible = False
Else
Me![OPT-152 (4310B)].Visible = True
Me![OPT-152 (4310B) Label].Visible = True
End If
它位于"当前"中的一个表单内。事件。现在,我知道通配符可以将所有这些语句限制为一个简单优雅的单个if语句解决方案,但我还没有能够让它们工作。我试过*,%,__和?,但没有运气。还有很多if else语句必须写,所以我希望有一个更清洁的解决方案。
谢谢!
答案 0 :(得分:1)
我首先创建一个Sub来处理设置可见性:
Sub SetVis(controlName as String)
Dim vis as Boolean
vis = Not IsNull(Me.Controls(controlName))
Me.Controls(controlName).Visible = vis
Me.Controls(controlName & " Label").Visible = vis
End sub
然后你可以从一个循环中驱动那个sub,也许是通过循环遍历表单的Controls集合来找到感兴趣的那个。
Dim c
For Each c in Me.Controls
'some If statement here to see if this is a control you're interested in
'Might also want to check that the control type is a textbox so you can
' skip the labels
If c.Name like "OPT*" And Not c.Name like "*Label" Then
SetVis CStr(c.Name)
End if
Next c
或者基于固定名称数组的简单循环:
Dim c
For Each c In Array("OPT-78 (5900EL)", "OPT-78 (7900EL)", "OPT-152 (4310B)")
SetVis CStr(c)
Next c