Ribbon Customization: Filling Dropdown Lists

Published 25 September 07 05:35 PM

In the previous post, I showed you how to create a simple customization to close the currently open object. Continuing along the lines of adding object helpers to an application, we'll add two dropdown lists to a customization to work with forms. The first will list all forms in a database and the second will list the open forms in an application. Start with the XML for the customization. For readability, I've left out the button created in the previous post.

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon startFromScratch="false">
        <tabs>
            <tab id="tab1" label="Object Helpers">
                <group id="grp1" label="Helpers">
                    <dropDown id="ddlAllForms"
                              label="All Forms"
                              imageMso="CreateFormMoreFormsGallery"
                              sizeString="AAAAAAAAAAAAAAA"
                              getItemCount="OnGetItemCount"
                              getItemLabel="OnGetItemLabel"
                              onAction="OnSelectItem">
                    </dropDown>
                    <dropDown id="ddlOpenForms"
                              label="Open Forms"
                              imageMso="CreateFormMoreFormsGallery"
                              sizeString="AAAAAAAAAAAAAAA"
                              getItemCount="OnGetItemCount"
                              getItemLabel="OnGetItemLabel"
                              onAction="OnSelectItem">
                    </dropDown>
                </group>
            </tab>
        </tabs>
    </ribbon>
</customUI>

Add this customization as an entry in a USysRibbons table in the database and set the Ribbon Name property of the database.

The customization shown here defines three callback routines used by the dropdown: getItemCount, getItemLabel, and onAction. These callbacks are used to determine the number of items in a dropdown, the label for a single item, and when an item is selected respectively. We're going to write one callback to cover both controls, starting with OnGetItemCount. We'll tell the dropdown to either display the number of forms in the database using the AllForms collection, or the number of open forms using the Forms collection.

Public Sub OnGetItemCount(ctl As IRibbonControl, ByRef Count)
    ' set the number of items to the number of forms in the database
    If (ctl.ID = "ddlAllForms") Then
        Count = CurrentProject.AllForms.Count  ' Total number of forms
    ElseIf (ctl.ID = "ddlOpenForms") Then
        Count = Forms.Count                    ' Number of open forms
    End If
End Sub

Next, create the OnGetItemLabel callback:

Public Sub OnGetItemLabel(ctl As IRibbonControl, Index As Integer, ByRef Label)
    ' set the label
   
If (ctl.ID = "ddlAllForms") Then
        Label = CurrentProject.AllForms(Index).Name
    ElseIf (ctl.ID = "ddlOpenForms") Then
        ' for open forms, use the Caption property of the form if set
       
If (Len(Forms(Index).Caption) > 0) Then
            Label = Forms(Index).Caption
        Else
            Label = Forms(Index).Name
        End If
    End If
End Sub

Now, add the OnSelectItem callback that is called when you choose an item in the dropdown. Here, we'll open the currently selected form.

Public Sub OnSelectItem(ctl As IRibbonControl, selectedId As String, selectedIndex As Integer)
    If (ctl.ID = "ddlAllForms") Then
        DoCmd.OpenForm CurrentProject.AllForms(selectedIndex).Name
    ElseIf (ctl.ID = "ddlOpenForms") Then
        DoCmd.OpenForm Forms(selectedIndex).Name
    End If
End Sub

Re-open the database to try it out and open some forms. When you go to the Object Helpers tab, you should have something that looks like this:

Dropdown lists in ribbon customizations

Comments

# Techy News Blog » Ribbon Customization: Filling Dropdown Lists said on September 25, 2007 8:43 PM:

PingBack from http://www.artofbam.com/wordpress/?p=2933

# Chris Sergent said on September 26, 2007 10:11 AM:

This worked well. It also merged with my form ribbon giving me a better idea on how to use the ribbon for application wide tools and how to use it for specific objects such as forms.

Is there a way to reference an XML file in the USysRibbons table?

The reason I ask is so I could use templates, so I don't have to re-type code and possibly share parts of the XML document with other people or applications and have it automatically update in my database.

# robcooper said on September 26, 2007 8:29 PM:

Chris,

There is no way to reference a file using the USysRibbons table, however you can load the XML for a customization using the LoadCustomUI method in Access. The second argument for this method is the XML string for a customization which can be loaded from a file.

This method does not automatically show a customization so you may still need to refer to the name of a customization using the Ribbon Name property of the database or a given form or report.

- Rob

# Alan Cossey said on September 27, 2007 8:42 AM:

Sorry this is off topic, but I can't find an item which is relevant and is still open.

Back at the end of August Zac posted thus:

# Zac Woodall said on August 29, 2007 12:41 PM:

Rex: see my previous comment in this post.  We're aware of the PDF issue.  It looks like there is a bug that our testing failed to catch and we're actively investigating it.  Sorry for ths inconvenience this is causing!

That was 4 weeks ago. Any news please? I have a customer (still) waiting to use it.

Thanks,

Alan Cossey

# Alan Cossey said on September 27, 2007 2:11 PM:

Re my post of a comment on September 27, 2007 8:42 AM:

I really ought to have mentioned that it referred to the Access 2007 runtime!

Alan

New Comments to this post are disabled

About robcooper

Rob Cooper is a Test Lead at Microsoft working on Microsoft Access. He started at Microsoft in 1998 working in Access product support in Charlotte, NC and then moved to Redmond to join the test team in 2001. For Access 2007, Rob worked on the new Grouping Pane for Reports, and security features such as Database Encryption and Disabled Mode. He is also a co-author on Expert Access 2007 Programming and Access 2007 VBA Programmer's Reference, both published by Wrox. Rob also spends time on www.utteraccess.com reading and answering questions.
Page view tracker