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:

  1. Establish the SP that will supply data for the report
  2. Establish a VB ‘model’ for the data in the report using a VB Dataset.XSD file based on your SP results.
  3. Create the report and link it to the Dataset (DS) table created above.
  4. Design the report using the visual tools in Crystal Reports (CR).
  5. 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.)
  6. Write code to execute your SP and store the result into a DataSet on the form creating the CR report viewer
  7. 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.)
  8. 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:

  1. Design the report using the visual tools in Crystal Reports (CR) and add Text Object, give it name eg Text1.
  2. Create a CR report viewer on a form(s).
  3. 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.

14 Responses to “Changing Report Objects Run Time”

  1. Rebecca Says:

    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?

  2. 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.

  3. 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.

  4. Prakash.Jai Says:

    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………

  5. Prakash.Jai Says:

    hi,

    I set that text object in crystal report Can Grow property enabled still i am not getting…..

    ok

  6. hi prakash,
    u can try this out.

    strString = “J.Prakash+chr(13)+Jaya+chr(13)+Hot guy”
    rptTxtGroup.Text = strString

    Regards,
    vikram

  7. svanistelrooy Says:

    Hi, referring to this line of code :

    rptTxtGroup = report.ReportDefinition.ReportObjects.Item(”Text1″)

    what is report?
    Thanks

  8. @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

  9. 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

  10. 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

  11. 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

  12. (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

  13. Hi,

    Can anyone help me to get the calcualted value of a Crystal report formula in VB.net.

    Thanks in advance…

Leave a Reply