目標は、MS Access フォームの特定のコントロールで利用できるメニューを作成し、そのコントロールを右クリックできるようにすることです。定義済みのサブルーチンまたは関数をトリガーします。
プログラムでこれを達成するための最良の方法は何ですか?
MS Access 2003 を使用しており、VBA を使用してこれを行いたいと考えています。
最初_MouseUp
に、それぞれのコントロールで実行するイベントを作成し、マウスの右ボタンがクリックされたかどうかを確認し、クリックされた場合は.ShowPopup
メソッドを呼び出します。
もちろん、これは
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
この時点ではコマンド バーMyListControlContextMenu
は定義されていないため、別のモジュールで次のようにメニューを定義します。
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
3 つの関数が参照されているので、これらを次のように定義することができます。
getText : このオプションには、コマンド バーのメニュー名とコントロールのキャプションの名前の両方への参照が必要です。
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 : この例では、シェル関数を作成し、テキスト "Hello World!" を返します。
Public Function LookupDetailsFunction() As String
LookupDetailsFunction = "Hello World!"
MsgBox LookupDetailsFunction, vbInformation, "Notice!"
End Function
DeleteRecordFunction : この例では、null に対してチェックすることでコントロールがまだ有効であることを確認し、有効な場合はクエリを実行してテーブルからレコードを削除します。
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
注:および関数の場合LookupDetailsFunction
、正しく機能するには、これらがパブリック スコープ内にある必要があります。DeleteRecordFunction
getText
最後に、最後のステップはメニューをテストすることです。これを行うには、フォームを開き、リスト コントロールを右クリックして、ポップアップ メニューからオプションの 1 つを選択します。
必要に応じbutton.FaceID
て、メニュー ポップアップ コントロールの各インスタンスに関連付ける既知のオフィス アイコンを示すために使用できます。
FaceID Browser Add-In の作成に関するPillai Shyam の作業が非常に役立つことがわかりました。
これを試して
Sub Add2Menu()
Set newItem = CommandBars("Form View Popup").Controls.Add(Type:=1)
With newItem
.BeginGroup = True
.Caption = "Make Report"
.FaceID = 0
.OnAction = "qtrReport"
End With
End Sub
ご覧のとおり、「フォーム ビュー ポップアップ」コマンド バーにアイテムが追加され、このアイテムがクリックされると、プロシージャqtrReportがロードされます。
この機能を使用して、Access のすべてのコマンド バーを表示します。
Sub ListAllCommandBars()
For i = 1 To Application.CommandBars.Count
Debug.Print Application.CommandBars(i).Name
Next
End Sub