D
Deleted member 73419
Guest
I'm looking for some help with the ribbons in my Access database.
Currently there are two forms and each one has its own ContextTab which appears when the form is opened. There is also a main ribbon which is available when the database is opened.
The ribbon XML as defined in USysRibbons are:
MainRibbon
this is the main ribbon and is set in Access > Options > Current Database > Ribbon and Toolbar Options > Ribbon Name
Context1
this ContextTab appears when Form1 is opened and is set in Form1 > Ribbon Name
this ContextTab appears when Form2 is opened and is set in Form2 > Ribbon Name
For the VBA code, I have this in a module:
this should run when MainRibbon is loaded and sets MRibbonUI
Form1 VBA
Form2 VBA code is identical to Form1 other than 1 is replaced by 2!!
Now the issue is that, when I open either Form1 or Form2, I get this error:
which then breaks to
It looks like mRibbonUI variable on the form is not initialised so ActivateTab fails.
So, my question is should the application and all forms all share the same ribbon or should all ribbons and ContextTabs all be in the scope of the form which activates them? As you can see from the code, the MainRibbon uses the mRibbonUI variable in the module and there are no issues here, but the forms have their own private mRibbonUI variable and this is not being set. Setting a break point in Context1_Load, I can see this is not being called hence mRibbonUI not being set.
I have had it suggested that all forms should have their own ribbons, but in order to replace the ribbons (main ribbon and contectTab ribbon) when a form is activated seems to go against what ContextTabs are for in the first place. Everytime a form is activated and deactivated (as would happen navigating between forms) would mean the whole ribbon is replaced.
What is the correct method for handling ContextTabs in a Ribbon application. Seems the mechanisms are there but little information to go with it (most examples deal only with a single ribbon without taking this further).
Thanks
Currently there are two forms and each one has its own ContextTab which appears when the form is opened. There is also a main ribbon which is available when the database is opened.
The ribbon XML as defined in USysRibbons are:
MainRibbon
XML:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="OnRibbonLoad" loadImage="LoadImages">
<ribbon startFromScratch="false">
<tabs>
<tab id="MainTab" label="Main Tab">
<group id="MainTabGroup1" label="Main Tab Group 1">
<button id="btnDemo1" size="large" label="Demo 1"/>
</group>
<group id="MainTabGroup2" label="Main Tab Group 2">
<button id="btnDemo2" size="large" label="Demo 2"/>
</group>
<group id="MainTabGroup3" label="Main Tab Group 3">
<button id="btnDemo3" size="large" label="Demo 3"/>
</group>
</tab>
</tabs>
</ribbon>
</customUI>
Context1
XML:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" loadImage="LoadImages" onLoad="Context1_Load">
<ribbon startFromScratch="false">
<contextualTabs>
<tabSet idMso="TabSetFormReportExtensibility">
<tab id="ContextualTab1" label="Contextual Tab 1">
<group id="ContextualTab1Group1" label="Contextual Tab 1 Group 1">
</group>
<group id="ContextualTab1Group2" label="Contextual Tab 1 Group 2">
</group>
</tab>
</tabSet>
</contextualTabs>
</ribbon>
</customUI>
XML:
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" loadImage="LoadImages" onLoad="Context2_Load">
<ribbon startFromScratch="false">
<contextualTabs>
<tabSet idMso="TabSetFormReportExtensibility">
<tab id="ContextualTab2" label="Contextual Tab 2">
<group id="ContextualTab2Group1" label="Contextual Tab 2 Group 1">
</group>
<group id="ContextualTab2Group2" label="Contextual Tab 2 Group 2">
</group>
</tab>
</tabSet>
</contextualTabs>
</ribbon>
</customUI>
For the VBA code, I have this in a module:
Code:
Private mRibbonUI As IRibbonUI
Public Function onRibbonLoad(ribbonUI As IRibbonUI)
On Error GoTo errorHandler
Set mRibbonUI = ribbonUI
errorHandler:
Exit Function
End Function
Form1 VBA
Code:
Private mRibbonUI As IRibbonUI
Private Sub Form_Activate()
mRibbonUI.ActivateTab "ContextualTab1"
End Sub
Private Function Context1_Load(ribbonUI As IRibbonUI)
On Error GoTo errorHandler
If (mRibbonUI Is Nothing) Then
Set mRibbonUI = ribbonUI
End If
errorHandler:
Exit Function
End Function
Form2 VBA code is identical to Form1 other than 1 is replaced by 2!!
Now the issue is that, when I open either Form1 or Form2, I get this error:
which then breaks to
Code:
mRibbonUI.ActivateTab "ContextualTab1"
It looks like mRibbonUI variable on the form is not initialised so ActivateTab fails.
So, my question is should the application and all forms all share the same ribbon or should all ribbons and ContextTabs all be in the scope of the form which activates them? As you can see from the code, the MainRibbon uses the mRibbonUI variable in the module and there are no issues here, but the forms have their own private mRibbonUI variable and this is not being set. Setting a break point in Context1_Load, I can see this is not being called hence mRibbonUI not being set.
I have had it suggested that all forms should have their own ribbons, but in order to replace the ribbons (main ribbon and contectTab ribbon) when a form is activated seems to go against what ContextTabs are for in the first place. Everytime a form is activated and deactivated (as would happen navigating between forms) would mean the whole ribbon is replaced.
What is the correct method for handling ContextTabs in a Ribbon application. Seems the mechanisms are there but little information to go with it (most examples deal only with a single ribbon without taking this further).
Thanks