?
Solved

Delphi and Crystal VCL

Posted on 2011-05-10
9
Medium Priority
?
1,557 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:czarubah
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 101

Expert Comment

by:mlmcc
ID: 35730465
Why don't you put the code in the form load or init event?

mlmcc
0
 

Author Comment

by:czarubah
ID: 35730650
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
 
LVL 101

Expert Comment

by:mlmcc
ID: 35731193
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:czarubah
ID: 35731643
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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 35734348
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
 

Author Comment

by:czarubah
ID: 35734684
let me try that.
0
 
LVL 3

Expert Comment

by:sYk0
ID: 35736064
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
 
LVL 3

Accepted Solution

by:
sYk0 earned 2000 total points
ID: 35736113
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
 

Author Closing Comment

by:czarubah
ID: 35738294
That was the missing piece on my code. I could not find how to initialize the structure. thanks.
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question