Changing Report Objects Run Time
Creating and using a Crystal Reports report in a VB.Net project using SQL Server is a multi-step process. This lesson takes you through the steps using one approach that is suitable for when the report is based on a stored procedure (SP) that generates dynamic data based on either current DB values or parameters passed to the underlying SP. There are other approaches but you will have to investigate them on your own if you are interested.
The steps are:
- Establish the SP that will supply data for the report
- Establish a VB ‘model’ for the data in the report using a VB Dataset.XSD file based on your SP results.
- Create the report and link it to the Dataset (DS) table created above.
- Design the report using the visual tools in Crystal Reports (CR).
- Create a CR report viewer on a form(s) along with whatever other interface elements you need to have (e.g., to provide SP parameter input, execute the report loading, etc.)
- Write code to execute your SP and store the result into a DataSet on the form creating the CR report viewer
- Create a report object in code and set appropriate values, including the location of the DataSet containing the report’s actual data. (Remember, the Dataset.XSD file referenced in Step 2 only contains a model of the data.)
- Write code to pass the report object to the report viewer as the report source. This causes the report to become visible in the viewer.
Sometimes it is useful to be able to change the values of text objects on a report in code. This is done in the SQL Database Documenter program (see the lesson on documenting SQL Server databases). You can change report text objects by creating a text object reference in your code and then setting properties. This must take place before the report object is passed to the report viewer control.
Note: objReport must have been declared and instantiated previously as illustrated in the “R” lines in the code listing above. Assignments such as the one below must take place before the report object (objReport) is passed to the report viewer.
Steps to do:
- Design the report using the visual tools in Crystal Reports (CR) and add Text Object, give it name eg Text1.
- Create a CR report viewer on a form(s).
- Take a looks on this code:
Dim rptTxtGroup As CrystalDecisions.CrystalReports.Engine.TextObject
rptTxtGroup = report.ReportDefinition.ReportObjects.Item(“Text1″)
rptTxtGroup.Text = “Halooo”
Yes, just this… very simple ist’n … he he he. Btw, you can use it in all object excepting OLE Object eg Picture or Blob mode.
4 March 2008 at 5:48 am
I tried running the above code to set the value of a text object, and it did not work – I am new to Crystal Reports – do I need to do anything else to get this to work. My code is not blowing up, in fact in the watch window I can verify that the textobject variable is being set correctly. Any ideas?
4 March 2008 at 9:40 am
Hai Rebecca, a’ru sure your code and steps like me? sure this code on your top header in your code:
Imports CrystalDecisions.CrystalReports.Engine.TextObject
Imports CrystalDecisions.Shared
and please, make sure text Object name not same, couse crystal will crazy. If still not happent, please post yor code.
11 March 2008 at 10:52 pm
nice job. What i want do do is to add objects to report (new lines for example)
I took a look on ReportObjects and there is no method for inserting/adding.
So if i want to build a dynamic report i have to draw the maximum number of lines that i can use and then i have to set the properties for each line.
If you know something about inserting objects, drawing line, feel free and share with us.
29 March 2008 at 2:35 pm
Hi,
Very Very Nice Job.I want say many many thanks to you.Can u post the same Text box how we set the multi line string
Ex: strString = “J.Prakash” & vbcrlf & “Jaya” & vbcrlf & “Hot guy”
rptTxtGroup.Text = strString
I set like this but its not working in crystal report. I am working with vb.net 2008 & there inbuilted crytal report i am using. Pls help me…
Thanks in Advance………
29 March 2008 at 2:37 pm
hi,
I set that text object in crystal report Can Grow property enabled still i am not getting…..
ok
1 April 2008 at 12:47 pm
hi prakash,
u can try this out.
strString = “J.Prakash+chr(13)+Jaya+chr(13)+Hot guy”
rptTxtGroup.Text = strString
Regards,
vikram
6 May 2008 at 2:35 pm
Hi, referring to this line of code :
rptTxtGroup = report.ReportDefinition.ReportObjects.Item(”Text1″)
what is report?
Thanks
7 May 2008 at 8:35 am
@Hi svanistelrooy…
It’s look like u have crystal design name CrExample.rpt
the referrance may like this:
Dim report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrExample
I have make u clear
4 June 2008 at 1:52 pm
Hi
Dim objRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalReport3
Dim rptTxtGroup As TextObject
rptTxtGroup = objRpt.ReportDefinition.ReportObjects.Item(“Text7″)
rptTxtGroup.Text = “abcedfg”
MessageBox.Show(rptTxtGroup.Text)
this is the code to display “abcedfg” to crystal report text7 field.. but i not able to see the value on the text7 in crystal report.. help me…
Regards
Sridhar
4 June 2008 at 3:16 pm
Dim objRpt As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalReport3
Dim rptTxtGroup As TextObject
rptTxtGroup = CType(objRpt.ReportDefinition.ReportObjects(“Text7″), TextObject)
rptTxtGroup.Text = Form1.TextBox1.ToString()
MessageBox.Show(rptTxtGroup.Text)
i can able to get the value of the texbox1 in popup window but i not able to display this in crystal report text filed.. i am using vb.net visual studio 2008 with crystal report 9i
help me ..
Thanks in advance
4 June 2008 at 7:12 pm
Hi,how to write sql select satement inside sql expression field to pikup the number from 1 to 100
give some examples to write sql query in sql expression field..
thanks and regards
Sridhar
4 June 2008 at 7:17 pm
(SELECT “PETTYCASH”.”PARTICULARS”,”PETTYCASH”.”VOUCHERDT”,”PETTYCASH”.”LOCATION”,”PETTYCASH”.”COSTCENTER”,”PETTYCASH”.”ACCTCODE”,”PETTYCASH”.”PROJCODE”,”PETTYCASH”.”CASHPAID”
FROM
“PETTYCASH”
WHERE “PETTYCASH”.”VOUCHERNO” = {?StartNo} TO {@EndNo})
is this code write for sql expression field in crystal report…
regards
Sridhar
9 April 2009 at 1:50 pm
Hi,
Can anyone help me to get the calcualted value of a Crystal report formula in VB.net.
Thanks in advance…
6 November 2009 at 10:20 pm
Here is the c# version:-
http://prabhat.me/2009/11/06/change-the-value-of-itextobject-of-crystal-report-at-runtime/