vba 隐藏在 ms access 2007 中具有焦点的控件

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

hiding a control which has focus in ms access 2007

formsms-accessvbacomboboxms-access-2007

提问by user1175126

I have a combobox on a form. Clicking on a particular label should hide this combobox. The problem is that if the combobox has focus, clicking on the button which hides this combobox gives error.How can I resolve this runtime error?

我在表单上有一个组合框。单击特定标签应隐藏此组合框。问题是,如果组合框具有焦点,单击隐藏此组合框的按钮会出现错误。如何解决此运行时错误?

回答by Fionnuala

Move the focus. If necessary, create a very small control to receive the focus.

移动焦点。如有必要,创建一个非常小的控件来接收焦点。

Me.SomeControlThatIsNotTheCombobox.SetFocus

Re Comments

重新评论

Note that this label is not associated with a control.

请注意,此标签与控件无关。

Private Sub Label1_Click()
   Me.Text1.SetFocus
   Me.Label1.Visible = False
End Sub

回答by ChrisPadgham

Rather than setting focus to any particular control, which may cause maintenance issues in the future if the controls on the form change, if you simulate a key press of Tab then focus will move to the next object in the tab order.

而不是将焦点设置到任何特定控件,如果表单上的控件更改,这可能会导致将来的维护问题,如果您模拟 Tab 键按下,则焦点将移动到 Tab 顺序中的下一个对象。

SendKeys "{TAB}"
DoEvents
Me.Command4.Visible = False

Note the doevents is necessary to allow the processing of the Tab.

注意 doevents 是允许处理 Tab 所必需的。

回答by RSDD

I know this is an old post, but I recently just ran into a similar issue (and this post was in the first 4 or 5 results). If the control you're trying to disable is the first on the subform, try setting its Tab Index to 1, not 0. As soon as the subform gets the focus, the first control on it does, too. I was trying to set this during a Form_Open event, and this solved it.

我知道这是一篇旧帖子,但我最近遇到了一个类似的问题(这篇文章出现在前 4 或 5 个结果中)。如果您要禁用的控件是子窗体上的第一个控件,请尝试将其 Tab Index 设置为 1,而不是 0。一旦子窗体获得焦点,其上的第一个控件也将获得焦点。我试图在 Form_Open 事件期间设置它,这解决了它。