Link to home
Start Free TrialLog in
Avatar of Monik22
Monik22

asked on

Creating crystal reports

I USE DELHPI 6, AND CRYSTAL REPORTS 8.5, AND THE DATABASE IS MS SQL SERVER.

THIS IS WHAT I HAVE ON MY CODE:

unit ReportUnit;

interface

uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, DB, ADODB, dbtables, CheckLst, Buttons, CRVIEWERLib_TLB, CRAXDRT_TLB,
   OleCtrls, OleServer;

type
   TReportForm = class(TForm)
   ReportViewer: TCRViewer;
   CrystalApplication: TApplicationCrystal;
   procedure FormCreate(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
   CrReport : Ireport; //the new Report
   CrField  : IFieldObject;  //use to format fields
   CrDbField : IDatabaseFieldDefinition; //use a database field
 end;

var
   ReportForm: TReportForm;

implementation
uses
MainUnit;

{$R *.dfm}

procedure TReportForm.FormCreate(Sender: TObject);
var
   i : Integer;
   blank : olevariant;

begin
   //Create report
   CrReport := CrystalApplication.NewReport;

   //Add a table to a the report
   CrReport.Database.Tables.Add('GeoffreySQL',blank,blank,blank,blank,blank,blank,blank,blank,blank);

   for i := 0 to MainForm.ColumnsCheckListBox.Items.Count - 1 do
   begin
       if MainForm.ColumnsCheckListBox.Checked[i] then
       begin
           //Add a Couple of fields to the detail section
           CRField := CrReport.Sections.Item ['D'].AddFieldObject(MainForm.ColumnsCheckListBox.Items.Strings[i],600,0);
       end;
   end;
   //set the report to the CrViewer object and view the report
   ReportViewer.ReportSource := CrReport;
   ReportViewer.ViewReport;
end;
end.

MY CODE IS FAILING AT THE "//Add a table to a the report" PART, CAN SOMEONE HELP ME WITH THIS.  THIS IS MY FIRST TIME USING DELPHI.

THANKS,
MONICA.
Avatar of Mike McCracken
Mike McCracken

Avatar of Monik22

ASKER

I don't know what does blank equal.
This is what the original code looked like:

{Created by Trevor Dubinsky on Tuesday February 22nd 1999

This is a simple application demonstrating how to create a report
on the fly using Delphi.

First step is to choose import type library from the Component menu
Import "Crystal Reports 8 Active x Designer Run Time Library.  It will
complain that TReport and TDatabase are already registered.  Just rename
them to TCrReport and TCrDatabase

You will also want to import the CRystal reports viewer control.

Once these are installed the following code should work.

Note that there is a TApplication object on the main form.  This object
is a craxdrt.application object and will overide the Delphi application object'
I don't know the work around to this yet.

I will be using a sample table called customer.db which is in this project
directory, in code I look for it in the root of C Drive.

I do want to reiterate that this is a VERY SIMPLE example showing how to create
a detailed report.  I will update this to have a white paper with better
description and information; as time permits}

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  OleCtrls, CRVIEWERLib_TLB, CRAXDRT_TLB, OleServer;

type
  TForm1 = class(TForm)
    CRViewer1: TCRViewer;
    Application1: TApplication;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    CrReport : Ireport; //the new Report
    CrField  : IFieldObject;  //use to format fields
    crDbField : IDatabaseFieldDefinition; //use a database field
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
   DbLocation : String; //holds database location string
   location,
   blank : olevariant;  //some of our calls use olevariants to pass strings
                        //blank is for null

begin
   //First create a new report
    CrReport :=  application1.NewReport;

   //Add a table to a the report
   Location := 'c:\customer.db';
   CrReport.Database.Tables.Add (location,blank,blank,blank,blank,blank,blank,blank,blank,blank);

   //Add a Couple of fields to the detail section
   CRField := CrReport.Sections.Item ['D'].AddFieldObject('{Customer.Customer Name}',600,0);
   CRField := CrReport.Sections.Item ['D'].AddFieldObject('{Customer.last years sales}',4500,0);

   //Lets add a group to the report (by region, in ascending order)
   CRDbField := CrReport.Database.Tables.Item [1].Fields[12];
   crreport.AddGroup (0,CrDbField,crGCAnyValue,CrAscendingOrder);

   //Add the region field to the Group header section and format it
   CRField := CrReport.Sections.Item ['GH1'].AddFieldObject('{Customer.Region}',600,0);
   CRField.TextColor := clblue;

   //set the report to the CrViewer object and view the report
   CRViewer1.ReportSource := CrReport;
   CRViewer1.ViewReport;
end;

end.
Dear expert(s),

A request has been made to close this Q in CS:
https://www.experts-exchange.com/questions/20455028/Deleting-questions.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
Sorr ythis took so long but I was gone for about 10 days and have been slow getting through the emails.

Take a look at

http://support.crystaldecisions.com/communityCS/TechnicalPapers/apps_delphi.pdf

mlmcc
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Monik22

ASKER

Can you close this Q and refund points.

Thanks
Hi Monik22,

Giving me a "C" grade doesn't help.
I denied the request as you didn't comment on the link mlmcc provided you with.

As I'm not allowed to have expert points as moderator, I've transferred them to a "Points for mlmcc" question:
https://www.experts-exchange.com/questions/20467175/Points-for-mlmcc.html

When you disagree with my handling, feel free to mail the site admin:
computer101@experts-exchange.com

modulo

Community Support Moderator
Experts Exchange