VBA 在忽略子窗体的同时迭代主窗体上的控件
VBA Iterate over controls on main form while ignoring subform
我有一个按钮,用于清除(搜索)表单上的所有活动控件。然而,每个搜索的输出都显示在一个子表单中,并且似乎是作为文本框实现的。 (虽然它显示为电子表格,但子窗体的设计视图显示文本框。)
因此,当我将表单上所有 acTextBox
控件的 Value
设置为 ""
时,它也会清除子表单上的内容以及所有进一步的搜索 return #Name 在所有字段中,直到重新打开表单。
此外,在发生此错误时,单击子窗体的 Form
对象会生成一条消息警告 Object invalid or no longer set.
尝试编辑关联的方法通常会导致窗体在设计视图中打开并采取重点,这可能重要也可能不重要。 (如果设计视图尚未打开,只要在方法中按下一个键就会发生这种情况。)
问题似乎在以下块中:
Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If Len(ctl.Properties("ControlSource")) = 0 Then
ctl.Value = "" '***
End If
ElseIf ctl.ControlType = acCheckBox Then
ctl.Value = False
ElseIf ctl.ControlType = acComboBox Then
ctl.Value = ""
End If
Next ctl
'***
后面的赋值是我缩小问题范围的地方,因为注释掉该行可以防止问题发生——但是将文本框的值设置为“”很重要我正在尝试做的部分工作,我无法摆脱它。
ControlSource 长度比较试图从下一行中排除子表单项,因为它们都是绑定的,而主表单项不是。它是嵌套的(与 If (... And ...) 相反),因为 VBA 没有惰性 Or.
要在不影响子窗体控件的情况下迭代所有主窗体控件需要做什么?
编辑:澄清 subform/query。
子表单正在使用以下语句从查询中提取数据:
SELECT DW_Query.Map_Number, DW_Query.Map_Name, DW_Query.Map_Type, DW_Query.Town, DW_Query.Address, DW_Query.Street, DW_Query.Block, DW_Query.Lot, DW_Query.group, DW_Query.Folder, DW_Query.Latitude, DW_Query.Longitude FROM DW_Query;
似乎 Town 字段可能会以某种方式被删除,因为有时会有一个 RTE 2465 的实例,can't find the field 'Town' referred to in your expression.
但是,这发生在主窗体的 Form_Open 子窗体中,在下面行:
Me.DW_Query_subform.Form.RecordSource = "SELECT DW_Query.Map_Number, DW_Query.Map_Name, DW_Query.Map_Type, DW_Query.Town," & _
"DW_Query.Address, DW_Query.Street, DW_Query.Block, DW_Query.Lot, DW_Query.group, DW_Query.Folder, DW_Query.Latitude, DW_Query.Longitude " & _
"FROM DW_Query;" 'Set subform to use address query. (Faster)
您需要设置为 Null,因为文本框可能不接受空字符串:
Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If Len(ctl.Properties("ControlSource")) = 0 Then
ctl.Value = Null
End If
ElseIf ctl.ControlType = acCheckBox Then
ctl.Value = False
ElseIf ctl.ControlType = acComboBox Then
ctl.Value = Null
End If
Next ctl
您可以缩小迭代范围,例如仅清除“详细信息”部分中的控件
Dim ctl As Control
For Each ctl In Me.Detail.Controls
If (ctl.ControlType = acTextBox) Then
If Len(ctl.Properties("ControlSource")) = 0 Then
ctl.Value = vbnullString
End If
ElseIf ctl.ControlType = acCheckBox Then
ctl.Value = False
ElseIf ctl.ControlType = acComboBox Then
ctl.Value = Null
End If
Next ctl
我有一个按钮,用于清除(搜索)表单上的所有活动控件。然而,每个搜索的输出都显示在一个子表单中,并且似乎是作为文本框实现的。 (虽然它显示为电子表格,但子窗体的设计视图显示文本框。)
因此,当我将表单上所有 acTextBox
控件的 Value
设置为 ""
时,它也会清除子表单上的内容以及所有进一步的搜索 return #Name 在所有字段中,直到重新打开表单。
此外,在发生此错误时,单击子窗体的 Form
对象会生成一条消息警告 Object invalid or no longer set.
尝试编辑关联的方法通常会导致窗体在设计视图中打开并采取重点,这可能重要也可能不重要。 (如果设计视图尚未打开,只要在方法中按下一个键就会发生这种情况。)
问题似乎在以下块中:
Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If Len(ctl.Properties("ControlSource")) = 0 Then
ctl.Value = "" '***
End If
ElseIf ctl.ControlType = acCheckBox Then
ctl.Value = False
ElseIf ctl.ControlType = acComboBox Then
ctl.Value = ""
End If
Next ctl
'***
后面的赋值是我缩小问题范围的地方,因为注释掉该行可以防止问题发生——但是将文本框的值设置为“”很重要我正在尝试做的部分工作,我无法摆脱它。
ControlSource 长度比较试图从下一行中排除子表单项,因为它们都是绑定的,而主表单项不是。它是嵌套的(与 If (... And ...) 相反),因为 VBA 没有惰性 Or.
要在不影响子窗体控件的情况下迭代所有主窗体控件需要做什么?
编辑:澄清 subform/query。
子表单正在使用以下语句从查询中提取数据:
SELECT DW_Query.Map_Number, DW_Query.Map_Name, DW_Query.Map_Type, DW_Query.Town, DW_Query.Address, DW_Query.Street, DW_Query.Block, DW_Query.Lot, DW_Query.group, DW_Query.Folder, DW_Query.Latitude, DW_Query.Longitude FROM DW_Query;
似乎 Town 字段可能会以某种方式被删除,因为有时会有一个 RTE 2465 的实例,can't find the field 'Town' referred to in your expression.
但是,这发生在主窗体的 Form_Open 子窗体中,在下面行:
Me.DW_Query_subform.Form.RecordSource = "SELECT DW_Query.Map_Number, DW_Query.Map_Name, DW_Query.Map_Type, DW_Query.Town," & _
"DW_Query.Address, DW_Query.Street, DW_Query.Block, DW_Query.Lot, DW_Query.group, DW_Query.Folder, DW_Query.Latitude, DW_Query.Longitude " & _
"FROM DW_Query;" 'Set subform to use address query. (Faster)
您需要设置为 Null,因为文本框可能不接受空字符串:
Dim ctl As Control
For Each ctl In Me.Controls
If (ctl.ControlType = acTextBox) Then
If Len(ctl.Properties("ControlSource")) = 0 Then
ctl.Value = Null
End If
ElseIf ctl.ControlType = acCheckBox Then
ctl.Value = False
ElseIf ctl.ControlType = acComboBox Then
ctl.Value = Null
End If
Next ctl
您可以缩小迭代范围,例如仅清除“详细信息”部分中的控件
Dim ctl As Control
For Each ctl In Me.Detail.Controls
If (ctl.ControlType = acTextBox) Then
If Len(ctl.Properties("ControlSource")) = 0 Then
ctl.Value = vbnullString
End If
ElseIf ctl.ControlType = acCheckBox Then
ctl.Value = False
ElseIf ctl.ControlType = acComboBox Then
ctl.Value = Null
End If
Next ctl