Question

In an MS Access form, I have a working popup menu but I wonder about ways to set it up differently.

Popup menu definition:

With CommandBars.Add("TreeNodeActions", msoBarPopup, False)
    With .Controls.Add(msoControlButton)
        .caption = "Copy"
        .OnAction = "=CopyNode()"
    End With
End With

Question 1: why are the equal sign and empty parameter list necessary (things don't work without them)? Why not just .OnAction = "copyNode", as suggested in other threads on this forum?

Question 2: I wish to run a subroutine, but the only way to do that is to call a function that calls the subroutine - see below. How can I call the subroutine directly?

Private Function CopyNode()
    Call CopyBid
End Function

Private Sub CopyBid()
    Dim nodx As node

    Set nodx = Me.TreeView1.Nodes(Me.nodeKeySelected.Value)
    Me.BidIdToBeCopied = nodx.Tag
End Sub

Question 3: the popup menu is on a treeview control; I wish to pass the node selected to the subroutine, so that I don't have to store the node key on the form (on a treeview mouse click) and use it in the sub above. I would like to be able to have the below. How can menu items defined in the commandbars control pass parameters?

Private Sub CopyBid(nodx As node)
    Me.BidIdToBeCopied = nodx.Tag
End Sub
Was it helpful?

Solution

Did some further researching myself. Not conclusive, but I got as far as this:
Q1: no documentation found, it's just a fact that the syntax in ms-access vba onAction property is like that. Still feels a little odd that no explanation can be found for this - I would have expected something like "the equal sign makes sure that the ensuing function or procedure is called. The parentheses at the end are redundant but have to be added for a parameterless functions"
Q2: it seems this cannot be done - so reuse of existing sub is not possible, unless by changing this to a function. Strange but probably a fact of life again. No documentation found on this one either.
Q3: passing parameters is less easy than it seems - the mandatory parentheses suggest that it is just a matter of supplying variables in the onAction spec for functions that have parameters. It's not that simple, probably because the onAction spec is a string that triggers a search for a known function name without taking into account parameters. One way of supplying parameters that I was able to find (finally some useful information!) may be found in calling a function and passing arguments with .onaction. Feels like a dirty work around to me, so I still wonder whether neater solutions are available. And I haven't tested this "solution" yet...

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