2016-03-08 3 views
1

Как создать контекстное меню в ms-доступе? В контекстном меню я имею в виду, когда пользователь щелкает правой кнопкой мыши и появляется меню.Как создать контекстное меню в ms-доступе?

Ниже приведен код, который я написал для создания этого контекстного меню, но получаю сообщение об ошибке.

Ошибка компиляции: Пользовательский тип не определен

On Line: Dim cmbRightClick Как Office.CommandBar

Option Compare Database 
Option Explicit 

Sub CreateReportShortcutMenu() 
    Dim cmbRightClick As Office.CommandBar 
    Dim cmbControl As Office.CommandBarControl 

    ' Create the shortcut menu. 
    Set cmbRightClick = CommandBars.Add("cmdReportRightClick", msoBarPopup, False, True) 

    With cmbRightClick 

     ' Add the Print command. 
     Set cmbControl = .Controls.Add(msoControlButton, 2521, , , True) 
     ' Change the caption displayed for the control. 
     cmbControl.Caption = "Quick Print" 

     ' Add the Print command. 
     Set cmbControl = .Controls.Add(msoControlButton, 15948, , , True) 
     ' Change the caption displayed for the control. 
     cmbControl.Caption = "Select Pages" 

     ' Add the Page Setup... command. 
     Set cmbControl = .Controls.Add(msoControlButton, 247, , , True) 
     ' Change the caption displayed for the control. 
     cmbControl.Caption = "Page Setup" 

     ' Add the Mail Recipient (as Attachment)... command. 
     Set cmbControl = .Controls.Add(msoControlButton, 2188, , , True) 
     ' Start a new group. 
     cmbControl.BeginGroup = True 
     ' Change the caption displayed for the control. 
     cmbControl.Caption = "Email Report as an Attachment" 

     ' Add the PDF or XPS command. 
     Set cmbControl = .Controls.Add(msoControlButton, 12499, , , True) 
     ' Change the caption displayed for the control. 
     cmbControl.Caption = "Save as PDF/XPS" 

     ' Add the Close command. 
     Set cmbControl = .Controls.Add(msoControlButton, 923, , , True) 
     ' Start a new group. 
     cmbControl.BeginGroup = True 
     ' Change the caption displayed for the control. 
     cmbControl.Caption = "Close Report" 
    End With 

    Set cmbControl = Nothing 
    Set cmbRightClick = Nothing 
End Sub 

Private Sub Report_Load() 
    CreateReportShortcutMenu 
End Sub 
+0

Это путь над моей головой, но некоторые исследования, кажется, указывают, что вам нужно объявить '.CommandBar' на уровне класса. (https://msdn.microsoft.com/en-us/library/scff9c7c%28v=vs.90%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1) – MoondogsMaDawg

+0

Нет, они являются точными как локальные переменные. – Andre

ответ

1

В редакторе VBA, откройте меню Сервис -> Ссылки и проверки ссылка Библиотека объектов Microsoft Office xx.0 (с xx, являющейся вашей версией Office, например 14 для Office 2010).

С помощью этой ссылки Access распознает Office.CommandBar, и ошибка компиляции исчезнет.

Но: вам нужно будет добавить некоторые вещи, чтобы сделать эту работу.

Созданный вами постоянный код команды "cmdReportRightClick" постоянно хранится в базе данных. Вам необходимо будет удалить его при закрытии отчета:

CommandBars("cmdReportRightClick").Delete 

или проверить его наличие перед его созданием.

И вы должны назначить его доклада:

Private Sub Report_Load() 
    CreateReportShortcutMenu 
    Me.ShortcutMenuBar = "cmdReportRightClick" 
End Sub 
Смежные вопросы