en primer lugar crear un evento _MouseUp
a ejecutar en el respectivo co Siga buscando para ver si se hizo clic con el botón derecho del mouse y, de ser así, llame al método .ShowPopup
.
Por supuesto, esto supone que el
Private Sub MyListControlName_MouseUp(ByVal Button As Integer, _
ByVal Shift As Integer, _
ByVal X As Long, ByVal Y As Long)
' 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
CommandBars("MyListControlContextMenu").ShowPopup
End If
End Sub
Dado que en este punto la barra de comandos MyListControlContextMenu
es indefinido, defino el menú en un módulo separado de la siguiente manera:
Public Sub SetUpContextMenu()
' Note: This requires a reference to Microsoft Office Object Library
Dim combo As CommandBarComboBox
' 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
End With
End Sub
Desde tres funciones han sido referenciado, podemos pasar a definir estos de la siguiente manera:
getText: Tenga en cuenta, esto La opción requiere una referencia tanto al nombre del nombre de menú de la barra de comandos como al nombre del título de control.
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
LookupDetailsFunction: Para este ejemplo, voy a crear una función de shell y devolver el texto "Hello World!".
Public Function LookupDetailsFunction() As String
LookupDetailsFunction = "Hello World!"
MsgBox LookupDetailsFunction, vbInformation, "Notice!"
End Function
DeleteRecordFunction: Para este ejemplo, voy a garantizar el control sigue siendo válida comprobando contra nulo, y si sigue siendo válido, se ejecutará una consulta para retirar el registro de una tabla.
Public Function DeleteRecordFunction() As String
If Not IsNull(Forms!MyFormName.Controls("MyListControlName").Column(0)) Then
Currentdb.Execute _
"DELETE * FROM [MyTableName] " & _
"WHERE MyKey = " & Forms!MyFormName.Controls("MyListControlName").Column(0) & ";"
MsgBox "Record Deleted", vbInformation, "Notice!"
End If
End Function
Nota: Para LookupDetailsFunction
, DeleteRecordFunction
y getText
funciones, éstas deben estar dentro de un ámbito público para que funcione correctamente.
Finalmente, el último paso es probar el menú. Para hacerlo, abra el formulario, haga clic con el botón derecho en el control de lista y seleccione una de las opciones del menú emergente.
Opcionalmente button.FaceID
se puede utilizar para indicar un icono de oficina conocido para asociar con cada instancia del control emergente de menú.
Encontré Pillai Shyam's work en la creación de un complemento de navegador FaceID para ser muy útil.
Referencias: Microsoft FaceID
La respuesta depende en gran medida de la versión de Access. Si A2007, una respuesta, cualquier versión anterior, una respuesta completamente diferente. –