2009-04-20 73 views
7

目標是創建可以與MS Access窗體上的某些控件一起使用的菜單,並且可以右鍵單擊該控件,例如在列表框和相關的上下文特定的菜單彈出選項,如果點擊,將觸發預定義的子程序或功能。如何將菜單項添加到默認的右鍵單擊上下文菜單

什麼是最好的方法來完成這個編程?

我正在使用MS Access 2003,並希望使用VBA進行此操作。

+0

答案在很大程度上取決於接入的版本。如果A2007,一個答案,任何以前的版本,一個完全不同的答案。 – 2009-04-21 06:38:03

回答

13

首先創建一個_MouseUp事件以在各自的co上執行看看是否點擊了鼠標右鍵,如果是,請致電.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 

由於三個功能已參考我們可以繼續定義這些如下 -

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:在這個例子中,我將創建一個shell函數,並返回文本 「Hello World!」。

Public Function LookupDetailsFunction() As String 

    LookupDetailsFunction = "Hello World!" 

    MsgBox LookupDetailsFunction, vbInformation, "Notice!" 

End Function 

DeleteRecordFunction:在這個例子中,我將確保控制仍然是通過檢查其對空有效,如果仍然有效,將執行一個查詢,以從表中刪除記錄。

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 

注:對於LookupDetailsFunctionDeleteRecordFunctiongetText功能,這些必須在一個公開的範圍才能正常工作。

最後,最後一步是測試菜單。爲此,請打開表單,右鍵單擊列表控件並從彈出菜單中選擇一個選項。

可選地,button.FaceID可用於指示與菜單彈出控件的每個實例相關聯的已知辦公圖標。

我發現Pillai Shyam's work創建一個FaceID瀏覽器加載項非常有幫助。

參考文獻: Microsoft FaceID

2

爲了使用包含默認操作和自定義操作的菜單替換默認快捷菜單,必須創建包含默認操作的自定義快捷菜單。無法擴展默認的快捷菜單。

Access 2003和之前的快捷菜單是一種特殊的工具欄。您創建它們的方式與您創建自定義工具欄的方式相同(或多或少)。然而,用戶界面有點奇怪,因爲有一個特殊的地方可以創建它們。

要開始使用,請右鍵單擊前端Access MDB中的工具欄。選擇CUSTOMIZE。在工具欄列表中,勾選SHORTCUT MENUS。這將爲您提供所有內置快捷菜單的列表,只是它們實際上並不像實際使用的那樣。例如,如果右鍵單擊窗體上,你得到這個快捷菜單:

Form Design 
Datasheet View 
PivotTable View 
PivotChart View 
Filter By Form 
Apply Filter/Sort 
Remove Filter/Sort 
Cut 
Copy 
Paste 
Properties 

現在,哪裏是在快捷菜單上該菜單?那麼,這個恰好是FORM VIEW TITLE BAR菜單,即使它在任何時候點擊窗體上的控件以外的任何地方彈出。因此,如果這是您想要更改的菜單,您可以通過向其添加菜單項(拖放操作)對其進行編輯。

我認爲它實際上更好(正如我上面所說的)創建一個自定義快捷菜單,複製內置菜單並添加增強功能,因爲這樣可以保留Access默認快捷菜單,同時還可以自定義版本它可供您在需要時使用。在這種情況下,您需要啓動一個新的快捷菜單,這裏是用戶界面不可思議的地方:

您點擊快捷菜單上的最後一個選項,CUSTOM。你看它下降了一個佔位符。你不能拖/放到它。相反,您必須在主工具欄編輯窗口中單擊「新建」,然後創建一個新的快捷工具欄(給它起想要的自定義快捷菜單的名稱)。您的新工具欄現在顯示在工具欄列表中。突出顯示它並單擊「屬性」,然後將類型更改爲POPUP。這會給你提供一個有用的警告,告訴你這種改變將它從工具欄改變爲快捷菜單。然後,您可以關閉工具欄/快捷菜單的屬性表,現在如果再次查看SHORTCUT MENUS並查看CUSTOM菜單,則會看到新創建的菜單。現在,您可以將內置菜單的菜單項拖放到新菜單中 - 但不要將它們放在菜單本身上,而是放在菜單名稱右側的彈出框中的佔位符處。

然後,您可以從任何菜單或工具欄中將所需的任何選項拖放到自定義菜單中。

我假設你知道如何使用快捷菜單,因爲它是所有表單對象屬性表的一部分。

UPDATE 2009/05/21: Access 2007官方博客剛剛在Access 2007中發佈了一個article on doing this programmatically。由於功能區界面,將會有差異,但有些事情會相同。

+0

Access是否按照Excel,Word等使用Office.CommandBar和Office.CommandBarButton對象? – onedaywhen 2009-04-22 10:33:53

+0

David, 這對理解Access如何處理命令欄非常有幫助。但是,我正在尋找以編程方式進行的操作。 – 2009-04-22 22:30:31

+0

這是可編程的,但相當挑剔,根據我的經驗。在VBE幫助搜索「程序工具欄」並閱讀那裏的幫助。由於快捷菜單只是一種工具欄形式,因此您可以使用相同的方法。雖然我不會那樣做。我會創建自定義菜單,然後根據需要顯示/隱藏它們上的特定項目。 – 2009-04-22 23:17:01

2

嘗試這個

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 
相關問題