vba 关闭当前表单并在 Access 中打开另一个表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21115348/
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
Close current form and open a different one in Access?
提问by Yarden
I'm trying to create a macro that closes the current form and opens the main form.
我正在尝试创建一个关闭当前表单并打开主表单的宏。
Since I have so many forms and I need the same button with this macro in all of them, I want to create a macro just once and not for each form separately.
由于我有很多表单,并且我需要在所有表单中使用带有此宏的相同按钮,因此我只想创建一个宏,而不是分别为每个表单创建一个宏。
Is there any way to do so?
有什么办法吗?
回答by Blackhawk
Create a code module and add the following function:
创建一个代码模块并添加以下函数:
Public Sub CloseMeAndOpenMain(frmMe As Form)
DoCmd.Close acForm, frmMe.Name
DoCmd.OpenForm "frmMain" 'Replace this with the actual name of your Main form
End Sub
Now, each time you have a button that should close the current form and open the "frmMain" form, you would write the click event handler like so:
现在,每次您有一个按钮应该关闭当前表单并打开“frmMain”表单时,您可以像这样编写点击事件处理程序:
Private Sub btnCloseForm_Click()
CloseMeAndOpenMain Me
End Sub
Me
is a reference to the current form, and each form has a Name
property. Therefore you can write a function to close it without having to hardcode the name as a String (except of course for the main form).
Me
是对当前表单的引用,每个表单都有一个Name
属性。因此,您可以编写一个函数来关闭它,而不必将名称硬编码为字符串(当然主窗体除外)。