vba 在 Access 窗体上将控件分组在一起
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1661751/
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
Grouping controls together on an Access form
提问by zohair
I have an Access2003 form where I wanted to group several controls together and change visibility programatically, though VBA code.
我有一个 Access2003 表单,我想在其中将多个控件组合在一起并通过 VBA 代码以编程方式更改可见性。
Is this possible? I do know that I can group items through Format -> Group, but if I do that, how do I refer the the entire group in my code?
这可能吗?我知道我可以通过 Format -> Group 对项目进行分组,但是如果我这样做了,我如何在我的代码中引用整个组?
Thank you
谢谢
回答by Joel Gauvreau
You could place all the controls in a group box control then change the visibility of the group box itself.
您可以将所有控件放在一个分组框控件中,然后更改分组框本身的可见性。
You could also add a value in the tag property of each control you want to group, then in VBA loop through the control and check for that value and change the visibility there.
您还可以在要分组的每个控件的标签属性中添加一个值,然后在 VBA 中循环遍历控件并检查该值并更改那里的可见性。
Set the tag property of all the controls you want to group to something like groupABC or whatever you wish.
将要分组的所有控件的 tag 属性设置为 groupABC 之类的内容或您希望的任何内容。
Then somewhere in your code use this to loop through the form controls and check for it.
然后在您的代码中的某处使用它来循环表单控件并检查它。
Dim ctrl As Control
For Each ctrl In Me.Controls
If ctrl.Tag = "groupABC" Then
ctrl.Visible = False
End If
Next
回答by David-W-Fenton
To elaborate on my comment on using custom collections, you'd do something like this in your form's module:
要详细说明我对使用自定义集合的评论,您可以在表单的模块中执行以下操作:
Private mcolGroupABC As New Collection
Private Sub IntializeCollections()
Dim ctl As Control
If mcolGroupABC.Count = 0 Then
For Each ctl in Me.Controls
If ctl.Tag = "GroupABC" Then
mcolGroupABC.Add ctl, ctl.Name
End If
Next ctl
Set ctl = Nothing
End If
End Sub
Private Sub Form_Load()
Call InitializeCollections
End Sub
Private Sub ShowControls(mcol As Collection, bolShow As Boolean)
Dim ctl As Control
For Each ctl In mcol
ctl.Visible = bolShow
Next ctl
Set ctl = Nothing
End Sub
To hide the controls, you'd do this:
要隐藏控件,您可以这样做:
Call ShowControls(mcolGroupABC, False)
And to show them:
并向他们展示:
Call ShowControls(mcolGroupABC, True)
That's pretty simple, no?
这很简单,不是吗?
This is the kind of code I use in my apps all the time, and I've used it ever since the first time I implemented it, about 10 years ago, and noticed that it was clearly noticeably faster to show/hide controls with the custom collection than it was with walking the entire Controls collection.
这是我一直在我的应用程序中使用的那种代码,自从大约 10 年前我第一次实现它以来,我一直在使用它,并注意到使用自定义集合而不是遍历整个 Controls 集合。
The only caveat is that if one of the controls has the focus, it will error out if you try to hide it. That's easily enough addressed, since if you're hiding a group of controls, you surely have an appropriate place to set the focus before you do so.
唯一需要注意的是,如果其中一个控件具有焦点,如果您尝试隐藏它,它将出错。这很容易解决,因为如果您要隐藏一组控件,那么在这样做之前您肯定有一个合适的位置来设置焦点。
回答by Fionnuala
I like the tag property suggested by Joel Gauvreau. Other possibilities include a tab control and / or subforms.
我喜欢 Joel Gauvreau 建议的标签属性。其他可能性包括选项卡控件和/或子表单。

