rapid applications programming with windows week 11
TRANSCRIPT
Rapid Applications Programming with Windows
Week 11
Topics Week 11
TQuery Components (from Week 10)Creating Reports in Delphi 5
QuickReportInterface Techniques
Splash screen Tooltips Bitmaps
TQuery Components
Usually result in more efficient processing specify DataBaseName, SQL statement at
design time set Active to True, RequestLive for
updateable data At run-time e.g. in the FormCreate Event:
Query1.SQL.Clear;Query1.SQL.Add(‘Select Name, Capital from
CountryTable’);Query1.Open;
TQuery Components
Use ExecSQL for SQL statements which do not return records:
Query1.SQL.Clear;Query1.SQL.Add(‘insert into CountryTable’);Query1.SQL.Add(‘ (Name, Capital) ‘);Query1.SQL.Add(‘ (values (“Australia”, “Canberra”) ’);Query1.ExecSQL; Can also build up the query as a string, then ‘Add’
the whole stringvar query: stringquery := query + ‘Select * from Orders where ‘; etc
TQuery ComponentsUsing Parameters in SQL statements: At design time, the SQL statement isselect * from CountryTable where name = :ParCountry; At run-time e.g. OnExit event of the edit controlWith Query1 dobeginDisableControls;Close;ParamByName(‘ParCountry’).AsString := edtCountry.Text;Open;EnableControls;end;
TQuery components Use the Datasource property to automatically
link to another datasource and access any matched-name fields, which can be used as parameters in the SQL for the Query.
Useful in master/detail forms. E.g. table1/ds1 accesses the customer table, query1/ds2 retrieves orders which match the
CustNo of the ‘current’ customer record: (CustNo is a foreign key in the orders table)
set the Datasource property of the Query to point to ds1 :CustNo becomes the parameter for the query
SQL property of the Query is:select * from orders where orders.CustNo = :CustNo;
QuickReport
QuickReport 3.0 Standard (limited) edition is included with Delphi 5 Demo program qr3demo.exe
For the Professional version, see http://www.qusoft.com
$99/license, 50% discount for students, unis Complete user documentation available to
registered users Web site provides FAQ, technical support,
message board, free downloads, tutorials
QuickReportTightly integrated with Delphi 5
a QReport tab on the palette, providing components with properties, events, and On-line Help
File|New Page -> Report module (not recommended!)
File|New, then Forms Page->labels, list, master/detail File|New, then Business Page -> QuickReport Wizard Works with the BDE to retrieve data from DB tables:
but not from Access tables in the Standard Edition A QuickReport ‘form’ uses the Forms Designer,
behaves like a Delphi form e.g. frmReport.Show; Allows user to preview, print the report from within
the Delphi application
QuickReport
QuickReport features: Full preview feature while designing Creates reports from database tables, queries,
text files etc Allows printing of labels, database fields,
calculations, memo fields, pictures, icons and shapes
Uses any Windows font, size, style and color Reports can be saved to file for later printing Event handlers can be attached to most parts
of the report for further control
QuickReport The report consists of ‘bands’ each with a particular
purpose e.g. a Page Header, Page Footer, Column Header, Summary etc
Data displayed can come from any DataSource, including TTable, TQuery and from lists, arrays, text files (use the OnNeedData event)
Provides Master/Detail report features Supports ‘groupings’ of data with unlimited levels, totals at
each level Automatically perform calculations like summary and
counting of fields. You can reset calculations at group level.
Provides a component for printing system data e.g. page number, date, time, report title
QuickReportA report consists of
1 QuickRep componentTransforms the Delphi form into a report
• drop a QuickRep component onto a formProperties, Methods and Events relate to the
whole reportAlso created via File|New->Report (not
preferred)
1->n QRBandsthe BandType property specifies which part of
the report the band is attached to, which in turn determines the type of data the designer will display in it
QuickReportQRBand BandTypes: rbPageHeader
Page Header band, printed automatically at the top of each page
Printing on first page depends on the setting of the TQuickRep.Options.FirstPage Header property
Multiple such bands are printed in design order rbTitle
Report title band, printed automatically, once at the beginning of the report, just after the Page Header
Can have several for successive headings Printed in design order
QuickReport
QRBand BandTypes:rbColumnHeader
Like the Page Header Printed automatically on top of every column
for each page
rbDetail specifies what is printed for each record in the
report’s master dataset effectively ‘replicated’ for each record can have many rbDetail bands in a report printed automatically
QuickReport
QRBand BandTypes:rbPageFooter
automatically printed at the end of each page.
Printing on last page depends on TQuickRep.Options.LastPageFooter property
rbSummary Automatically printed at the end of a report
after all detail bands Can be more than one
QuickReport
QRBand BandTypes:rbGroupHeader
For printing information at the beginning of a new group e.g. the group identification: City: Melbourne
Used in a Master/Detail report, a ‘grouped’ report Must be linked to a QRGroup or a QRSubDetail
component to be printed
rbGroupFooter for printing information at the end of a group of
records eg. Total Customers in Melbourne: 23 as for rbGroupHeader
QuickReport
QRBand BandTypes:rbSubDetail
used as special band type by the QRSubDetail component, which acts as a band
used for printing the detail table’s data in a master/detail report
do not set this band type manually
rbOverlay Overlays all other bands, positioned from top left
corner (NOT USED IN QR3 - included for backward compatibility)
QuickReport Design ElementsComponents for static display
labels, images, shapes, text very similar to the standard VCL components
Components for the display of data from a dataset e.g. a database QRDBText, QRDBImage, QRDBRichText usually placed on the detail band of a report
Components with specialised functions QRSysData: display date, time, page number etc QRExpr: define, display calculated results e.g.
Average, Count, Sum etc DisplayFormat property formats data
QuickReport: Creating Reports
Start with one QuickRep component Effectively the ‘canvas’ where you place the
elements that the report will display some properties affect how the report will appear
when printed: Page, PrinterSettings, Options Dataset property points to the TTable or TQuery
where the report will get its data from (+-Filter) methods Preview, Print e.g.
Form2.QuickRep1.Preview Double-Click on the QuickReport canvas to invoke
the Report Settings dialog - combines many of the other properties in one place
QuickReport:DB Data in Reports
Drop a TTable or TQuery component onto the report
Set DatabaseName, then TableName/SQL, then Active = True Add all required fields via the Fields Editor now set the Dataset property of the QuickRep
component to point to the TTable/TQuery Create a Detail band: expand the Bands property of
the Quickrep component, then set HasDetail to True Place data-aware Report components on the detail
band e.g. QRDBText, and link them to the TTable/TQuery via the DataSet and DataField properties
QuickReport: A Simple List Report
PageTitle band showing the report title, what data is displayed e.g. Sales figures for January, QRSysData component to display e.g. date, time of
report. Text is printed before the Data e.g. Page 1 QRSysData can display the Report Title defined in the
QuickRep component Detail band which repeats for each record in the dataset
Use QRDB…components Summary band with totals
Use a QRExpr component, and define totals via the Expression Wizard (access via theExpression property)
QuickReport: Reports with Groups
Records must be ‘ordered’ according to the group field e.g. City, Company, Date
Group field can be a table index Set the IndexName property of a TTable
component
Or write SQL for a TQuery component to sort the records for the dataset
QuickReport provides a GroupHeader band and a GroupFooter band for printing of data before and after the group detail records
QuickReport: Reports with Groups
To create a ‘Grouped’ Report: Add the QRGroup component, set its Expression
property to the field/expression that the data will be grouped on e.g. City ORCOPY(Table1.Company, 1,1) // extract 1st Char
By default, a Group Header Band has been created, with its Master property pointing to the QuickReport component
Create a Group Footer Band, place e.g. a calculated total for the group on it (Use a QRExpr component, and set the ResetAfterPrinting property)
QuickReport:Reports with Groups
Set the FooterBand property of the Group Header Band to the new Group Footer Band.
To ensure that a group footer band is printed on the same page as the detail information, set its LinkBand to the detail band name
Set ReprintOnNewPage on Group Header if neededTo print a report with each record on a new page,
set the ForceNewPage property of the detail band to True
Right-click the QuickReport component and choose ‘Preview Report’ to view the report at design time
QuickReport:Master/Detail Reports
For each Master (e.g. Customer) record printed, all the Detail (e.g. Orders) records will be printed immediately following
Each time the current record in the master dataset changes, the new values in the linked fields are used to select corresponding records in the detail dataset
To create a Master/Detail Report Start with a Group Report, sorted by e.g. Custno Add a DataSource component pointing to the master
dataset (TTable linked to Customers.db) Add a TTable for the detail data e.g. Orders.db Set its MasterSource to DataSource1
QuickReport:Master/Detail Reports
To create a Master/Detail Report… Set the IndexName of the detail dataset to the field
which will link the data in the two tables e.g. Custno Set the MasterFields property of the detail dataset
via the Browse button -> Field Link Designer (Custno) Add a QRSubDetail component (it displays as a band)
to display the detail data Set its dataset property to the detail dataset, and its
Master property to the QuickReport component Place QR data-aware components on the subdetail
band Can create many levels of detail bands
QuickReportTo sum two aggregates:
QRExpr1.Expression := Sum(Field1) QRExpr2.Expression := Sum(Field2) QRExpr3.Expression := Sum(Field1 + Field2)
To print a footer only on the last page of the report: use a Summary Band instead, and set it’s
AlignToBottom property to True.
To suppress printing of a record: Write code for a band’s BeforePrint eventPrintBand := CustTableTotalSales > 100; When PrintBand is set to False, band will not print
Interface Techniques
Launch the Splash screen from the project’s DPR file, before any of the forms are instantiated e.g.
beginApplication.Initialize;frmSplash := TfrmSplash.Create(Application);frmSplash.Show;frmSplash.Refresh;
……code to create all the other formsfrmSplash.Free;Application.Run;
end.
Interface Techniques
Display ToolTips Specify the Hint property for each control that
a Help Hint should appear for. Set the ShowHint property of each appropriate
control to True To force all form controls to show the form’s Hint
property, set the ParentShowHint property of all controls to True
To enable/disable all Help Hints, set the value of the application's ShowHint property to True/False. E.g. at FormCreate:Application.ShowHint = True
Interface Techniques:Using BitmapsDrop an ImageList component onto a formDouble-click to invoke the Image EditorAdd images as required: note their index
position in the listUse in a menu:
Set the Images property of the MainMenu component to the ImageList, then
Set the ImageIndex property of each menu item
Or set the Bitmap property of each menu item
Tutorial Week 11
Finish the Delphi 5 familiarisation exercise from last week
Work through this week’s exercises to learn how to create reports
Assignment Tasks: Create a Splash screen and link it to
your assignment Start work on the assignment reports