VBA CommandBar不执行OnAction子例程

时间:2019-05-16 08:26:22

标签: ms-access access-vba

以下代码在我的Access应用程序中没有执行任何操作。 我希望运行Subrutine TestMacro

它显示菜单,但选择后完全没有反应。

有什么主意吗?

Private Sub Check_Status_Click()
  Set The_Menu = CreateSubMenu
  The_Menu.ShowPopup
End Sub


Function CreateSubMenu() As CommandBar
  Const pop_up_menu_name = "Pop-up Menu"
  Dim the_command_bar As CommandBar
  Dim the_command_bar_control As CommandBarControl

  Set the_command_bar = CommandBars.Add(Name:=pop_up_menu_name, Position:=msoBarPopup, MenuBar:=False, Temporary:=False)
  Set the_command_bar_control = the_command_bar.Controls.Add
  the_command_bar_control.Caption = "Run Macro Hello World!"
  the_command_bar_control.OnAction = "TestMacro"
  Set CreateSubMenu = the_command_bar
End Function


Public Sub TestMacro()
  MsgBox "Hello World"
End Sub

1 个答案:

答案 0 :(得分:0)

确保TestMacro在公共模块中,而不是在表单模块中。

在添加新菜单之前,还应删除现有菜单(如果存在)

On Error Resume Next
CommandBars.Item(pop_up_menu_name).Delete
On Error GoTo 0

否则,您会得到一个错误