VBA MS Access 代码无法运行

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

VBA MS Access code wont run

vbams-accessms-access-2007access-vba

提问by Bawn

I am very new to vba and I am trying to get my first bit of VBA code to run, I have added a button to a form. I then opened the on click event and placed

我对 vba 很陌生,我正在尝试运行我的第一个 VBA 代码,我在表单中添加了一个按钮。然后我打开点击事件并放置

Private Sub run_Click()
MsgBox "hello World"
End Sub`

into the module but when I go back into the form and click the button in form view nothing happens, I am doing something wrong, i have tryed to run lots of other sniptes of code but nothing is working, I am kinda unsure how I actaully "run" the code.

进入模块,但是当我返回表单并单击表单视图中的按钮时,什么也没有发生,我做错了,我尝试运行了许多其他代码片段,但没有任何效果,我有点不确定我是怎么做的“运行”代码。

enter image description here

在此处输入图片说明

I Click run then I get this

我点击运行然后我得到这个

enter image description here

在此处输入图片说明

I have gone to the trust centre and Enabled Macros and still I get this error ??

我已经去了信任中心并启用了宏,但仍然出现此错误??

// This issue has been solved my marcos were not enabled in the trust centre

// 这个问题已经解决了我的 marcos 没有在信任中心启用

回答by Gord Thompson

If this is a brand new database then it may not be "trusted" yet. Exit Access completely, then try opening the database again. if you see a warning like...

如果这是一个全新的数据库,那么它可能尚未“受信任”。完全退出 Access,然后再次尝试打开数据库。如果您看到类似...的警告

Security Warning: Some active content has been disabled.

安全警告:某些活动内容已被禁用。

...along with an "Enable Content" button then click that button (to enable macros and VBA code) and then try your form again.

...连同“启用内容”按钮,然后单击该按钮(以启用宏和 VBA 代码),然后再次尝试您的表单。

回答by Steven

When MS Access first opens on your desktop and it has VBA within, you'll need to enable that code. In MS Access 2010, File --> Options --> Trust Center --> Macro Settings as well as ActiveX Settings. The security nanny-statists have set the default so VBA will not run unless you specifically allow it.

当 MS Access 首次在您的桌面上打开并且其中包含 VBA 时,您需要启用该代码。在 MS Access 2010 中,文件 --> 选项 --> 信任中心 --> 宏设置以及 ActiveX 设置。安全保姆统计员已设置默认值,因此除非您明确允许,否则 VBA 将不会运行。

回答by Bawn

In your VBA editor do this:

在您的 VBA 编辑器中执行以下操作:

  1. Create a userform and rename it to yourFormNameRename your form
  2. Create a button on the form and rename it to yourButtonRename your button
  3. Double-click your button and paste the below
    Private Sub yourButton_Click()
    MsgBox "hello world"
    Me.Hide
    End Sub

  4. Insert a module and paste this code in
    Sub RunMain()
    With yourFormName
    yourFormName.Show
    Unload yourFormName
    End With
    End Sub

  5. Run the RunMain macro and then click the button!
  1. 创建一个用户表单并将其重命名为yourFormName重命名表单
  2. 在表单上创建一个按钮并将其重命名为yourButton重命名您的按钮
  3. 双击您的按钮并粘贴以下内容
    Private Sub yourButton_Click()
    MsgBox "hello world"
    Me.Hide
    End Sub

  4. 插入一个模块并将此代码粘贴到
    Sub RunMain()
    With yourFormName
    yourFormName.Show
    Unload yourFormName
    End With
    End Sub

  5. 运行 RunMain 宏,然后单击按钮!