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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:33:57  来源:igfitidea点击:

Close current form and open a different one in Access?

formsvbams-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

Meis a reference to the current form, and each form has a Nameproperty. 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属性。因此,您可以编写一个函数来关闭它,而不必将名称硬编码为字符串(当然主窗体除外)。