Đầu tiên hãy tạo một sự kiện _MouseUp
để thực thi trên đồng tương ứng ntrol tìm cách xem nút chuột phải có được nhấp hay không và nếu có, hãy gọi phương thức .ShowPopup
.
Tất nhiên điều này giả định
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
Kể từ thời điểm này Command Bar MyListControlContextMenu
là undefined, tôi xác định các Menu trong một module riêng biệt như sau:
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
Kể từ khi ba chức năng đã được tham chiếu, chúng tôi có thể chuyển sang để xác định những điều sau:
getText: Lưu ý, điều này tùy chọn yêu cầu tham chiếu đến cả tên của tên menu Lệnh thanh công cụ cũng như tên của chú thích kiểm soát .
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: Ví dụ này, tôi sẽ tạo ra một chức năng vỏ và trả lại văn bản "Hello World!".
Public Function LookupDetailsFunction() As String
LookupDetailsFunction = "Hello World!"
MsgBox LookupDetailsFunction, vbInformation, "Notice!"
End Function
DeleteRecordFunction: Ví dụ này, tôi sẽ đảm bảo sự kiểm soát vẫn còn hợp lệ bằng cách kiểm tra nó chống lại null, và nếu vẫn còn hiệu lực, sẽ thực hiện một truy vấn để loại bỏ các bản ghi từ một bảng.
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
Lưu ý: Đối với LookupDetailsFunction
, DeleteRecordFunction
và getText
chức năng, những phải nằm trong một phạm vi công cộng để làm việc một cách chính xác.
Cuối cùng, bước cuối cùng là kiểm tra menu. Để thực hiện việc này, hãy mở biểu mẫu, nhấp chuột phải vào điều khiển danh sách và chọn một trong các tùy chọn từ trình đơn bật lên.
Tùy chọn button.FaceID
có thể được sử dụng để biểu thị biểu tượng văn phòng đã biết liên kết với từng trường hợp kiểm soát bật lên menu.
Tôi đã tìm thấy Pillai Shyam's work khi tạo Add-In FaceID trình duyệt rất hữu ích.
Tài liệu tham khảo: Microsoft FaceID
Câu trả lời phụ thuộc rất nhiều vào phiên bản của Access. Nếu A2007, một câu trả lời, bất kỳ phiên bản trước nào, một câu trả lời hoàn toàn khác nhau. –