Link to home
Start Free TrialLog in
Avatar of czarubah
czarubah

asked on

Delphi and Crystal VCL

I have a short delphi program to print a Crystal Report...if I use a form, the code works,but you need to click a button. so I decided to write a console application,

the console application exits after the first line of code, regardless of which line is the first one. I'm using Borland developer studio 6. the code below should work but it does not. i do have a complier message indicating that crpe1 might not have been initialized.
i have tried setting the report name first as shown in sample code.

var Crpe1:TCrpe;

begin
  Crpe1.Printer.SetCurrent();
  Crpe1.ReportName:='c:\rpt\store_incidence.rpt';
  {Run the Report}
  Crpe1.Printer.GetCurrent(true);
  Crpe1.DiscardSavedData;
  Crpe1.Refresh;
  Crpe1.Output := toPrinter;
  Crpe1.Execute;
end.
Avatar of Mike McCracken
Mike McCracken

Why don't you put the code in the form load or init event?

mlmcc
Avatar of czarubah

ASKER

Not quite sure I follow. the events I have are  
Application.Initialize;
  Application.CreateForm(TForm2, Form2);
  Application.Run;

I don't know where to interact with the initalization, I know I can remove the create form so It will not be visible.
Not sure what you mean by you have to click a button.

I don't use Delphi but in VB if I add a form and set it as the startup form then I can put the Crystal running code in the form load event and the report rns when the program is run.

Can you do that in Delphi?

mlmcc
All my forms have controls, most are buttons to interact with the end user...in this case the end user wants to automatically print the report. Since Crystal does not have a direct control to send something to printer I have written applications to allow the end user to preview, print or export the report....

in any case, VB and Delphi have similar aspects, thanks, to your comment I found some ways to load OnCreate event. I'm now trying to see if I can modify the code to run my app on that section.
You can use OnShow event of the form

procedure TForm1.FormShow(Sender: TObject);
begin
  Crpe1.Printer.SetCurrent();
  Crpe1.ReportName:='c:\rpt\store_incidence.rpt';
  {Run the Report}
  Crpe1.Printer.GetCurrent(true);
  Crpe1.DiscardSavedData;
  Crpe1.Refresh;
  Crpe1.Output := toPrinter;
  Crpe1.Execute;
end.
let me try that.
You can use your code in a console application (if you really want too).

I think your problem is that Crpe1 is not created (initialized), this is why your application doesn't get past the "first line of code", it's trying to read from memory that doesn't actually exist (and possibly crashes).

var Crpe1:TCrpe;

begin
  [b]Crpe1 := TCrpe.Create([u]#Not sure if there are any parameters required here#[/u]);[/b]

  Crpe1.Printer.SetCurrent();
  Crpe1.ReportName:='c:\rpt\store_incidence.rpt';
  {Run the Report}
  Crpe1.Printer.GetCurrent(true);
  Crpe1.DiscardSavedData;
  Crpe1.Refresh;
  Crpe1.Output := toPrinter;
  Crpe1.Execute;
end.

Open in new window


The reason why the code works on the form is because the component is created automatically when the form is created (this is not the case with a console application).
ASKER CERTIFIED SOLUTION
Avatar of sYk0
sYk0

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
That was the missing piece on my code. I could not find how to initialize the structure. thanks.