In some cases you want to count the number of items in a SharePoint list and do something with that value. Let say show or hiding a control in InfoPath. This is how you can do this:
Add a new datasource to you InfoPath form that points to your list that you want to count:
Select the ID column. It really doesn’t matter what columns you select.
After you created the datasource we add a textbox to the form to display the number of items in the form:
Now modify the properties of the textbox by right clicking on it and select Text Box Properties:
We will count the number of items in the datasource we just created:
Make sure you select the “dataFields” SharePointListItem folder and not the queryFields one.
You can use the “filter data” button to filter the items before counting.
That’s all you have to do, besides clicking “OK” a few more times. Now you can preview your form and you can see the number of items in the list.