Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Print Query Results

Posted on 1997-12-27
Medium Priority
Last Modified: 2010-04-04
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
Question by:tam97
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

Accepted Solution

mvz121697 earned 1000 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;
     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);


Author Comment

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

Expert Comment

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


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


Author Comment

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

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.


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

636 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