vba 如何遍历窗体中的所有控件,包括子窗体中的控件 - Access 2007

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3344649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:40:09  来源:igfitidea点击:

How to loop through all controls in a form, including controls in a subform - Access 2007

vbams-accessms-access-2007

提问by noelmcg

As the title of my question suggest, how is it possible to loop through all the controls in a form, including subforms.

正如我的问题标题所暗示的那样,如何循环遍历表单中的所有控件,包括子表单。

For example I use the below sub routine to set the background colour of controls with the tag *

例如,我使用下面的子程序来设置带有标签 * 的控件的背景颜色

Public Sub colCtrlReq(frm As Form)
'  Sets background color for required field -> Tag = *
Dim setColour As String
setColour = RGB(255, 244, 164)
Dim ctl As Control
For Each ctl In frm.Controls
        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
            If InStr(1, ctl.Tag, "*") <> 0 Then
                ctl.BackColor = setColour
            End If
        End If
Next ctl
Set ctl = Nothing
End Sub

How would alter this to catch the controls in a subform? Thanks in advance for any help or pointers.

如何更改它以捕获子窗体中的控件?在此先感谢您的任何帮助或指示。

Cheers Noel

干杯诺埃尔

回答by Fionnuala

You can use recursion

您可以使用递归

Public Sub colCtrlReq(frm As Form)
''  Sets background color for required field -> Tag = *
Dim setColour As String
setColour = RGB(255, 244, 164)
Dim ctl As Control
For Each ctl In frm
        If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox _
            Or ctl.ControlType = acListBox Then
            If InStr(1, ctl.Tag, "*") <> 0 Then
                ctl.BackColor = setColour
            End If
        ElseIf ctl.ControlType = acSubform Then
            colCtrlReq frm(ctl.Name).Form

        End If
Next ctl
Set ctl = Nothing
End Sub

回答by HansUp

Access the controls collection of the subform control's Form property.

访问子窗体控件的 Form 属性的控件集合。

Beware, the name of the subform control may not be the same as the name of the saved form object.

请注意,子表单控件的名称可能与保存的表单对象的名称不同。

If your subform controlis named SubformControlName, start from here:

如果您的子表单控件名为 SubformControlName,请从这里开始:

For Each ctl In frm!SubformControlName.Form.Controls
    Debug.Print ctl.Name
Next

Update: From your comment, here's what I think you're looking for.

更新:根据您的评论,这就是我认为您正在寻找的内容。

If you don't know the name(s) of your subform control(s) in advance, you can identify which of your form's controls are subform controls at run time.

如果事先不知道子窗体控件的名称,则可以在运行时确定窗体的哪些控件是子窗体控件。

For Each ctl In frm.Controls
    If TypeName(ctl) = "SubForm" Then
        Debug.Print ctl.Name & " is a SubForm"
        For Each ctlSub in ctl.Form.Controls
            Debug.Print ctlSub.Name
        Next 
    End If
Next