在 Access 表单上将控件分组在一起
我有一个 Access2003 表单,我想将多个控件组合在一起并通过 VBA 代码以编程方式更改可见性。
这可能吗?我确实知道我可以通过格式对项目进行分组 ->组,但如果我这样做,如何在代码中引用整个组?
谢谢
I have an Access2003 form where I wanted to group several controls together and change visibility programatically, though VBA code.
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?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将所有控件放置在组框控件中,然后更改组框本身的可见性。
您还可以在要分组的每个控件的标记属性中添加一个值,然后在 VBA 中循环遍历该控件并检查该值并更改那里的可见性。
将要分组的所有控件的 tag 属性设置为 groupABC 或任何您想要的值。
然后在代码中的某个地方使用它来循环表单控件并检查它。
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.
Set the tag property of all the controls you want to group to something like groupABC or whatever you wish.
Then somewhere in your code use this to loop through the form controls and check for it.
为了详细说明我对使用自定义集合的评论,您需要在表单的模块中执行类似的操作:
要隐藏控件,您需要执行以下操作:
并显示它们:
这很简单,不是吗?
这是我一直在我的应用程序中使用的代码,自从大约 10 年前第一次实现它以来,我就一直在使用它,并且注意到使用自定义集合比遍历整个控件集合要好。
唯一需要注意的是,如果其中一个控件具有焦点,如果您尝试隐藏它,则会出错。这很容易解决,因为如果您隐藏一组控件,那么在执行此操作之前肯定有一个合适的位置来设置焦点。
To elaborate on my comment on using custom collections, you'd do something like this in your form's module:
To hide the controls, you'd do this:
And to show them:
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.
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.
我喜欢 Joel Gauvreau 建议的标签属性。其他可能性包括选项卡控件和/或子表单。
I like the tag property suggested by Joel Gauvreau. Other possibilities include a tab control and / or subforms.
更有效的方法是将所有相关控件添加到选项卡控件中。您使用单个页面创建一个 Tab 控件,然后将 Tab 控件的
Style
设置为None
,并将Back Style
设置为Transparent.这样,Tab 控件的视觉外观就会消失,但其分组语义仍然存在。然后将控件添加到页面,可以在内部创建它们,也可以将现有控件剪切+粘贴到页面中(请记住在粘贴后重新启用事件过程)。
如果只是可见性问题,现在您只需设置 Page 控件的
Visible
属性,属于该控件的所有控件都会显示或隐藏。但是,如果您需要对每个分组控件执行更详细的操作,则可以使用以下方法迭代它们:
您还可以使用 .Tag 或 .Type 属性对这些控件执行不同的操作。
A more efficient approach is to add all related controls to a Tab Control. You create a Tab control with a single Page, and then set the Tab control
Style
toNone
andBack Style
toTransparent
. This way the visual appearance of a Tab control fades away, but its grouping semantics remain. Then add controls to the Page, either creating them inside or Cut+Paste existing controls into the page (remember to reenable event procedures after pasting).If it’s just a matter of visibility, now you can just set the
Visible
property of the Page control and all controls that belong to it will show or hide.However, if you need to do something more elaborate to each grouped control, you can iterate through them with:
You can also use the .Tag or .Type properties to do different things to these controls.