Color scheme: Light
You are here: start » en » Documentation » Microsoft Access development tools » WKL Forms Object Library » WKL Forms Object Library how-to guides » How to: Control the ribbon from code

How to: Control the ribbon from code

Dim, hide, rename, or press ribbon commands from VBA, and respond before a command runs.

Requires WKL Forms Object Library 1.49 or later.

Solution overview

The generated module basWklRibbon contains the function WklRibbonObject(), which returns the database's ribbon runtime. Setting Enabled, Visible, Label, or Pressed for a command takes effect immediately on the ribbon, in the File view, and in the context menus; ResetCommand returns the command to the state in the model. The Action event fires before a command's action runs and can cancel it.

Implementation

' In the module of the form frmOrders
Private WithEvents mRibbon As WklRibbon
 
Private Sub Form_Load()
    Set mRibbon = WklRibbonObject()
End Sub
 
Private Sub Form_Current()
    ' Only a released order can be printed.
    mRibbon.Enabled("cmdPrint") = (Nz(Me!Status, "") = "Released")
End Sub
 
Private Sub Form_Close()
    mRibbon.ResetCommand "cmdPrint"
End Sub
 
Private Sub mRibbon_Action(ByVal Command As String, ByVal Pressed As Boolean, Cancel As Integer)
    If Command = "cmdPrint" And Me.Dirty Then Me.Dirty = False
End Sub
 
Private Sub mRibbon_ActionError(ByVal Command As String, ByVal Description As String)
    MsgBox "The command couldn't be run: " & Description, vbExclamation
End Sub

Remarks

  • Setting Visible to False hides a command in addition to its condition; it can't show a command that its condition hides.
  • The values belong to the database in the current Access session, not to the form: reset them when the form closes, as shown above, unless they should stay in effect.
  • A WithEvents variable is possible only at the module level of a form, report, or class, and the events go to that object. WklRibbonObject() always returns the module's object, so every listener receives the ribbon's events.
  • Execute "cmdPrint" runs a command from code as if the user had chosen it.
en/wiki/msaccess/comctl/how-to/control-the-ribbon-from-code.txt · Last modified: by Wolfgang van der Stille