Print Query Results

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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;
     fm.Query1.Params[1].asDateTime:= StrToDateTime(edit2.text);
     fm.Query1.Params[1].asDateTime:= StrToDateTime(edit3.text);


2) Make it yourself: Something like:

  Bookmark: TBookmark;
  PrintFile: TextFile;
  AssignPrn (PrintFile);
  Rewrite (PrintFile);
    Printer.Canvas.Font := Font;
    Bookmark := Table1.GetBookmark;
        while not Table1.EOF do
          Writeln (PrintFile, 'Caption: ',
            Table1.FieldByName ('SomeField').AsString);
          Writeln (PrintFile, 'Another l: ',
            Table1.FieldByName ('AnotherField').AsString);
          Writeln (PrintFile);
      Table1.GotoBookmark (Bookmark);
      Table1.FreeBookmark (Bookmark);
    System.CloseFile (PrintFile);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tam97Author Commented:
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
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;

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

tam97Author Commented:
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

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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.