1
我正在使用此代碼爲我的數據表窗體(Access 2007)創建右鍵單擊菜單。此代碼在打開事件數據表子窗體運行:從右鍵單擊CommandBar菜單打開當前記錄
Dim sMenuName As String
sMenuName = "DatasheetRightClickMenu"
On Error Resume Next
CommandBars(sMenuName).Delete
If Err.Number <> 0 Then Err.Clear
On Error GoTo 0
Dim cmb As Office.CommandBar
Dim cmbItem
Set cmb = CommandBars.Add(sMenuName, _
msoBarPopup, False, False)
Set cmbItem = cmb.Controls.Add(msoControlButton, , , , True)
With cmbItem
.Caption = "Open"
.OnAction = "=OpenDetails()"
End With
Me.ShortcutMenu = True
Me.ShortcutMenuBar = sMenuName
我無法弄清楚如何將當前記錄的ID傳遞給OpenDetails功能。如果我能弄清楚如何傳遞表單或記錄集變量/引用,我會很高興,但我似乎無法弄清楚如何做到這一點。
將「實時」參數或參數從右鍵菜單傳遞到自定義函數的技巧是什麼?用戶點擊時是否必須構建右鍵單擊菜單?或者有更好的方法來做到這一點?
EDIT1:
這是我走到這一步的工作是什麼:
Private Sub Form_Current()
Call CreateRightClickMenu
End Sub
Private Sub CreateRightClickMenu()
Dim sMenuName As String
sMenuName = Me.Name & "RClickMenu"
On Error Resume Next
CommandBars(sMenuName).Delete
If Err.Number <> 0 Then Err.Clear
On Error GoTo 0
Dim cmb As Office.CommandBar
Dim cmbItem
Set cmb = CommandBars.Add(sMenuName, _
msoBarPopup, False, False)
Dim s1() As String, s2 As String
If Nz(Me.txtitemdesc, "") <> "" Then
s2 = Me.txtitemdesc & " "
s2 = Replace(s2, ",", " ")
s1 = Split(s2, " ")
s2 = s1(0)
End If
Set cmbItem = cmb.Controls.Add(msoControlButton, , , , True)
With cmbItem
.Caption = "Open " & Replace(Me.txtitemdesc, "&", "&&")
.Parameter = Me!ItemID
.OnAction = "OpenFromDatasheetRightClick"
End With
Set cmbItem = cmb.Controls.Add(msoControlButton, , , , True)
With cmbItem
.FaceId = 640
.Caption = "Filter = '" & s2 & "'"
.Parameter = s2
.OnAction = "FilterAllItemsDatasheet"
End With
If Me.FilterOn = True And Me.Filter <> "" Then
Set cmbItem = cmb.Controls.Add(msoControlButton, , , , True)
With cmbItem
.Caption = "Clear Filter"
.Parameter = ""
.OnAction = "FilterAllItemsDatasheet"
End With
End If
Me.ShortcutMenu = True
Me.ShortcutMenuBar = sMenuName
End Sub
看來,我的回調函數必須是一個公共模塊,而不是形式的模塊中。
Public Sub FilterAllItemsDatasheet()
Dim cbar As CommandBarControl
Set cbar = CommandBars.ActionControl
If cbar Is Nothing Then
Debug.Print "CBar is nothing"
Exit Sub
End If
Dim s1
s1 = cbar.Parameter
If s1 = "" Then
Call Forms("frmAllItemsDatasheet").ClearFilter
Else
Forms("frmAllItemsDatasheet").cboSearch = s1
Call Forms("frmAllItemsDatasheet").UpdateSubform
End If
End Sub
Public Sub OpenFromDatasheetRightClick()
Dim cbar As CommandBarControl
Set cbar = CommandBars.ActionControl
If cbar Is Nothing Then
Debug.Print "CBar is nothing"
Exit Sub
End If
Dim s1
s1 = cbar.Parameter
Call OpenItemDetailForm(s1)
Forms("frmAllItemsDatasheet").SetFocus
End Sub
我得到一個錯誤:對象不包含自動化對象「ID 」。我嘗試了幾個不同的領域,他們都有同樣的問題。 – HK1
我使用上面的代碼,數據表格表單和Open事件進行了測試。我使用2010年,但與2007年的菜單幾乎相同。我想知道是否值得使用臨時數據庫和新表單進行嘗試?當您在相同的表單上工作了一段時間並且它已經損壞時,有時會出現此錯誤。 'Screen.Activeform。[somesuitablefield]'會返回任何東西嗎? – Fionnuala
你把OpenDetails功能放在哪裏?表單模塊或代碼模塊? – HK1