?
Solved

Print Query Results

Posted on 1997-12-27
4
Medium Priority
?
227 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 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;
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

Independent Software Vendors: 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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

762 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