How to Truncate Text Using Custom VB Code in Bold Reports
Bold Reports offers a feature that allows you to truncate excessively long text within tablix cells using custom VB code. This approach limits the number of characters displayed and automatically appends an ellipsis (…) when the text exceeds the defined limit. It helps maintain a clean, readable layout, especially useful for fields like user feedback or detailed descriptions with variable lengths.
Steps to Implement Text Truncation
Adding Custom VB Code
- Access the Properties panel
- Open the Properties panel in the Report Designer and click on the Settings Icon to open the report properties
- Open the Properties panel in the Report Designer and click on the Settings Icon to open the report properties
- Navigate to the Code section
- Expand the Code section and click on Code to open the Code Editor.
- Expand the Code section and click on Code to open the Code Editor.
- Set the Custom VB Code
- Use the following code in the Code Module and click OK to save.
Function TruncateString(ByVal input As String, ByVal length As Integer) As String
If input.Length > length Then
Return Left(input, length) & "..."
Else
Return input
End If
End Function
Code | Description |
---|---|
Declaration: Function TruncateString(ByVal input As String, ByVal length As Integer) As String |
Declares a function that takes a string and a length, and returns a truncated string.3 |
Condition: If input.Length > length |
Checks if the input string exceeds the specified length. |
Truncate: Return Left(input, length) & "..." |
Returns the first length characters followed by an ellipsis if the string is too long. |
Adding the Expression
- Setting the Expression in the Tablix Cell:
- Open the Expression Editor from the desired tablix cell. Enter the following expression and click OK to apply
=Code.TruncateString(Fields!YourFieldName.Value, 10)
- Open the Expression Editor from the desired tablix cell. Enter the following expression and click OK to apply
The expression =Code.TruncateString(Fields!YourFieldName.Value, 10)
calls the TruncateString function to limit the YourFieldName field to 10 characters, appending “…” if longer, or returning the full string if shorter.
- Preview
- Before Configuration: The full text is displayed, potentially causing layout issues.
- After Configuration: The text is truncated to 10 characters with an ellipsis for longer strings.
- Before Configuration: The full text is displayed, potentially causing layout issues.
Conclusion
Using custom VB code in Bold Reports to truncate text ensures that your report layout remains tidy and professional. This method is especially effective for handling fields with unpredictable or lengthy content, improving both usability and visual appeal.