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.
czarubahAsked:
Who is Participating?
 
sYk0Connect With a Mentor Commented:
sorry, I added some font formatting inside the code tags.

Crpe1 := TCrpe.Create(#Not sure if there are any parameters required here#);

var Crpe1:TCrpe;

begin
  Crpe1 := TCrpe.Create(nil);

  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


if you want you can also use the code in a normal program (non-console), edit the project source to match the following.

program CrystalVCL;

uses
  UCrpe {CrystalReport Unit ?UCrpe32?};
  {add other required units here, if needed}

var
  Crpe1:TCrpe;

begin
  Crpe1 := TCrpe.Create(nil);
  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

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

mlmcc
0
 
czarubahAuthor Commented:
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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
mlmccCommented:
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
0
 
czarubahAuthor Commented:
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.
0
 
Ephraim WangoyaCommented:
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.
0
 
czarubahAuthor Commented:
let me try that.
0
 
sYk0Commented:
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).
0
 
czarubahAuthor Commented:
That was the missing piece on my code. I could not find how to initialize the structure. thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.