Help needed Programming command bars

Posted on 2004-11-29
Last Modified: 2012-05-05
I really need help  with this so I'm reasking the question and giving it 500 pts:

I'd like to programatically add a commandbar button (msoControlButton) to a 'menu' (msoControlPopup) and I'd like it to behave the same was as if I'd have manually chosen to:
1. customize the menu bars, created/selected my custom toolbar,
2. Dragged on to that toolbar a 'NewMenu'
3. On to that new menu, dragged a 'form', from the AllForms section

AFAICT, this means that my commandbutton has to have a builtin ID of 1837 and a parameter which is the  name of the desired form.

so here is what I imagine the code should resemble:
 Dim z As CommandBarButton
 Set z = Application.CommandBars("MyMenu").Controls(1).controls.Add(msoControlButton, 1837, "Form1")

When I try this I get this error:
  -2147467259(800004005) 'Method "Add" of object commandbarcontrols failed' -
Question by:valhallatech
    LVL 58

    Accepted Solution

    "AFAICT", you have all the right assumptions... But, I never got this to work.
    But I have two workarounds:

    1) the one I ended up using is this:

        With Application.CommandBars("MyMenu").Controls(1)
            With .Controls.Add
                .Caption = "Form1"
                .FaceID = 1837
                .OnAction = "= DoCmd.OpenForm(""Form1"")"
                .TooltipText = "Open Form1"
            End With
        End With

    2) if you really need a BuiltIn Id 1837 command button:
    Create a "template" toolbar (hidden, etc...) with one of them and go:

        CommandBars("Template").Controls(1).Copy _

    And then customize the new button. I suspect there is an other "CreateCommandButton" method which is not available to us, used in the "Customize" screen.

    Good Luck
    LVL 2

    Author Comment

    Thanks harfang - sorry its been almost 2 mths, I haven't been around -

    I was afraid this was the news - this kind of  'not available to us' type of 'behaviour' is one of the maddening things about access and proprietery software in general.

    I'm trying to build a utility to integrate my access work into the same production processes as my other work, and that involves using subversion and/or cvs as source control - except for this little quirk, I think i'd pretty much be there.

    I'll have a play with these workarounds - as they at least let me get on.

    thanks again for the 'bad news' - I can stop holding out for the perfect solution
    LVL 58

    Expert Comment

    Yeah, we loose much time with these problems...
    The "custom button" solution works fine, though, and is transparent to the user.

    Good luck with your project!

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    This article is a continuation or rather an extension from Cascading Combos ( and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
    I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now