遍历具有特定命名结构的控件以设置可见 属性
Loop through controls with certain naming structure to set visible property
我构建了一个表格,显示每个月的 "status" 个项目。我想在每个月初更新一次 column/control 框属性(随着年份的推移取消隐藏当前月份)。由于未来几个月还没有任何数据,我编写了以下代码。
Public Function updateProgressForm()
Dim currentMonth As String
Dim currentYear As Long
Dim txtDate As String
Dim todaysmonth As Long
Dim txtboxMonth As Integer
Dim txtboxName As String
Dim txtboxLabel As String
todaysmonth = Month(Now())
currentMonth = MonthName(todaysmonth, 3)
currentYear = Year(Now())
txtDate = currentMonth & " " & currentYear
DoCmd.OpenForm "IndProgressTracking", acDesign, , , , acWindowNormal
Forms!IndProgressTracking!Month12.ControlSource = txtDate
Forms!IndProgressTracking!Month12Label.Caption = txtDate
For txtboxMonth = 1 To 12 Step 1
txtboxName = "Month" & txtboxMonth
txtboxLabel = txtboxName & "Label"
If IsNull(txtboxName) Then
txtboxName.Visible = False
txtboxLabel.Visible = False
Else
txtboxName.Visible = True
txtboxLabel.Visible = True
End If
Next
txtboxName = "Month1"
Forms!IndProgressTracking.Controls!txtboxName.Visible = True
End Function
给我一个错误的部分是 txtboxName.Visible 它指出无效的限定符。因此我尝试了最后两行,但是,最后一行(在结束函数之前)声明它找不到我引用的控件(txtboxName
)。我每个月都有一个文本框和标签,所以如果我必须使用控件名称分别调用每个月,这没什么大不了的,我只是希望通过使用 For 循环来简化。欢迎提出任何建议!
您已将 txtboxName
定义为字符串而不是控件对象。
要通过名称引用控件,请使用:
Forms!IndProgressTracking.Controls(txtboxname).Visible = True
我构建了一个表格,显示每个月的 "status" 个项目。我想在每个月初更新一次 column/control 框属性(随着年份的推移取消隐藏当前月份)。由于未来几个月还没有任何数据,我编写了以下代码。
Public Function updateProgressForm()
Dim currentMonth As String
Dim currentYear As Long
Dim txtDate As String
Dim todaysmonth As Long
Dim txtboxMonth As Integer
Dim txtboxName As String
Dim txtboxLabel As String
todaysmonth = Month(Now())
currentMonth = MonthName(todaysmonth, 3)
currentYear = Year(Now())
txtDate = currentMonth & " " & currentYear
DoCmd.OpenForm "IndProgressTracking", acDesign, , , , acWindowNormal
Forms!IndProgressTracking!Month12.ControlSource = txtDate
Forms!IndProgressTracking!Month12Label.Caption = txtDate
For txtboxMonth = 1 To 12 Step 1
txtboxName = "Month" & txtboxMonth
txtboxLabel = txtboxName & "Label"
If IsNull(txtboxName) Then
txtboxName.Visible = False
txtboxLabel.Visible = False
Else
txtboxName.Visible = True
txtboxLabel.Visible = True
End If
Next
txtboxName = "Month1"
Forms!IndProgressTracking.Controls!txtboxName.Visible = True
End Function
给我一个错误的部分是 txtboxName.Visible 它指出无效的限定符。因此我尝试了最后两行,但是,最后一行(在结束函数之前)声明它找不到我引用的控件(txtboxName
)。我每个月都有一个文本框和标签,所以如果我必须使用控件名称分别调用每个月,这没什么大不了的,我只是希望通过使用 For 循环来简化。欢迎提出任何建议!
您已将 txtboxName
定义为字符串而不是控件对象。
要通过名称引用控件,请使用:
Forms!IndProgressTracking.Controls(txtboxname).Visible = True