So, say I have the following string
THIS IS STUFF (MORE STUFF)
How would I get the string to format as such in a textbox in SSRS?
THIS IS STUFF
(MORE STUFF)
This data is pulled from a single field in a query and I will not be able to manually inject a break line.
Also, sometimes the (More Stuff) is not in the field.
Extra examples:
When you need to include a line break within a textbox in SQL Server Reporting Services, a predefined Visual Basic constant may be used. The vbcrlf, Visual Basic Carriage Return Line Feed, handles this quite nicely within an SSRS expression.
Using Newline functionMake use of Environment. NewLine() function to add line break within the expression.
If you want to fix it, you can add a rectangle control and then fix your boxes with required space inside it. About the extra blank paper at the end, it happens when your controls exceed the dimensions of the report. Search for it, you'll find many helpful articles.
In Design view, click the text box on the design surface to which you want to add an expression. For a simple expression, type the display text for the expression in the text box. For example, for the dataset field Sales, type [Sales] . For a complex expression, right-click the text box, and select Expression.
You need to insert a line break into the string, i.e. set the textbox expression as something like:
="THIS IS STUFF" & vbcrlf & "(MORE STUFF)"
So the expression in a single textbox:
Will render as:
You can see there is a line break even though the string could fit on one line.
Edit after comment
OK, we need to handle various existing strings at the report level.
To do this, I would suggest using Custom Code, i.e. a function like:
Function SplitString (fieldValue As String) As String
If IsDBNull(fieldValue) OrElse IsNothing(fieldValue) Then
SplitString = ""
Else If InStr(fieldValue, "(") > 0
SplitString = RTrim(Left(fieldValue, InStr(fieldValue, "(") - 1)) & _
vbcrlf & Right(fieldValue, Len(fieldValue) - InStr(fieldValue, "(") + 1)
Else
SplitString = fieldValue
End If
End Function
I'm suggesting this as SSRS expression are notoriously brittle, and the lack of short circuiting means that what works for one string might display #Error
on another. You could get around this with numerous IIf
statements but Custom Code is much neater and easier to understand.
You can call the function with an expression like:
=Code.SplitString(Fields!MYFIELD.Value)
Either in a textbox expression or as a Calculated Field in the Dataset. The above works for me on your data:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With