Solved

Print Query Results

Posted on 1997-12-27
4
226 Views
Last Modified: 2010-04-04
Hi
Can anyone help me with some coding please ?

I'm trying to code a button to print the results of a dynamic query.

So as to help you get a clear picture of what I've done, I'll give you details of what is on the form. It is made up of a lookupbox showing Initials from a StaffTable, this value is sent to a hidden editbox which is parameter 1 of the SQL query, the next 2 edit boxes are From & To (dates) Params 2+3.The query shows all records for this individual from a holiday table. The query runs great and is displayed on a dbGrid. The non-visuals are 1 TQuery, 1TTable and 2 TDatasources, the tables are Paradox and its Delphi3.

I now want to click on a button and print the records shown in the dbGrid. This is part of my final year college project and I would be very greatful if somebody could help me.

Thanks for your time.

Regards Tam97
0
Comment
Question by:tam97
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
mvz121697 earned 250 total points
ID: 1354724

1) Use QuickReport.
Since you are using Delphi 3: look at File-New-Business-QuickReport Wizard.
The code under your button could be something like:

var fm:TQuickRepForm;
Begin
  fm:=TQuickRepForm.Create;
  try
     fm.Query1.Close;
     fm.Query1.Params[0].asstring:=edit1.text;
     fm.Query1.Params[1].asDateTime:= StrToDateTime(edit2.text);
     fm.Query1.Params[1].asDateTime:= StrToDateTime(edit3.text);
    fm.Query1.Open;
   fm.QuikRep1.Preview;

finally
  fm.Free;
end;



2) Make it yourself: Something like:

var
  Bookmark: TBookmark;
  PrintFile: TextFile;
begin
  AssignPrn (PrintFile);
  Rewrite (PrintFile);
  try
    Printer.Canvas.Font := Font;
    Bookmark := Table1.GetBookmark;
    try
      Table1.DisableControls;
      try
        Table1.First;
        while not Table1.EOF do
        begin
          Writeln (PrintFile, 'Caption: ',
            Table1.FieldByName ('SomeField').AsString);
          Writeln (PrintFile, 'Another l: ',
            Table1.FieldByName ('AnotherField').AsString);
          Writeln (PrintFile);
          Table1.Next;
        end;
      finally
        Table1.EnableControls;
      end;
    finally
      Table1.GotoBookmark (Bookmark);
      Table1.FreeBookmark (Bookmark);
    end;
  finally
    System.CloseFile (PrintFile);
  end;

0
 

Author Comment

by:tam97
ID: 1354725
Hi mvz

Thank you for helping so quick, your've earned a friend from Scotland.

I tried your second suggestion and it worked great!, all I had to do was change table1 to query1 add Printers to the header and it compiled no problem.

Your first suggestion regarding Quick report, would'nt compile, it stopped at the var fm:TQuickRepForm, I dont think its your coding I just dont have enough knowledge on Delphi to work it out.

Do you think you could explain how to get it to compile in more laymans terms, I've only been using Delphi since September and I've a lot to learn.

Also only if you have time, do you have any hints on how to print by the second method in a table layout, ie across the page per record rather than per field.

Thank you once again for all your help

Regards tam97
0
 
LVL 2

Expert Comment

by:mvz121697
ID: 1354726
You should use QR, i think.
In my first example i assumed that you first had created the Quickreport-form (QuickRepForm) yourself.
You can do this in 2 way's:

1) use the wizard:  ' File-New-Business-QuickReport Wizard '

2) Do it yourself:
- Create a form
- drop a QR-component
- drop a Query, and fill in the property's (databasename, sql,params)
- set the band-property of QuickRep1 to have at least a detail-band
- set QuickRep1.dataset to the Query1
- drop QrDbText fields on the detailband
- Set the QrDbText fields property Dataset to Query1
- Set the QrDbText fields property DatasField to required field
- Set Query1.Active to TRUE.
- Right click the QuickRep and select Preview
Now you have a design-time view at the output !
- Save the form

It both above way's you proceed by:
Select the form from where you want to start the report.
Select 'File-Use Unit' and select  your just created QR-form.

You can use 2 ways to do things at runtime:
1) let the form be autocreated at startup (this is default !)

Then you can just issue

QRFormname.Query1.open;
QRFormname.Query1.QuickRep1.Preview;

2) don't let the form be autocreated at stratup
(This can be selected in project-options.
In big projects, this reduces resources at startup)
Then use my 1st example in my 1st answer

Succes and Greetings
from Holland



0
 

Author Comment

by:tam97
ID: 1354727
Hi mvz

Thanks once again for your speedy reply, I followed your explaination and created the Quick report form not problem.

On pressing the button the report preview activates unfortunatly there is no data on it, I checked it by re-writing the SQL to SELECT * From (without dynamic parameters) and it worked.

Therefore I think my problem is with the "filling" of the SQL parameters, the Quick report Query1 is activating correctly but it is doing so without receiving the inputs I've placed into the edit boxes,possibly because its on another form, whereas the Query1 on the form with the edit boxes is receiving the params and working correctly.

I'm not going to ask you to use up any more of your time on this one as your've done more than enough already, I'll work it out somehow, please accept my thanks, I've rated your help as Excellent and the 250 points was well earned.

Happy New Year to you when it comes.

Regrds tam97

P.S.
I've visited Holland many times and always enjoyed it, one of my favourite towns is Goes, I'm hoping to visit Amsterdam during 98.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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