我有一堆文本框,其中将填充从下拉列表中选择的值。
让我们说在10个文本框中,将值填充到5:Rinv1
至Rinv5
。
如何遍历这些值并收集这些值?
我试图使用for each
循环并将这些值收集到数组中,然后将这些值存储在数据库的字段中。
Rolls = Array(Me.Rinv1, Me.Rinv2, Me.Rinv3)
Dim RollName As Variant
'iterating using For each loop.
For Each Item In Rolls
RollName = RollName & Item
Next
Forms![LabelSHEETER2].JOB = RollName
某些语法错误。
答案 0 :(得分:2)
我建议遍历控件名称而不是控件对象,例如,遵循以下内容:
Dim Itm
Dim RollName As String
For Each Itm in Split("Rinv1 Rinv2 Rinv3")
RollName = RollName & Me.Controls(Itm).Value
Next Itm
Forms![LabelSHEETER2].JOB = RollName
答案 1 :(得分:1)
这是一个简单的示例,说明如何获取所有TextBoxes的值并将它们组合为字符串
Private Sub CommandButton1_Click()
Dim ctrl As Control
Dim str As String
For Each ctrl In UserForm1.Controls
If TypeName(ctrl) = "TextBox" Then
str = str & ctrl.Text
End If
Next ctrl
MsgBox str
End Sub
答案 2 :(得分:1)
我将遍历Form控件并检查名称是否匹配。 这样,您无需再次离开“表单设计”窗口即可更改代码或将对象添加到数组。
Dim ctl As Control
Dim strRollName As String
For Each ctl in Me.Controls
If ctl.Name Like "Rinv*" Then
strRollName = strRollName & ctl.Value & " "
End If
Next ctl