我将制作一个简单的用户窗体,以查找值并在ListBox中显示结果。我有15个文本框和一个按钮供查找。查找的数据源始终相同。仅输入值(用于查找)将是动态的。我的想法是:
问题是什么?通常,我可以为所有文本框编写很多代码,例如:
If TextBox1.Value <> "" Then
' execute finding method - input is TextBox1.Value
ElseIf TextBox2.Value <> "" Then
' execute finding method - input is TextBox2.Value
...
并生成15x ElseIf或secound选项分别创建按钮。它将看起来很有趣的15个按钮。
但是,两种解决方案都不是完美的。我该如何实施好的解决方案?
我的想法是:
- 声明一个公共变量,这将是输入值
- 检查某些文本框是否为空
- 如果不是,则声明变量将TextBox.Value(不为空)并执行查找方法
这是您要尝试的吗?
Dim MyValue As String
Dim cCont As Control
'~~> Loop through all textboxes
For Each cCont In Me.Controls
If TypeName(cCont) = "TextBox" Then
If Len(Trim(cCont.Value)) <> 0 Then
'~~> Set the value and exit the loop
MyValue = cCont.Value
Exit For
End If
End If
Next cCont
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句