I have a custom Ribbon in Access 2010 and I want one tab (the admin tab) to show only for a particular user. I already have the function written for checking the user and have the tab hidden in the XML but I am not finding a way to target the hidden tab to make it visible.
I'm not wanting to hide the entire ribbon, I'm looking to show a tab that is loaded with the ribbon but has its visibility hidden. I'm not seeing any information in the post or the link provided by user Ari referring to targeting tabs.
I'm looking to show a tab that is loaded with the ribbon but has its visibility hidden. I'm not seeing any information in the post or the link provided by user Ari referring to targeting tabs.
I'm not looking to give a tab focus, but change it's visibility attribute from "false" to "true."
When the app loads, the ribbon is loaded from USysRibbons with the XML I created for my custom ribbon. There are two tabs, with the second starting like this:
The tab doesn't show when the custom ribbon is loaded in. What I am wanting to do is to write a function that makes this tab visible when a particular user is using the app.
Then you need to invalidate the ribbon, so you'll need to maintain a reference to the ribbon when it loads, also using a callback. Do you know how to do that?
The ribbon is an IRibbonUI object and it has two methods you can call, Invalidate(), and InvalidateControl(ControlID as string). Invalidating a ribbon or a control in a ribbon forces it to redraw itself so if you've defined any callbacks like your getVisible, those'll run, return values, and the ribbon will redraw accordingly.
It's common to use one callback for lots of ribbon controls, so the RibbonGetVisible() callback, mentioned above, should be called by any and all controls that need visibility info, so its common to use a Select Case statement, like this . . .
Code:
Sub RibbonGetVisible(irc as Office.IRibbonControl, visible)
Select Case irc.id
Case "tabMyCustomer"
visible = [I]< boolean expression >[/I]
Case "tabSomeOtherTab"
visible = [I]< some other boolean expression >[/I]
Case Else
visible = true
MsgBox "Unknown irc.id " & irc.id & " in RibbonGetVisible()"
End Select
End Sub
Sometimes, for smaller ribbons, I'll use a callback like . . .
Code:
Sub RibbonGetState(irc as ribboncontrol, state)
. . . so I can use that in a ribbon for getVisible="RibbonGetState", but also getEnabled="RibbonGetState", or getShowLabel="RibbonGetState". A problem arises in more complex ribbons if the same control needs to getEnabled and getVisible and getShowLabel, so that requires a little planning.
I followed this example but still received the same error message in my app:
Code:
Microsoft Access cannot run the macro or callback function 'adminTabGetVisible'. Make sure the macro or function exists and takes the correct parameters.
So, I took my Sub and put it in the file Ari sent, changed the ribbon by setting the getVisible attribute to "adminTabGetVisible" and it worked without any issues.
Here is my Sub in a standard module:
Code:
Public Sub adminTabGetVisible(control As IRibbonControl, ByRef returnVal)
Select Case control.Id
Case "tabAdmin"
If Environ("Username") = "DoeJohn" Or Environ("Username") = "DoeJane" Then
returnVal = True
Else
returnVal = False
End If
End Select
End Sub
Here is the XML for the ribbon:
Code:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"
loadImage="fncLoadImage">
<ribbon startFromScratch="true">
<!-- ***TAG QAT***-->
<tabs>
<!-- *** TAB MAIN ***-->
<tab id = "guiaMain" label = "Main" >
<!-- *** GROUP SEVERAL ***-->
<group id = "grSeveral" label = "Several"></group>
</tab>
<!-- *** END OF THE TAB MAIN***-->
<tab id = "tabAdmin" label = "Admin" getVisible="adminTabGetVisible">
<group id="grAdmin" label="Admin"></group>
</tab>
</tabs>
</ribbon>
</customUI>
Is there a setting in the database that would prevent this code from working in my app that allows it to work in the example given by Ari?
Attached what I am workign with. Started a empty database, added the USysRibbons table and added my ribbon XML in there. Also added my Sub in the only module (modAdminRibbon). I am still getting the error message from above (MS Access can't access the macro or function).
There are errors in your xml. To get Access to tell you what they are you need to "show add-in user interface errors."
Click -->Office Button-->Access Options-->Advanced-->General->Show Add-in User Interface Errors Checkbox. Set this check box and re-open your application.
Actually, your ribbon xml is might be OK if you are using Access 2010.
But what fails for sure is your use of IRibbonUI and IRibbonControl objects. Find the Compile button in your VBA project and click it. You need a reference to the Office Object Library. In a code window goto Menu-->Tools-->References and set a reference to "Microsoft Office 14.0 Object Library".
Public Sub RibbonGetVisible(control As IRibbonControl, ByRef visible)
If TempVars!currentuserID = "72677" Then
visible = True
Else
visible = False
End If
End Sub
The TempVars!currentuserID value is set by a form after AutoExec marco executes. tab2 is not still visible even after setting TempVars!currentuserID to "72677".