我想遍历表单上的所有 UNBOUND 控件并清除它们的数据或重置它们的值。我有文本框、组合框和复选框。每次我尝试这样的事情:
Dim ctl As Control
For Each ctl In Me.Controls
If IsNull(ctl.ControlSource) Then
ctl.Value = Nothing
End If
Next ctl
我收到运行时错误消息:
438 This object doesn't support this property or method.
最佳答案
该代码循环遍历表单 Controls
中的每个控件。收藏。该集合包括控件,例如标签和命令按钮,它们既未绑定(bind)也未绑定(bind)...因此尝试引用它们的.ControlSource
产生该错误。
对于诸如未绑定(bind)文本框之类的控件,其.ControlSource
property 是一个空字符串,而不是 Null。
因此,当您遍历控件时,请检查 .ControlSource
仅适用于您希望定位的那些控件类型。在下面的示例中,我选择了文本框和组合框。当.ControlSource
是一个长度为零的字符串,设置控件的.Value
为空。
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox ' adjust to taste
'Debug.Print ctl.Name, Len(ctl.ControlSource)
If Len(ctl.ControlSource) = 0 Then
ctl.value = Null
End If
Case Else
' pass
End Select
Next
关于ms-access - 循环遍历表单上所有未绑定(bind)的控件并清除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15348730/