Question

I'm trying to create my own toolbar button (commandbarbutton) in Access VBA Editor. Its a button to run a procedure in a public module called "RunTests". I made the button like so:

Public Sub CreateToolbar()
    Dim cmdBar As CommandBar
    Dim cmdButton As CommandBarButton
    Set cmdBar = Application.VBE.CommandBars.Add("Run Tests")
    Set cmdButton = cmdBar.Controls.Add(msoControlButton)
    cmdButton.FaceId = 558
    cmdButton.DescriptionText = "Run the tests"
    cmdButton.Caption = "Run Tests"
    cmdButton.OnAction = "RunTests"
    cmdButton.Enabled = True
    cmdButton.Style = msoButtonIconAndCaption
    cmdBar.Visible = True
End Sub

I ran the procedure as a test. And it created the button just fine. But the "click" action doesn't respond. The button doesn't seem to be clickable. Does anybody know why?

Edit: Found the solution: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q280607 - I will answer my own question as soon as I can to close it.

Was it helpful?

Solution 2

Found the solution - To make it work you have to implement the workaround described here: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q280607

OTHER TIPS

The key for me was to make "RunTests" a public subroutine. When it was private it didn't work.

cmdButton.OnAction = "=RunTests()"

public sub RunTests()

private sub RunTests() does not work.

Unfortunately I tried to be fancy and define my button and menus in a class. The OnAction subroutine was never executed while it was defined locally as part of the class. It would only execute when defined outside of the class definition.

When I redefined my class to be just a module containing many subs and functions, then the OnAction subroutine was executed. Might have to do with scope and visibility or that I didn't have the OnAction semantics correct for a call to a class method. Bottom line is to define the button/menu/widget in a module and it will be much easier. (I was using Excel VBA but I think Access and Excel VBA are the same.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top