使用 VBA 在 Microsoft Access 2007 数据表中隐藏列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1717508/
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-11 10:58:38  来源:igfitidea点击:

Hiding columns in a Microsoft Access 2007 datasheet with VBA

ms-accessvbams-access-2007access-vba

提问by Skywalker

I am trying to hide specific columns in an Access 2007 split form through code. I need the form to check certain conditions to see whether it needs to display a column or not. I have code in the form's 'Activate' event to hide the column like this:

我试图通过代码隐藏 Access 2007 拆分表单中的特定列。我需要表单来检查某些条件以查看它是否需要显示一列。我在表单的“激活”事件中有代码来隐藏这样的列:

txtControl.ColumnHidden = True

This code works in the "Open" event, but if I hide the column on Activate, it won't display these changes until I close the form and open it again. I have tried calling the form's refresh, repaint, and requery methods, but this doesn't work. Please help!

此代码适用于“打开”事件,但如果我在“激活”上隐藏该列,则在我关闭表单并再次打开它之前,它不会显示这些更改。我曾尝试调用表单的刷新、重绘和重新查询方法,但这不起作用。请帮忙!

Edit:Ideally, I need this event to occur whenever the focus switches to this form.That's why I'm using the Activate event rather than the Open event.

编辑:理想情况下,每当焦点切换到此表单时,我都需要发生此事件。这就是我使用 Activate 事件而不是 Open 事件的原因。

回答by Dale

Try setting it in either the form's Currentor Loadevents.You will also probably need to requery the control after setting that property: Me.TextControl.RequeryCurrent is called every time a form's record is changed, the form is repainted or requeried. Load, as its name suggests, is called once, after the form has opened when the form loads its records. These have always been more reliable for me than using Activate, which really has to do with more the focus of the form, not really what you want.

尝试在窗体的CurrentLoad事件中设置它。您可能还需要在设置该属性后重新查询控件:Me.TextControl.Requery每次更改表单记录、重新绘制或重新查询表单时都会调用 Current。Load,顾名思义,在窗体加载其记录时打开窗体后调用一次。这些对我来说一直比使用Activate更可靠,这实际上与表单的更多焦点有关,而不是您真正想要的。

回答by Ben McCormack

I've had a problem like this before working in Access 2002. I was able to solve the problem with a subform by setting the subform source object equal to itself and then running the requery.

在使用 Access 2002 之前,我遇到过这样的问题。我能够通过将子表单源对象设置为等于自身然后运行重新查询来解决子表单的问题。

Me.SubForm.SourceObject = Me.SubForm.SourceObject
Me.SubForm.Requery

See if this technique works for your particular situation.

看看这种技术是否适用于您的特定情况。