Insert a text on your crystal report from code
This FAQ demonstrates how you can set a textvalue in your code and show this text in your report.
The attached program shows you how to do this.
Basically we have three different scenarios:
1)Via a parameter: this is the standard communication between VB.NET and a report.
This is not my recommended way to set a text on a report. It's quite some code, and it's prone to errors. I had some strange experiences with it in the past (like a maximum text length of 254 bytes). Consult the updates and monthly fixes on the support site: http://www.businessobjects.com/services/support/default.asp
Code:Dim paramFields As ParameterFieldDefinitions
Dim paramField As ParameterFieldDefinition
Dim paramValue As ParameterValues
Dim paramDiscreteValue As New ParameterDiscreteValue
paramFields = rpt.DataDefinition.ParameterFields
SetParameter(paramFields, "PersonalCommentsParameter", Me.txtParameter.Text)
Public Sub SetParameter(ByVal paramDef As ParameterFieldDefinitions, ByVal paramName As String, ByVal paramValue As String)
Dim crParameterFieldDefinition As ParameterFieldDefinition = paramDef.Item(paramName)
Dim crParameterValues As ParameterValues = crParameterFieldDefinition.CurrentValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = paramValue
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
End Sub
2)Via a text object: This is my recommended way to go: create a text object in your report, give it a clear name and drag it on your form.
Code:Dim SampleReportText As CrystalDecisions.CrystalReports.Engine.TextObject = CType(rpt.ReportDefinition.ReportObjects.Item("PersonalCommentsText"), CrystalDecisions.CrystalReports.Engine.TextObject)
SampleReportText.Text = Me.txtTextObject.Text
3)Via a formula: create a formula, give it a clear name and drag it on your form. There's no code needed inside the formula body.
Whatever way you choose: set it's 'can grow' option to true. Select the field inside your report, right click, select Format Field, and you should check the 'Can Grow' box in the common tab.
Dim crFormulas As FormulaFieldDefinitions
Code:
'formulas that will contain the field name
Dim crFormulaTextField1 As FormulaFieldDefinition
'set the Formulas collection to the current report's formula
'collection
crFormulas = rpt.DataDefinition.FormulaFields
crFormulaTextField1 = crFormulas.Item("PersonalCommentsFormula")
crFormulaTextField1.Text = "'" & Me.txtFormula.Text & "'"
Check the attachment for a full blown example!
Edit:In this thread someone pointed out a problem with multiple line text when working with the formula solution. I made a solution to it. The trick is to tell CR that there is a CR/LF in the text. This is done by adding the string "Chr(10) & Chr(13)" to your text. Note that I said "string", not "characters". CR has to interprete this string itself, and will translate that string to a CR/LF itself!!
This is also for displaying single quotes in your string.
Code:
Try
Dim crFormulas As FormulaFieldDefinitions
'formulas that will contain the field name
Dim crFormulaTextField1 As FormulaFieldDefinition
'set the Formulas collection to the current report's formula
'collection
crFormulas = rpt.DataDefinition.FormulaFields
crFormulaTextField1 = crFormulas.Item("PersonalCommentsFormula")
crFormulaTextField1.Text = TranslateStringToCRFormula(Me.txtFormula.Text)
Catch ex As Exception
End Try
Private Function TranslateStringToCRFormula(ByVal VBString As String) As String
Dim Returnstring As String = "'"
'Split the string at every LF
For Each SubString As String In VBString.Split(Chr(10))
SubString = SubString.Replace("'", "' & Chr(39) & '")
'Trim all the CR / LF characters
SubString = SubString.Trim(vbCrLf.ToCharArray)
'Form your string to the compatible CR Formula format. Chr(10) &nd Chr(13) should be inserted as a string, not as values!!
Returnstring = Returnstring & "' & Chr(10) & Chr(13) & '" & SubString
Next
Returnstring = Returnstring & "'"
Return Returnstring
End Function
Edit: One of our users, Makavelli, found a problem in this last method (it returns a new line regardless if there is one or not ). Here's his fix:
Code:Public Function TranslateStringToCRFormula(ByVal strValue As String) As String
Dim Returnstring As String = "'"
Dim i As Integer = 0
'Split the string at every LF
For Each SubString As String In strValue.Split(Chr(10))
'Trim all the CR / LF characters
SubString = SubString.Trim(vbCrLf.ToCharArray)
If i > 0 Then
Returnstring &= "' & Chr(10) & Chr(13) & '"
End If
i += 1
SubString = SubString.Replace("'", "' & Chr(39) & '")
Returnstring &= SubString
Next
Returnstring = Returnstring & "'"
Return Returnstring
End Function
The attached program shows you how to do this.
Basically we have three different scenarios:
1)Via a parameter: this is the standard communication between VB.NET and a report.
This is not my recommended way to set a text on a report. It's quite some code, and it's prone to errors. I had some strange experiences with it in the past (like a maximum text length of 254 bytes). Consult the updates and monthly fixes on the support site: http://www.businessobjects.com/services/support/default.asp
Code:Dim paramFields As ParameterFieldDefinitions
Dim paramField As ParameterFieldDefinition
Dim paramValue As ParameterValues
Dim paramDiscreteValue As New ParameterDiscreteValue
paramFields = rpt.DataDefinition.ParameterFields
SetParameter(paramFields, "PersonalCommentsParameter", Me.txtParameter.Text)
Public Sub SetParameter(ByVal paramDef As ParameterFieldDefinitions, ByVal paramName As String, ByVal paramValue As String)
Dim crParameterFieldDefinition As ParameterFieldDefinition = paramDef.Item(paramName)
Dim crParameterValues As ParameterValues = crParameterFieldDefinition.CurrentValues
Dim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = paramValue
crParameterValues.Add(crParameterDiscreteValue)
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
End Sub
2)Via a text object: This is my recommended way to go: create a text object in your report, give it a clear name and drag it on your form.
Code:Dim SampleReportText As CrystalDecisions.CrystalReports.Engine.TextObject = CType(rpt.ReportDefinition.ReportObjects.Item("PersonalCommentsText"), CrystalDecisions.CrystalReports.Engine.TextObject)
SampleReportText.Text = Me.txtTextObject.Text
3)Via a formula: create a formula, give it a clear name and drag it on your form. There's no code needed inside the formula body.
Whatever way you choose: set it's 'can grow' option to true. Select the field inside your report, right click, select Format Field, and you should check the 'Can Grow' box in the common tab.
Dim crFormulas As FormulaFieldDefinitions
Code:
'formulas that will contain the field name
Dim crFormulaTextField1 As FormulaFieldDefinition
'set the Formulas collection to the current report's formula
'collection
crFormulas = rpt.DataDefinition.FormulaFields
crFormulaTextField1 = crFormulas.Item("PersonalCommentsFormula")
crFormulaTextField1.Text = "'" & Me.txtFormula.Text & "'"
Check the attachment for a full blown example!
Edit:In this thread someone pointed out a problem with multiple line text when working with the formula solution. I made a solution to it. The trick is to tell CR that there is a CR/LF in the text. This is done by adding the string "Chr(10) & Chr(13)" to your text. Note that I said "string", not "characters". CR has to interprete this string itself, and will translate that string to a CR/LF itself!!
This is also for displaying single quotes in your string.
Code:
Try
Dim crFormulas As FormulaFieldDefinitions
'formulas that will contain the field name
Dim crFormulaTextField1 As FormulaFieldDefinition
'set the Formulas collection to the current report's formula
'collection
crFormulas = rpt.DataDefinition.FormulaFields
crFormulaTextField1 = crFormulas.Item("PersonalCommentsFormula")
crFormulaTextField1.Text = TranslateStringToCRFormula(Me.txtFormula.Text)
Catch ex As Exception
End Try
Private Function TranslateStringToCRFormula(ByVal VBString As String) As String
Dim Returnstring As String = "'"
'Split the string at every LF
For Each SubString As String In VBString.Split(Chr(10))
SubString = SubString.Replace("'", "' & Chr(39) & '")
'Trim all the CR / LF characters
SubString = SubString.Trim(vbCrLf.ToCharArray)
'Form your string to the compatible CR Formula format. Chr(10) &nd Chr(13) should be inserted as a string, not as values!!
Returnstring = Returnstring & "' & Chr(10) & Chr(13) & '" & SubString
Next
Returnstring = Returnstring & "'"
Return Returnstring
End Function
Edit: One of our users, Makavelli, found a problem in this last method (it returns a new line regardless if there is one or not ). Here's his fix:
Code:Public Function TranslateStringToCRFormula(ByVal strValue As String) As String
Dim Returnstring As String = "'"
Dim i As Integer = 0
'Split the string at every LF
For Each SubString As String In strValue.Split(Chr(10))
'Trim all the CR / LF characters
SubString = SubString.Trim(vbCrLf.ToCharArray)
If i > 0 Then
Returnstring &= "' & Chr(10) & Chr(13) & '"
End If
i += 1
SubString = SubString.Replace("'", "' & Chr(39) & '")
Returnstring &= SubString
Next
Returnstring = Returnstring & "'"
Return Returnstring
End Function
Labels: Crystal Reports

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home