ADO Database Delphi

Title: VCL for Generic TDataSet print of ALL or selected columns
Question: Need a quick component to print contents of TTables or TQueries that is GENERIC ?
Set the DataSet property, the FieldList property for selected fields or leave the list EMPTY for ALL fields and the Title. Call Execute.
Answer:
unit QRPrtTab;
interface
// ==================================================================
// Print TQuery or TTable (TDataSet) by column using QuickReport
// Mike Heydon Oct 2000
//
// This is an extract of a more complex VCL. The more complex one
// has Print,Preview and Printer setup options. Since these use
// additional forms and VCL's I have simplified the following code.
// It should be easy to incorporate your own routines.
//
// Properties
//
// DataSet TDataSet
// Title Main title of report
// DetailBandHeight Height of printed detail band
// DisableControls Disable refresh of DataSet to avoid rapid
// Screen updates
// PrintFields String list of field names to print.
// (EMPTY = ALL fields)
// BeforePrint Event triggered before the print
// AfterPrint Event trigered after the print
//
// Methods
//
// Execute Print DataSet with selected fields
// =================================================================
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, db, QRExtra, QuickRpt, QrPrntr;
type
TQRPrintTable = class(TComponent)
private
{ Private declarations }
FFieldList : TStringList;
FDisableControls : boolean;
FBeforePrint,FAfterPrint : TNotifyEvent;
FBandHeight : byte;
FTitle : string;
FDataSet : TDataSet;
procedure SetFFieldList(NewValue : TStringList);
protected
{ Protected declarations }
FieldList : TStringList;
procedure Notification(AComponent : TComponent;
Operation : TOperation); override;
public
{ Public declarations }
constructor Create( AOwner: TComponent ); override;
destructor Destroy; override;
procedure Execute;
published
{ Published declarations }
property Title : string read FTitle write FTitle;
property DataSet : TDataSet read FDataSet write FDataSet;
property BeforePrint : TNotifyEvent read FBeforePrint
write FBeforePrint;
property AfterPrint : TNotifyEvent read FAfterPrint
write FAfterPrint;
property DetailBandHeight : byte read FBandHeight
write FBandHeight;
property DisableControls : boolean read FDisableControls
write FDisableControls;
property PrintFields : TStringList read FFieldList
write SetFFieldList;
end;
procedure Register;
// =================================================================
implementation
procedure Register;
begin
RegisterComponents('Data Controls', [TQRPrintTable]);
end;
constructor TQRPrintTable.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FieldList := nil;
FFieldList := TStringList.Create;
FDisableControls := true;
FBandHeight := 15;
FTitle := '';
FDataSet := nil;
end;
destructor TQRPrintTable.Destroy;
begin
FFieldList.Free;
inherited Destroy;
end;
procedure TQRPrintTable.SetFFieldList(NewValue : TStringList);
begin
FFieldList.Assign(NewValue);
end;
procedure TQRPrintTable.Execute;
var SavePlace : TBookMark;
FQRForm : TForm;
QRReport : TCustomQuickRep;
begin
if FDataSet = nil then
MessageDlg('No DataSet for TQRPrintTable',mtError,[mbOk],0)
else begin
SavePlace := DataSet.GetBookMark;
QRReport := nil;
FQRForm := TForm.Create(self);
try
if FFieldList.Count 0 then FieldList := FFieldList;
if FDisableControls then FDataSet.DisableControls;
QRCreateList(QRReport,FQRForm,FDataSet,FTitle,FieldList);
QRReport.Bands.DetailBand.Height := FBandHeight;
if Assigned(FBeforePrint) then FBeforePrint(self);
if Assigned(QRReport) then QRReport.Print;
if Assigned(FAfterPrint) then FAfterPrint(Self);
finally
if FDisableControls then FDataSet.EnableControls;
FieldList := nil;
QRReport.Free;
FQRForm.Free;
end;
FDataSet.GotoBookmark(SavePlace);
FDataSet.FreeBookMark(SavePlace);
end;
end;
procedure TQRPrintTable.Notification(AComponent : TComponent; Operation : TOperation);
begin
inherited Notification(AComponent,Operation);
if (Operation = opRemove) and (AComponent = DataSet) then FDataSet := nil;
end;
end.