Hide/Show Ribbon Tab using VBA (1 Viewer)

heslopg

Registered User.
Local time
Today, 08:24
Joined
Feb 5, 2013
Messages
13
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.

Any suggestions?
 

heslopg

Registered User.
Local time
Today, 08:24
Joined
Feb 5, 2013
Messages
13
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.
 

mdlueck

Sr. Application Developer
Local time
Today, 08:24
Joined
Jun 23, 2011
Messages
2,631
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.

?????

Do you want to set a specific tab to have visual focus?
 

heslopg

Registered User.
Local time
Today, 08:24
Joined
Feb 5, 2013
Messages
13
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:

Code:
<tab id="tabMyTabAdmin" label="Admin" visible="false">

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.
 

mdlueck

Sr. Application Developer
Local time
Today, 08:24
Joined
Jun 23, 2011
Messages
2,631
When the app loads, the ribbon is loaded from USysRibbons with the XML I created for my custom ribbon

Oh, this is the first I realized you have created a custom ribbon. I have never done that. Perhaps someone else will be able to assist you.
 

MarkK

bit cruncher
Local time
Today, 05:24
Joined
Mar 17, 2004
Messages
8,178
Ribbons Use Callbacks

One of the possible members of a ribbon tab xml is getVisible. You need to set getVisible to the name of a sub having this signature . . .
Code:
Sub RibbonGetVisible(irc as iribboncontrol, visible)
. . . so in your ribbon tab xml, you'll have something like . . .
Code:
<tab id="tabMyCustom" label="My Tab" getVisible="RibbonGetVisible" >
    . . . 
</tab>
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.

hth
 

Ari

Registered User.
Local time
Today, 05:24
Joined
Oct 22, 2011
Messages
139
Follows a small example, using the attribute getVisible to activate the Admin tab
 

Attachments

  • RibbonGetVisible.zip
    29.5 KB · Views: 2,557

heslopg

Registered User.
Local time
Today, 08:24
Joined
Feb 5, 2013
Messages
13
Follows a small example, using the attribute getVisible to activate the Admin tab

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?
 

MarkK

bit cruncher
Local time
Today, 05:24
Joined
Mar 17, 2004
Messages
8,178
I don't see a problem; don't see why that wouldn't work. You can post your db if you want. Someone might be able to find something.
Cheers,
 

heslopg

Registered User.
Local time
Today, 08:24
Joined
Feb 5, 2013
Messages
13
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).
 

Attachments

  • LAPS2010-GetVisibleTest.zip
    27.6 KB · Views: 962

MarkK

bit cruncher
Local time
Today, 05:24
Joined
Mar 17, 2004
Messages
8,178
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.

hth
 

MarkK

bit cruncher
Local time
Today, 05:24
Joined
Mar 17, 2004
Messages
8,178
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".

Cheers,
 

a.sharma

Registered User.
Local time
Today, 17:54
Joined
Apr 22, 2013
Messages
20
Try the attached db now.

and do chk the code.. some change done and added ref to ms office 14 object lib

should work now.
 

Attachments

  • LAPS2010-GetVisibleTest.zip
    31.1 KB · Views: 1,429

a.sharma

Registered User.
Local time
Today, 17:54
Joined
Apr 22, 2013
Messages
20
UPDATED Db.
tested working fine on A2010
 

Attachments

  • LAPS2010-GetVisibleTest.zip
    22.8 KB · Views: 601

heslopg

Registered User.
Local time
Today, 08:24
Joined
Feb 5, 2013
Messages
13
Setting a reference to Microsoft Office 14.0 Object Library did the trick! Thank you all so much for your assistance on this. Thank you's all around!
 

zuhairaltaf

New member
Local time
Today, 17:24
Joined
May 28, 2020
Messages
1
I am facing the same problem

<tab id="tab2" label="Admin" getVisible="RibbonGetVisible" >
</tab>

with vba code

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".
 

Gasman

Enthusiastic Amateur
Local time
Today, 12:24
Joined
Sep 21, 2011
Messages
14,051
Shouldn't that be control.visible ? :unsure:
 

Users who are viewing this thread

Top Bottom