how to determine which item of a contextmenu within a form was clicked?

  • Thread starter Karl-thomas.schmidt
  • Start date
K

Karl-thomas.schmidt

Hi @ all

wrote some code who ties a popupmenu to a dblclick of a
listbox. the listbox
is filled with values from a table

dim Taxis as Worksheet
set Taxis = Worksheets("table"
Dim myBar As CommandBar
Dim myBttn As CommandBarButton

CommandBars("myKontext").Delete
On Error GoTo handler
Set myBar = Application.CommandBars.Add("myKontext",
msoBarPopup)


Do Until IsEmpty(Taxis.Cells(i, 1))
Set myBttn = myBar.Controls.Add
myBttn.Caption = Taxis.Cells(i, 1).Value
i = i + 1
Loop


This seems to be OK

in the form the contextmenu is linked to the listfield
with:

Private Sub lstAktAuftraege_DblClick(ByVal Cancel As
MSForms.ReturnBoolean)
CommandBars("myKontext").ShowPopup
End Sub

this leeds to a runtime error 80004005

any ideas?
any samples?

best regards..
Kalle
 
D

Dick Kusleika

Kalle

The problem has to do with what I call roll-up objects. When you use
CommandBars by itself (as opposed to Application.CommandBars), you are
implicitly qualifying it with its default roll-up object. The defualt roll
up object will be determined by where your code is, i.e. which module. In a
standard module, the default roll up for CommandBars is the Application
object, so this line:
CommandBars("myKontext").Delete

is like saying

Application.CommandBars("myKontext").Delete

In a class module (like a module behind a userform), the default roll up is
the class itself. So this line
Set myBar = Application.CommandBars.Add("myKontext",
msoBarPopup)

works fine because you have fully qualified it with the Application object,
but this line:
CommandBars("myKontext").ShowPopup

doesn't work, because it's the same as saying

Userform1.CommandBars("myKontext").ShowPopup

To fix it, fully qualify all of your CommandBars properties with
Application. I know that's a long winded explanation to just say "put
Application in front of it", but I thought you might like to know.
 
Top