弹出窗体中的右键菜单

时间:2013-12-12 22:36:35

标签: vba ms-access access-vba ms-access-2010

我是这个伟大论坛的新手。 我正在尝试在列表框上执行右键菜单。 目前我正在尝试实现一个右键菜单,它将显示一个简单的信息框。

我的问题是列表框位于弹出窗体上,最大化打开了。现在,当我右键单击列表框时,我看到右键菜单,但当我点击其中一个菜单选项时,没有任何事情发生(似乎它没有按照它应该去的功能)。我也在功能上设置了断点,但从不提示。

重要的是要提一下,如果我将表单弹出选项设置为no,则右键单击菜单工作正常(当我点击其中一个选项时,我会看到其匹配的messege框)。

我正在运行以下vba代码:

这是我的列表框的鼠标向上事件处理程序:

Private Sub Song_List_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)

' Call the SetUpContextMenu function to ensure it is setup with most current context
' Note: This really only needs to be setup once for this example since nothing is
' changed contextually here, but it could be further expanded to accomplish this
SetUpContextMenu
' See if the right mouse button was clicked
If Button = acRightButton Then
'DoCmd.CancelEvent
CommandBars("MyListControlContextMenu").ShowPopup
End If
End Sub

设置“SetUpContextMenu”子:

Public Sub SetUpContextMenu()
' Note: This requires a reference to Microsoft Office Object Library
Dim combo As CommandBarControl

' Since it may have been defined in the past, it should be deleted,
' or if it has not been defined in the past, the error should be ignored

On Error Resume Next
CommandBars("MyListControlContextMenu").Delete
On Error GoTo 0

' Make this menu a popup menu
With CommandBars.Add(Name:="MyListControlContextMenu", Position:=msoBarPopup)

' Provide the user the ability to input text using the msoControlEdit type
Set combo = .Controls.Add(Type:=msoControlEdit)
combo.Caption = "Lookup Text:" ' Add a label the user will see
combo.OnAction = "=getText()" ' Add the name of a function to call

' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.BeginGroup = True ' Add a line to separate above group
combo.Caption = "Lookup Details" ' Add label the user will see
combo.OnAction = "=LookupDetailsFunction()" ' Add the name of a function to call

' Provide the user the ability to click a menu option to execute a function
Set combo = .Controls.Add(Type:=msoControlButton)
combo.Caption = "Delete Record" ' Add a label the user will see

combo.OnAction = "=DeleteRecordFunction()" ' Add the name of the function to call"
combo.SetFocus
combo.Visible = True
End With

End Sub

设置全部3功能,在点击时显示不同的信息框:

Public Function getText() As String

getText = CommandBars("MyListControlContextMenu").Controls(" Lookup Text:").Text

' You could optionally do something with this text here,
' such as pass it into another function ...
MsgBox "You typed the following text into the menu: " & getText

End Function

Public Function LookupDetailsFunction() As String

LookupDetailsFunction = "Hello World!"

MsgBox LookupDetailsFunction, vbInformation, "Notice!"

End Function

Public Function DeleteRecordFunction()

' If Not IsNull(Forms!MyFormName.Controls("Song_List").Colu mn(0)) Then
MsgBox "Record Deleted"
' End If

End Function

1 个答案:

答案 0 :(得分:0)

只需将属性表(在另一个选项卡中)中的“快捷方式”菜单栏设置设置为快捷菜单的名称即可。

此时您不需要任何代码来显示/启动您创建的上下文菜单。