Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question: I have a question. I am in the process of building an InfoPath form and I can’t seem to figure out how to display a value formatted as currency. I know that I can write a script to do that. I have found some examples of formatting strings. It’s just that this seems as overkill. Is there any way that I can do this without writing code? Don’t get me wrong I love writing code but this seems like there has to be an easier way.
Answer: Absolutely, there is an easier way. Like you I love code – but sometimes I just have to give in and use the built in approach. Here are some steps that should help to do this.
1. Starting with the following data source.
2. We can then create a form that displays the data source.
3. Within this form we want to show the Item Cost field as currency. If you review the data source view shown above this field (ItemCost) is defined as an integer. Follow these steps to change the display the contents of this field to currency.
- Select the Item Cost textbox within the layout view.
- Right click and select the text box properties
- Within the properties screen select the format button
- Within Format select the type of format that you want.
5. Once you save everything and preview the form you can see that the field is displayed as currency.
One thing to remember is that even though data is displayed as currency, it is actually stored as an integer. As an example, if you were to fill out and then save a form based on this template. The XML created would resemble the following.
<my:SaleItems>
<my:ItemName>test</my:ItemName>
<my:ItemCost xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">12</my:ItemCost>
<my:ItemQuantity xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">1</my:ItemQuantity>
<my:ItemExtended></my:ItemExtended>
</my:SaleItems>
<my:TotalSale>
<my:TotalSalePrice></my:TotalSalePrice>
</my:TotalSale>
The ability to separate data storage and presentation is an important feature of InfoPath. Formatting is actually defined as stored within the solution file (XSN) as an XSLT transformation. The InfoPath solution file is actually just a compressed CAB file that contains a set of XML files. In this example, the view was created with the default name “view1”. Using a program like WinZip, you can view the contents of the file.
Once open you can expand the XSN and open the “view1.xsl” using notepad. Stored within this transformation file you can find the following line that does the actual transformation.
<td><span class="xdTextBox xdBehavior_Formatting" hideFocus="1" title="" contentEditable="true" tabIndex="0" xd:xctname="PlainText" xd:CtrlId="CTRL12" xd:binding="my:ItemCost" xd:boundProp="xd:num" xd:datafmt=""currency","numDigits:0;negativeOrder:0;positiveOrder:0;currencyLocale:1033;"" style="WIDTH: 100%">
<xsl:attribute name="xd:num">
<xsl:value-of select="my:ItemCost"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="function-available('xdFormatting:formatString')">
<xsl:value-of select="xdFormatting:formatString(my:ItemCost,"currency","numDigits:0;negativeOrder:0;positiveOrder:0;currencyLocale:1033;")"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="my:ItemCost"/>
</xsl:otherwise>
</xsl:choose>
</span>
</td>