I really dislike this type of numbering for a variety of reasons.
However, if you must do this, you should do it as part of the submit rules, so as to minimize the likelihood of two forms getting the same number. You'll have to query the form library to get back all the existing numbers in use, then use some substring functions to parse things out.
If I had to do this, I would probably:
- Promote a field to the library that contains the first three letters of the Site field to make it easier to query for just the forms I need
- Add a data connection to query the library, including just the Reference number and field from step 1, sorted by Reference number desc - don't run the query on form load
- In my submit rules, set the query field in the data connection from step 2 to the three letters of the Site field in this form, and query the data connection - this way I get back JUST the Reference numbers that apply to this site
- Since I sorted by Reference number descending, the first returned row will have the number I need to increment. I know the first three letters of the site, so can use substring-after to get just the number portion
- Use the number function to get rid of preceding zeros & then add one
- Use a concat formula with substring to pad with zeros (search this site or the web in general for zero padding in InfoPath)
Make sense?