fbpx

Company News

How to Disable a Dropdown in InfoPath when Empty

March 23, 2016

At one point a client asked me to disable a dropdown field in an InfoPath form when that field was empty. Unfortunately this isn’t as easy as it sounds. Here’s my rather roundabout way of solving the problem, which works, but isn’t pretty.

 
 

You need to start by creating a second (companion) field, which is a simple text box. This text box will be a hidden field that will be directly tied to the dropdown field from here on out. Let’s call this field “_hiddenMyDrop”, and the field it will be associated to will be “MyDrop” (a drop down).

 
 


 
 

You will also need a secondary data source (like a SharePoint list, that is the source of the data for the drop down – sorry you can’t do this without using a secondary data source). For this article, I’ve created a list in SharePoint called Temp, which has no items in it to start. I’ve also added it to my form as a data source.

 
 

Once you have that setup, you need to set the default value for the new field to be “count(Title)” (of the Title column in the secondary data source). I’ll walk you through this below.

 
 

First, start by right clicking on the hidden field in the form and selecting “Text Box Properties”.

 
 

Next, go into the function button to the right of the value field under default value.

 
 

 
 

From the Field category choose “count”.

 
 

 
 

Double click on the section to fill in the field in the formula.

 
 

 
 

Choose your secondary data source (such as your SharePoint list) from the drop down.

 
 

 
 

Then choose any column with a value, such as ID or Title (so long as the column has a value, that’s what matters, as that’s what we’ll be counting – the total number of values in the list).

 
 

 
 

Now you’ve completed your formula, click OK.

 
 

 
 

Now you’ve set the default value correctly, click OK.

 
 

 
 

The value of your field will now be equal to the number of entries in the “MyDrop” field. This should update automatically as the contents of MyDrop changes.

 
 

Next up, you need to set a condition which will disable the MyDrop field when the value is 0. In the Fields panel on the right of the screen, choose the MyDrop field and add a rule. The rule can be seen below.

 
 

 
 

Now, as a temporary measure, you should add the _hiddenMyDrop field to your form, just to see the effects, but I wouldn’t leave it there (it doesn’t need to be literally on the form view in order for the dropdown changes to take effect). I’ve previously added this field for demonstration purposes.

 
 

If we take a look at our list in SharePoint, we’ll note it currently has no items in it.

 
 


 
 

Start by previewing the form with no items.

 
 

You can see below, that in the case where there are no items (the count of MyDrop = 0) the field is disabled.

 
 

 
 

Now add a few items to your SharePoint list.

 
 

 
 

And when there are some items (or at least one item), and you preview again, the field becomes active.

 
 

 
 

And viewing the same form with the drop down, presents you the same results as in the SharePoint list.

 
 

 
 

Hope this help you out there with making your InfoPath forms more beautiful.

Leave a Reply

Colin Phillips

itgroove Alumni

Colin Phillips

Subscribe via Email

Categories

Powered by...

itgroove Corporate Profile
Connect With Us