从 Access 中的命令按钮运行 VBA 子程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26062723/
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
Run a VBA sub from command button in Access
提问by jia103
I'm having trouble attaching a VBA macro to a form button in MS Access. I have my public sub Demo defined in a Module1 under VBA. In Access, I ensured macros are enabled because I can click in the VBA window and run my macro by pressing F5.
我在将 VBA 宏附加到 MS Access 中的表单按钮时遇到问题。我在 VBA 下的 Module1 中定义了我的公共子演示。在 Access 中,我确保启用了宏,因为我可以在 VBA 窗口中单击并按 F5 运行我的宏。
In Access, I created a new blank form (Ribbon > Create > Forms > Blank Form). On the form, I went into Design View. Then Ribbon > Design > Controls > Ensure "Use Control Wizards" is pressed.
在 Access 中,我创建了一个新的空白表单(功能区 > 创建 > 表单 > 空白表单)。在表单上,我进入了设计视图。然后功能区 > 设计 > 控件 > 确保按下“使用控件向导”。
Then I pressed Button and dropped a new command button onto my form. In the Command Button Wizard, I clicked Miscellaneous under Categories, and then Run Macro under Actions, and pressed Next.
然后我按下 Button 并将一个新的命令按钮放到我的表单上。在命令按钮向导中,我单击类别下的杂项,然后单击操作下的运行宏,然后按下一步。
I expected to see a list of modules or macros listed in the list on the following screen that prompts, "What macro would you like the command button to run?"
我希望在以下屏幕上看到列表中列出的模块或宏列表,提示“您希望命令按钮运行什么宏?”
Instead, the list is empty. Where do I find the macro to add to the command button?
相反,该列表是空的。在哪里可以找到要添加到命令按钮的宏?
采纳答案by Johnny Bones
- Go into the Design View of your form.
- Press the ALT key and the F11 key simultaneously, and you will see the VBA Code window.
Find your form and double-click it, which will bring up the code behind your form.
Find your button, and put this piece of code in it:
DoCmd.RunMacro "YourMacroName"
- 进入表单的设计视图。
- 同时按下 ALT 键和 F11 键,您将看到 VBA 代码窗口。
找到您的表单并双击它,这将显示表单背后的代码。
找到您的按钮,并将这段代码放入其中:
DoCmd.RunMacro "YourMacroName"
Obviously, replace "YourMacroName" with the name of your macro, but keep it inside the double quotes.
显然,将“YourMacroName”替换为您的宏的名称,但将其保留在双引号内。