通过形式清晰的数据上的所有未绑定的控件循环 [英] Loop through all unbound controls on a form and clear data

查看:126
本文介绍了通过形式清晰的数据上的所有未绑定的控件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的窗体上的所有未绑定的控件循环,并清除他们的数据或重新设置它们的值。我有文本框,组合框和复选框。每次我尝试是这样的:

 昏暗的CTL作为控制
    对于每个CTL在Me.Controls
        如果ISNULL(ctl.ControlSource)然后
            ctl.Value =无
        结束如果
    接下来CTL
 

我得到一个运行时错误说:

  

438这个对象不支持此属性或方法。

解决方案

这code依次通过的每次的形式的控制控制采集。该系列包括控件,如标签和命令按钮,这既不是绑定的,也没有绑定...所以试图引用他们的 .ControlSource 生成错误。

有关,如绑定文本框控件,它的 .ControlSource 属性为空字符串,而不是空。

因此​​,作为通过控制你循环,检查 .ControlSource 只希望定位的控制方式。在下面的例子中,我选择了文本和组合框。当 .ControlSource 是一个零长度字符串,设置控件的。价值设置为NULL。

对于每个CTL在Me.Controls     选择案例ctl.ControlType     案例acTextBox,acComboBox调整口味         Debug.Print ctl.Name,莱恩(ctl.ControlSource)         如果len(ctl.ControlSource)= 0则             ctl.value = NULL         结束如果     案例否则         ' 通过     最终选择 下一个

I would like to loop through all UNBOUND controls on my form and clear their data or reset their values. I have textboxes, comboboxes and checkboxes. Every time I try something like this:

Dim ctl As Control
    For Each ctl In Me.Controls
        If IsNull(ctl.ControlSource) Then
            ctl.Value = Nothing
        End If
    Next ctl

I get a runtime error saying:

438 This object doesn't support this property or method.

解决方案

That code loops through every control in the form's Controls collection. The collection includes controls, such as labels and command buttons, which are neither bound nor unbound ... so attempting to reference their .ControlSource generates that error.

For a control such as an unbound text box, its .ControlSource property is an empty string, not Null.

So as you loop through the controls, inspect the .ControlSource for only those control types you wish to target. In the following example I chose text and combo boxes. When the .ControlSource is a zero-length string, set the control's .Value to Null.

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

这篇关于通过形式清晰的数据上的所有未绑定的控件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆