• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Print Query Results

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
tam97
Asked:
tam97
  • 2
  • 2
1 Solution
 
mvz121697Commented:

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
 
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
0
 
mvz121697Commented:
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
 
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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now