command bar sub menu problems (1 Viewer)

stuartam

Registered User.
Local time
Today, 18:38
Joined
Jul 16, 2004
Messages
93
Can anyone help, i have written a "few" functions to allow me to create menus but when i try to add a button to a sub menu i get this error:

Run time error 5

invalid proceadure or call

any ideas, this has got me really stumped.

thanks

Function to create main menu bar

Code:
Public Function CreateMenu(MenuName As String)
    Dim menubar As CommandBar
    If fIsCreated(MenuName) Then
        Application.CommandBars(MenuName).Delete
    End If
    Set menubar = Application.CommandBars.Add(MenuName, msoBarTop, True, False)
    Application.CommandBars(MenuName).Visible = True
End Function

function to create menu popup items

Code:
Public Function AddMenuMainItem(MenuName As String, ItemType As String, ItemCaption As String, Optional OnAction As String)
    Dim menubar As CommandBar
    Dim MenuItem As CommandBarControl
    Set menubar = Application.CommandBars.ActiveMenuBar
    Set MenuItem = menubar.Controls.Add(msoControlPopup)
    MenuItem.Caption = ItemCaption
End Function

function to create buttons on popup menus

Code:
Public Function AddMenuSubItem(MenuName As String, MainItemName As String, ItemType As String, ItemCaption As String, ItemIcon As Long, Optional OnAction As String)
    Dim menubar As CommandBar
    Dim MenuItem As CommandBarButton
    Dim SubMenuItem As CommandBarPopup
    Dim mycontrol As CommandBarControl
    Select Case ItemType
        Case "msoControlButton"
            Set menubar = Application.CommandBars(MenuName)
            Set mycontroll = menubar.Controls(MainItemName)
            Set MenuItem = mycontroll.Controls.Add(msoControlButton)
            MenuItem.Caption = ItemCaption
            MenuItem.OnAction = OnAction
            MenuItem.Style = msoButtonAutomatic
            MenuItem.FaceId = ItemIcon
        Case "msoControlPopup"
            Set menubar = Application.CommandBars(MenuName)
            Set mycontroll = menubar.Controls(MainItemName)
            Set SubMenuItem = mycontroll.Controls.Add(msoControlPopup)
            SubMenuItem.Caption = ItemCaption
    End Select
End Function

creating the menu

Code:
Call CreateMenu("Main Menu1")
Call AddMenuMainItem("Main Menu1", "msoControlPopup", "&File")
Call AddMenuSubItem("Main Menu1", "&File", "msoControlButton", "&Print Screen", 4, "=docmd.printout")
Call AddMenuSubItem("Main Menu1", "&File", "msoControlButton", "&Edit", 463, "=docmd.quit")
Call AddMenuMainItem("Main Menu1", "msoControlPopup", "&popup2")
Call AddMenuSubItem("Main Menu1", "&popup2", "msoControlPopup", "&Batchs", 0)
Call AddMenuSubItem("Main Menu1", "&Batchs", "msoControlButton", "cap", 0, "=docmd.openform('frmcap')")
 

MarkK

bit cruncher
Local time
Today, 10:38
Joined
Mar 17, 2004
Messages
8,179
What line causes the error? Do you know how to set breakpoints and step through code?

Your assignment to SubMenuItem references an object called mycontroll, (note the double trailing "ll") which does not appear to be declared anywhere.
 

stuartam

Registered User.
Local time
Today, 18:38
Joined
Jul 16, 2004
Messages
93
Thanks for the reply.
the error is here:

Code:
Set mycontrol = menubar.Controls(MainItemName)

when this line of code is called:

Code:
Call AddMenuSubItem("Main Menu1", "&Batchs", "msoControlButton", "cap", 0, "=docmd.openform('frmcap')")

i corrected the mycontroll mistype ( thanks for spotting it )

i will try and step through the code

thanks
 

MarkK

bit cruncher
Local time
Today, 10:38
Joined
Mar 17, 2004
Messages
8,179
Don't know if you found it yet but this
Code:
Call AddMenuSubItem("Main Menu1", "&Batchs", "msoControlButton", "cap", 0, "=docmd.openform('frmcap')")
should be this
Code:
Call AddMenuSubItem("Main Menu1", [COLOR="DarkRed"]"&popup2"[/COLOR], "msoControlButton", "cap", 0, "=docmd.openform('frmcap')")
 

stuartam

Registered User.
Local time
Today, 18:38
Joined
Jul 16, 2004
Messages
93
thanks for the reply,

"&popup2" - is the popup menu on the main menu

"&Batchs" - is the popup menu on the "&popup2" menu

what i want is

Main menu
| |
File menu Popup2 > batch
bnt1 bnt1 btn1
bnt2 bnt2 btn2

hope that makes sence.

thanks for the help.
 

MarkK

bit cruncher
Local time
Today, 10:38
Joined
Mar 17, 2004
Messages
8,179
Then I think you're going to need an AddMenuSubSubItem() routine which you haven't written yet, becuase this reference...
Code:
Set mycontrol = menubar.Controls("&popup2")
is going to need to resolve to one level deeper in the "menu tree"
Code:
Set mycontrol = menubar.Controls("&popup2").Controls("&Batchs")
See it?

One option is don't pass in strings like you're doing, but rather pass in the last object you created.
Code:
Public Function AddMenu(mName as string) as CommandBar
[COLOR="Green"]  'returns a menu object - delete routine omitted for clarity[/COLOR]
  Set AddMenu = CommandBars.Add(mName, msoBarTop, True, False)
End Function

Public Function AddPopup(cb as CommandBar, caption as string) as CommandBar
  Dim cbp as CommandBarPopup
  Set cbp = cb.controls.add(msoControlPopup)
  cbp.caption = caption
  Set AddPopup = cbp
End Function

Public Sub AddButton(cb as commandbar, caption as string, faceid as long, onaction as string) as CommandBarButton
  Dim cbb as CommandBarButton
  Set cbb = cb.Controls.Add(msoControlButton)
  With cbb
    .Caption = caption
    .OnAction = OnAction
    .Style = msoButtonAutomatic
    .FaceId = faceid
  End With
End Function
Now when you create the menu, hang on to your objects
Code:
Dim cb as commandbar
Dim cbp as commandbarpopup

[COLOR="Green"]'create the menu[/COLOR]
Set cb = AddMenu("Main1")
[COLOR="Green"]'add a popup to the menu[/COLOR]
Set cbp = AddPopup(cb, "&File")
[COLOR="Green"]'add buttons to the popup[/COLOR]
AddButton cbp, "&Print Screen", 4, "=docmd.printout"
AddButton cbp, "&Edit", 463, "=docmd.quit")
[COLOR="Green"]'add a popup to the menu[/COLOR]
Set cbp = AddPopup(cb, "&popup2")
[COLOR="Green"]'add a popup to the popup, not the menu[/COLOR]
Set cbp = AddPopup([COLOR="DarkRed"]cbp[/COLOR], "&Batchs")
[COLOR="Green"]'add button to the popup on the popup[/COLOR]
AddButton cbp, "cap", 0, "=docmd.openform('frmcap')"
[COLOR="Green"]'and you can use the .Parent property to navigate back to "&popup2"
'Set cbp = cbp.parent
[/COLOR]
I haven't tested any of this so beware, but you get the idea.
 

stuartam

Registered User.
Local time
Today, 18:38
Joined
Jul 16, 2004
Messages
93
thanks for the reply, i will have a go with it later today.

thanks again
 

stuartam

Registered User.
Local time
Today, 18:38
Joined
Jul 16, 2004
Messages
93
thanks

thanks it worked great. just have a new problems with combo boxes on menus in getting it to show the selected item in the onaction....
 

IllusionRay

Registered User.
Local time
Today, 13:38
Joined
Jan 9, 2018
Messages
15
Hello stuartam,

I am looking to create a MenuBar for a form and the information on the web is
very limited and vague, i was wondering if you still have the module you created, i was not sure how to put the code in this tread in module and form and if it was debug or not.

im using access 2016 not sure if it still compatible or not but i wish to try it so if you would be so kind to past the module code and form code in 2 block i would appreciate it greatly.

i hope that your still around.
Thx for your time!
 

isladogs

MVP / VIP
Local time
Today, 18:38
Joined
Jan 14, 2017
Messages
18,209
Stuartam was last here in sept 2007 so unlikely to reply.

Command bars were removed in Access 2007.
Newer version use the ribbon but this is MUCH harder to modify
 

IllusionRay

Registered User.
Local time
Today, 13:38
Joined
Jan 9, 2018
Messages
15
Yeah i have seen the ribbon setup but i don't really like it, my forms are all in popup mode and i hide the Access window so ppl see only what they need, i guess i will have to switch on c# in Visual studio so i can do what i want and not what i can lol

hoo well thx ridders for taking the time to answer here.
 

MarkK

bit cruncher
Local time
Today, 10:38
Joined
Mar 17, 2004
Messages
8,179
You would still use CommandBars if you want a popup menu. I still use those on right click for things, like nodes in a treeview, but if your Access window is hidden you would also thereby hide your fixed position menu bars and toolbars.
Mark
 

Users who are viewing this thread

Top Bottom