0

I'm developing an app for a client in MS Access using VBA. I'm new to the Ribbon but I do understand how to enable/disable buttons based on when a form is opened. My challenge now is enabling a button (Open Document) when node is clicked in a list view on another form.

To further explain: A master form contains a list of documents in a ListViewCtl control with the Doc ID as a tag. When the user clicks a node a global variable (gbCurDoc) is set to the tag. That's when I'd like to enable the button in the ribbon that, when clicked, opens the file (gbCurDoc > 0). But I want that button disabled if no document is selected (gbCurDoc = 0).

My VBA:

Public Sub ControlEnabled(control As IRibbonControl, ByRef enabled)

Select Case control.Tag
    Case "DocOpen"
        If gbCurDoc = 0 Then
            enabled = False
        Else
            enabled = True
        End If
        myRibbon.InvalidateControl ("DocOpen")
End Select

End Sub

The XML looks like this:

<button id="DocOpen" getImage="GetImageCallBack" size="large" tag="DocOpen" onAction="ribOpenForm" getEnabled="ControlEnabled" supertip="Open Document" />
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
Kwixster
  • 51
  • 8
  • Looks like you have the ribbon callback set up. How are you calling it? What is the issue - error message, wrong result, nothing happens? Are you familiar with this site https://www.accessribbon.de/en/?Access_-_Ribbons:Callbacks:getVisible – June7 Nov 16 '22 at 21:26
  • When a node is clicked it fires an event that sets the gbCurDoc to the document id in the tag field (this works fine), This line runs myRibbon.InvalidateControl ("DocOpen") I thought that would trigger re-evaluating the button where the "ControlEnabled" sub would run. But it doesn't run. – Kwixster Nov 16 '22 at 23:12

1 Answers1

1

You need to use callbacks in the ribbon XML markup. Specifically, to enabled and disable controls you need to specify the getEnabled callback in the ribbon XML. It seems you have already done that part.

The other part of the journey is to use the Invalidate or InvalidateControl methods of the IRibbonUI interface.

For example, if an add-in writer implements the getEnabled callback procedure for a button, the function is called once, the state loads, and then if the state needs to be updated, the cached state is used instead of recalling the procedure. This process remains in place until the add-in signals that the cached values are invalid by using the Invalidate method, at which time, the callback procedure is again called and the return response is cached. The add-in can then force an immediate update of the UI by calling the Refresh method.

Dim MyRibbon As IRibbonUI 
 
Sub MyAddInInitialize(Ribbon As IRibbonUI) 
 Set MyRibbon = Ribbon 
End Sub 
 
Sub myFunction() 
 MyRibbon.Invalidate() ' Invalidates the caches of all of this add-in's controls 
End Sub

In the ribbon XML don't forget to specify the onLoad callback:

<customUI … OnLoad="MyAddinInitialize" …>
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45