(ebook) crysdev - a developer's guide to integrating crystal reports with visual foxpro (hentzenw

Upload: xuan-madrid-daviran

Post on 16-Oct-2015

384 views

Category:

Documents


7 download

TRANSCRIPT

  • CrysDev: A Developers Guide to Integrating Crystal Reports

    Craig Berntson

    Hentzenwerke Publishing

  • Published by: Hentzenwerke Publishing 980 East Circle Drive Whitefish Bay WI 53217 USA Hentzenwerke Publishing books are available through booksellers and directly from the publisher. Contact Hentzenwerke Publishing at: 414.332.9876 414.332.9463 (fax) www.hentzenwerke.com [email protected] CrysDev: A Developers Guide to Integrating Crystal Reports

    By Craig Berntson Technical Editor: Dan Jurden Copy Editor: Nicole McNeish Cover Art: Crystal by Todd Gnacinski, Milwaukee, WI

    Copyright 2003 by Craig Berntson All other products and services identified throughout this book are trademarks or registered trademarks of their respective companies. They are used throughout this book in editorial fashion only and for the benefit of such companies. No such uses, or the use of any trade name, is intended to convey endorsement or other affiliation with this book. All rights reserved. No part of this book, or the ebook files available by download from Hentzenwerke Publishing, may be reproduced or transmitted in any form or by any means, electronic, mechanical photocopying, recording, or otherwise, without the prior written permission of the publisher, except that program listings and sample code files may be entered, stored and executed in a computer system. The information and material contained in this book are provided as is, without warranty of any kind, express or implied, including without limitation any warranty concerning the accuracy, adequacy, or completeness of such information or material or the results to be obtained from using such information or material. Neither Hentzenwerke Publishing nor the authors or editors shall be responsible for any claims attributable to errors, omissions, or other inaccuracies in the information or material contained in this book. In no event shall Hentzenwerke Publishing or the authors or editors be liable for direct, indirect, special, incidental, or consequential damages arising out of the use of such information or material. ISBN: 1-930919-38-7 Manufactured in the United States of America.

  • This book is dedicated to the memory of my dear friends, Tom Piper and Ed Rauh. Tom, may you go barefoot through the eternities. Ed, even when you knew you were dying, you kept pressing on. May we learn from your example, and may we always remember you both.

    Craig Berntson

  • v

    Our Contract with You, The Reader

    In which we, the folks who make up Hentzenwerke Publishing, describe what you, the reader, can expect from this book and from us.

    Hi there!

    Ive been writing professionally (in other words, eventually getting a paycheck for my scribbles) since 1974, and writing about software development since 1992. As an author, Ive worked with a half-dozen different publishers and corresponded with thousands of readers over the years. As a software developer and all-around geek, Ive also acquired a library of more than 100 computer and software-related books.

    Thus, when I donned the publishers cap five years ago to produce the 1997 Developers Guide, I had some pretty good ideas of what I liked (and didnt like) from publishers, what readers liked and didnt like, and what I, as a reader, liked and didnt like.

    Now, with our new titles for 2002, were entering our fifth season. (For those who are keeping track, the 97 DevGuide was our first, albeit abbreviated, season, the batch of six Essentials for Visual FoxPro 6.0 in 1999 was our second, and, in keeping with the sports analogy, the books we published in 2000 and 2001 comprised our third and fourth.)

    John Wooden, the famed UCLA basketball coach, posited that teams arent consistent; theyre always getting betteror worse. Wed like to get better

    One of my goals for this season is to build a closer relationship with you, the reader. In order for us to do this, youve got to know what you should expect from us.

    You have the right to expect that your order will be processed quickly and correctly, and that your book will be delivered to you in new condition.

    You have the right to expect that the content of your book is technically accurate and up-to-date, that the explanations are clear, and that the layout is easy to read and follow without a lot of fluff or nonsense.

    You have the right to expect access to source code, errata, FAQs, and other information thats relevant to the book via our Web site.

    You have the right to expect an electronic version of your printed book to be available via our Web site.

    You have the right to expect that, if you report errors to us, your report will be responded to promptly, and that the appropriate notice will be included in the errata and/or FAQs for the book.

    Naturally, there are some limits that we bump up against. There are humans involved, and they make mistakes. A book of 500 pages contains, on average, 150,000 words and several megabytes of source code. Its not possible to edit and re-edit multiple times to catch every last

  • vi

    misspelling and typo, nor is it possible to test the source code on every permutation of development environment and operating systemand still price the book affordably.

    Once printed, bindings break, ink gets smeared, signatures get missed during binding. On the delivery side, Web sites go down, packages get lost in the mail.

    Nonetheless, well make our best effort to correct these problemsonce you let us know about them.

    In return, when you have a question or run into a problem, we ask that you first consult the errata and/or FAQs for your book on our Web site. If you dont find the answer there, please e-mail us at [email protected] with as much information and detail as possible, including 1) the steps to reproduce the problem, 2) what happened, and 3) what you expected to happen, together with 4) any other relevant information.

    Id like to stress that we need you to communicate questions and problems clearly. For example

    Your downloads dont work isnt enough information for us to help you. I get a 404 error when I click on the Download Source Code link on www.hentzenwerke.com/book/downloads.html is something we can help you with.

    The code in Chapter 10 caused an error again isnt enough information. I performed the following steps to run the source code program DisplayTest.PRG in Chapter 10, and I received an error that said Variable m.liCounter not found is something we can help you with.

    Well do our best to get back to you within a couple of days, either with an answer or at least an acknowledgment that weve received your inquiry and that were working on it.

    On behalf of the authors, technical editors, copy editors, layout artists, graphical artists, indexers, and all the other folks who have worked to put this book in your hands, Id like to thank you for purchasing this book, and I hope that it will prove to be a valuable addition to your technical library. Please let us know what you think about this bookwere looking forward to hearing from you.

    As Groucho Marx once observed, Outside of a dog, a book is a mans best friend. Inside of a dog, its too dark to read. Whil Hentzen Hentzenwerke Publishing April 2003

  • vii

    List of Chapters

    Chapter 1: Introducing Crystal Reports 1 Chapter 2: Touring Crystal Reports 17 Chapter 3: Creating Your First Report 51 Chapter 4: Accessing Data 73 Chapter 5: Intermediate Reporting 105 Chapter 6: Advanced Reporting 133 Chapter 7: Subreports 149 Chapter 8: Using Formulas 163 Chapter 9: The RDC: Introduction, Printing, and Databases 189 Chapter 10: The RDC: Manipulating Data 225 Chapter 11: The RDC: Formatting the Report 245 Chapter 12: Previewing the Report at Runtime 283 Chapter 13: The Report Designer Control 303 Chapter 14: Exporting Reports 313 Chapter 15: Integrating COM Components 343 Chapter 16: Web Reporting 357 Chapter 17: Crystal Reports .NET 385 Chapter 18: Licensing and Distribution 401 Chapter 19: Crystal Reports Tools 417

  • ix

    Table of Contents

    Our Contract with You, The Reader iii Acknowledgements xvii About the Author xix How to Download the Files xxi Chapter 1: Introducing Crystal Reports 1

    Crystal Reports versions 2 Installing Crystal Reports 2 Whats changed in version 9 13

    Whats missing 13 Whats new 14

    Upcoming chapters 14 Summary 15

    Chapter 2: Touring Crystal Reports 17

    Report Experts 17 Data page 17 Links page 17 Fields page 18 Grouping page 18 Summaries page 18 Group Sorting page 18 Chart page 18 Record Selection page 18 Template page 18

    Blank Reports 19 The Crystal Reports desktop 19

    Menus 19 Toolbars 25 Status bar 28 Design page 28 Preview page 29

    Report Sections 30 Section Expert 30

    Formatting report objects 31 Common page 32 Border page 33 Font page 34 Paragraph page 35 Hyperlink page 36

    Resizing and moving objects 36

  • x

    Using the mouse 37 Using the keyboard 37 The Object Size and Position window 37 Guidelines 37

    Setting options 38 Application options 38 Report options 46

    The report-processing model 47 Pre-pass 1 48 Pass 1 48 Pre-pass 2 48 Pass 2 48 Pass 3 48

    Summary 48

    Chapter 3: Creating Your First Report 51 Designing the report 51

    Sketching the report 51 The report data 53

    Your first report, step-by-step 53 Selecting the data source 54 Adding data fields to the report 55 Adding a calculated field 58 Grouping and group totals 60 Formatting the report 64 Adding the page header 66 Charting made easy 68

    Summary 71 Chapter 4: Accessing Data 73

    Using the Database Expert 73 Creating a new connection 74 Selecting database objects 87 Linking tables 89

    Field definitions files 92 Creating a field definition file 92 Using a field definition file 94

    Working with images 95 Images in the database 96 Adding a watermark 96

    Limiting records selected 97 The Select Expert 98

    Miscellaneous data functions 100 Set Database Location 100 LogOn or Off Server 100 Show SQL Query 101

  • xi

    Perform Grouping on Server 102 Report Bursting Indexes 102

    Summary 103

    Chapter 5: Intermediate Reporting 105 Understanding the Crystal Repository 105

    Adding objects to the repository 106 Using repository objects in a report 107 Updating objects in the repository 107

    Sorting 108 Groups 108

    Drill-down 113 Hierarchical groups 113

    Summaries 114 Subtotals and grand totals 114 Running totals 116

    Cross-tabs 117 Charts 123

    Basic charting 124 Advanced charting 128

    Maps 129 The Data tab 129 The Type tab 130 The Text tab 131

    Summary 132 Chapter 6: Advanced Reporting 133

    Report parameters 133 Multiple-value parameters 135 Default values 137

    Report alerts 139 OLE objects 142 Embedded fields 144 Hierarchical reports 146 Summary 147

    Chapter 7: Subreports 149

    Inserting a subreport 149 Unlinked subreports 150 Linked subreports 157 On-demand subreports 159 Updating subreports 160 Summary 162

  • xii

    Chapter 8: Using Formulas 163 The Formula Editor 165

    The General toolbar 167 The Workshop toolbar 167 The Custom Function toolbar 168 The Workshop Tree 168 The Editor toolbar 169

    Understanding formulas 171 Comments 171 Data types 171 Variables 174 Fields 174 Operators 175 Functions 182

    Custom functions 183 The Formula Extractor 185

    The Formula Expert 186 Summary 187

    Chapter 9: The RDC: Introduction, Printing, and Databases 189

    Understanding the Report Design Component 190 Getting started with RDC programming 190

    Registering the runtime component 192 Working with collections 194 The Application object 195 The Report object 199

    The Database object 206 Working with tables 207

    Passing stored procedure parameters 209 The DatabaseTable object 209 The ConnectionProperties object 211 Working with the ConnectionProperties object 211 Getting connection property information 213

    Working with fields 214 Linking tables 215 Connecting to datasome examples 217

    Logging on and off a server database 217 Connecting to ADO 218 Connecting through an ODBC connection 219 Connecting to XML 220 Connecting to file-based data 220

    You connected to your data, now what? 220 Summary 223

  • xiii

    Chapter 10: The RDC: Manipulating Data 225 Sorting 225 Working with Groups 226 Summary Fields 228 Running Totals 231 SQL Expressions 234 Formula Fields 236 Parameter Fields 237 Report Alerts 241 Summary 243

    Chapter 11: The RDC: Formatting the Report 245 Working with Areas 245

    Condition Formulas 248 Working with Sections 249

    The ReportObject object 251 TextObject object 251 Field elements 253 LineObject object 257 BoxObject object 257 FieldObject object 258 Picture fields 263 Special variable fields 263 Summary fields 265 BlobFieldObject object 265 Crosstabs 267 GraphObject object 269 The FieldDefinitions collection 275 MapObject object 276 OLEObject 276 OLAPGridObject object 278 The ObjectSummaryFieldDefinitions collection 279 Unbound fields 279 Subreports 280

    Summary 282 Chapter 12: Previewing the Report at Runtime 283

    Registering the control 283 Creating a preview form 284 Methods of the Viewer Control 291

    Showing the report 291 Retrieving information 293

  • xiv

    Events 294 Report objects events 295 Drill events 297 Toolbar objects events 297 Miscellaneous events 299

    Summary 301 Chapter 13: The Report Designer Control 303

    Registering the Control 303 Creating a design form 304 Working with the designer 310

    Providing Help 311 Using the Designer Control 312 Summary 312

    Chapter 14: Exporting Reports 313

    Programmatic exports 314 Exporting to files 318

    Adobe Acrobat (PDF) 318 Microsoft Word 319 Microsoft Excel 320 Rich Text Format (RTF) 323 HTML 325 XML 328 Comma Separated Values (CSV) 332 Tab-Separated Text 334 Text 335 Report Definition 337

    Exporting to an application 338 Exporting to MAPI 338 Exporting to ODBC 339 Summary 341

    Chapter 15: Integrating COM Components 343 Formulas revisited 343 Events 344

    Report events 344 Section events 352 Separate image files 354 Report variables 355

    Summary 356

  • xv

    Chapter 16: Web Reporting 357 Report design revisited 357

    Embedded hyperlinks 357 Cascading style sheets 359 Navigation 360 Report Parts 362

    Working with ASP 364 Page rendering objects 366 Customizing the Viewer 371

    The Report Application Server 372 Customizing the RAS 379 The RAS SDK 382

    Summary 384

    Chapter 17: Crystal Reports .NET 385 Windows-based applications 385 Web-based applications 388 The Crystal Reports .NET designer 392 Connecting to ADO .NET 394 XML web services 396

    Publishing a web service 396 Consuming a web service 397

    The CrystalDecisions namespaces 398 Summary 399

    Chapter 18: Licensing and Distribution 401 Distribution 401

    RDC distribution 401 Web reporting 414 .NET distribution 414

    Licensing 414 The License Manager 415

    Summary 416

    Chapter 19: Crystal Reports Tools 417 Documentation files 417 The Crystal Decisions web site 418

    Hot fixes and service packs 419 Utilities 420 Support forums 421

    Summary 421

  • xvi

  • xvii

    Acknowledgements I remember sitting down at dinner at the hotel restaurant the night before the Great Lakes Great Database Workshop in 2000. Whil Hentzen, sitting next to me, leaned over and said So, about this book youre writing. We had discussed the possibility in passing a few times before that, but nothing concrete. That was when I started thinking I just might be able to do it. But, it still took a year before I started work on the project. Thanks, Whil, for all your efforts, and for publishing the best collection of development books available.

    Putting this book together has not been easy. I want to thank my coworkers at 3M HIS,

    Tony Curtis, Cole Gleave, and Erie Walker. Thanks to the people from the Visual FoxPro community that helped in many ways, Cathi Gero, George Tasker, Cathy Pountney, Ted Roche, Doug Dodge, Nancy Folsom, Ken Cluff, Evan Delay, Rick Schummer, and Cindy Winegarden. Im sure I left out someone, but that doesnt mean your help wasnt appreciated.

    I also need to thank Jaylene Crick and the many support people I worked with at Crystal

    Decisions. I must not forget the entire Visual FoxPro team at Microsoft. Especially Brad Peterson, Calvin Hsia, Ken Levy, and my good friend John Koziol. These guys have flat out produced the best development tool you can find. I have to thank my Tech Editor, Dan Jurden and my copy editor, Nicole McNeish.

    I cant forget Mom and Dad. Finally, I need to thank my boys, Johnathan and Jason.

    There were many hours and days that I wasnt there because of this book. Thank you for your understanding.

  • xviii

  • xix

    About the Authors

    Craig Berntson Craig has been developing custom and commercial applications for business and

    government for 20 years. He has developed applications for small business as well as large enterprises. He uses Visual FoxPro, C++, and Crystal Reports as his main development tools. He has written several articles for FoxTalk, some of which are available on the MSDN web site. He has also spoken at many software developer conferences, seminars, and user groups, including Advisor Visual FoxPro DevCon, the Great Lakes Great Database Conference, Essential Fox, and MSDN events.

    Craig is a Microsoft Certified Solution Developer and has been named a Microsoft Most

    Valuable Professional every year since 1997 for his support of the Visual FoxPro community. You can often find him online at the Universal Thread (www.universalthread.com). Craig is the President of the Salt Lake City FoxPro User Group where he frequently gives presentations on using Visual FoxPro and other current and emerging technologies. He is currently a Senior Software Engineer at 3M Health Information Systems in Salt Lake City. e-mail: [email protected], Web: www.craigberntson.com

    Dan Jurden Dan Jurden is a Senior Application Developer for EPS-Software Corp. located in Houston,

    Texas. He is a Microsoft Certified Professional. He co-authored the book Creating Visual FoxPro Applications using Visual FoxExpress with BOb Archer, published by Hentzenwerke Publishing. He has also authored articles published in CoDe Magazine and Universal Thread Magazine dealing with SQL Server and other topics. Dan has presented topics at the German DevCon, Essential Fox, SQL Server Live!, and GLGDW conferences. He has been developing Client-Server applications using SQL Server and Crystal Reports for over 6 years. Dan can be reached via email at [email protected].

  • xx

  • xxi

    How to Download the Files Hentzenwerke Publishing generally provides two sets of files to accompany its books. The first is the source code referenced throughout the text. Note that some books do not have source code; in those cases, a placeholder file is provided in lieu of the source code in order to alert you of the fact. The second is the e-book version (or versions) of the book. Depending on the book, we provide e-books in either the compiled HTML Help (.CHM) format, Adobe Acrobat (.PDF) format, or both. Heres how to get them.

    Both the source code and e-book file(s) are available for download from the Hentzenwerke Web site. In order to obtain them, follow these instructions:

    1. Point your Web browser to www.hentzenwerke.com.

    2. Look for the link that says Download

    3. A page describing the download process will appear. This page has two sections:

    Section 1: If you were issued a username/password directly from Hentzenwerke Publishing, you can enter them into this page.

    Section 2: If you did not receive a username/password from Hentzenwerke Publishing, dont worry! Just enter your e-mail alias and look for the question about your book. Note that youll need your physical book when you answer the question.

    4. A page that lists the hyperlinks for the appropriate downloads will appear.

    Note that the e-book file(s) are covered by the same copyright laws as the printed book. Reproduction and/or distribution of these files is against the law. If you have questions or problems, the fastest way to get a response is to e-mail us at [email protected].

  • Chapter 1: Introducing Crystal Reports 1

    Chapter 1 Introducing Crystal Reports

    What is Crystal Reports? Who should use it? Why should you use it? When should Crystal Reports be used? How do you use it? These are some of the questions that will be answered in this book.

    You have probably heard of Crystal Reports. In fact, you probably use it. Why else would you buy this book? I assume you are also a software developer looking for more information on how to use Crystal Reports in your application. After all, the word develop is in the title of this book. Youre about to learn that Crystal Reports is a very powerful and flexible reporting tool. However, before digging into the nitty gritty, I will address a few basic questions in case you are new to Crystal Reports or are investigating it for use in your application.

    Many years ago, I took a high school journalism class. I dont remember much from the class, but I do remember one important thing: all good stories answer six questions; who, what, when, where, why, and how. Even though Im not a journalist today, I think these questions are important to address. I will start with the what.

    What is Crystal Reports? Simply put, it is a reporting application that creates sophisticated reports from a company called Crystal Decisions. It can pull data from many different data sources and be used stand-alone or embedded in an application.

    Who should use Crystal Reports? Thats pretty easy. I use Crystal Reports for all my reporting and I think you should too. End users can also use Crystal Reports to pull data from your application, databases, operating system logs, and many other data sources. So, Crystal Reports is for both the developer and end user.

    Why should you use Crystal Reports? Again, a question with an easy answer. Im not the type of author who learns a product, writes a book about it, and then moves on to the next product. In fact, my development tool, Visual FoxPro, includes a good built-in reporting tool, but I use Crystal Reports because it gives the user a lot of power in their reporting needs. You will soon see how easy it is to add grouping, graphs, and drill down to reports.

    When should you use Crystal Reports? As I said earlier, I use Crystal Reports exclusively for reports in applications I develop. I could use the Visual FoxPro report designer for simple reports and Crystal Reports for more sophisticated needs, but by using only one reporting tool, my users have a consistent look and feel across all reports.

    Where should you use Crystal Reports? Again, in every application you develop. Finally, how do you use Crystal Reports? Well, thats the purpose of this book. You will

    see how to seamlessly embed Crystal Reports into your applications.

  • 2 CrysDev: A Developers Guide to Integrating Crystal Reports

    Crystal Reports versions As I was writing this book, Crystal Reports 9 was released. Some chapters were updated to include new material; others were checked to make sure they were still correct for this release. You should be aware that four editions are available:

    Standardprovides basic reporting capabilities for accessing the most common databases.

    Professionaladds additional database support such as OLAP and XML plus additional report design features like custom templates, repository, and more.

    Developerthe base developers version. Gives you report design and runtime distribution capabilities. You also get a license for up to five concurrent users for runtimes and development testing.

    Advancedadds advanced development and deployment capabilities. Report queuing and caching as well as report creation and modification SDKs.

    If you are developing applications, you should be using either the Developer or the Advanced edition, because they are the only ones that give you both the distributable controls and a license to ship them with your application. If you purchase one of the other versions, an upgrade is available from your reseller or directly from Crystal Decisions.

    Installing Crystal Reports Installing Crystal Reports is a simple process. The following steps walk you through the installation:

    1. Insert the Crystal Reports CD into your CR ROM drive. If you have enabled Autostart, the startup window (see Figure 1) displays.

  • Chapter 1: Introducing Crystal Reports 3

    Figure 1. The startup window displays when you insert the Crystal Reports CD.

    There are several options available on the left side of the startup screen:

    Release Notesdisplays information on installation and system requirements. Technical Resourceslaunches your web browser and navigates to a Crystal

    Decisions web site where you can get more information about technical resources such as training, technical support, white papers, etc.

    Servicesnavigates to a site that provides an overview of training, consulting, and certification resources available from Crystal Decisions.

    Web Reportingdisplays a web site that discusses Crystal Enterprise, a tool for enterprise reporting needs.

    Crystal Decisions Productsprovides an overview of other products from Crystal Decisions, such as Crystal Enterprise, Crystal Analysis, and several analytical applications.

    2. In the lower right corner of the start up window is the install button. Click Install Crystal Reports 9 to launch the installation wizard. The Welcome to the Installation Wizard (see Figure 2) displays.

  • 4 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 2. The welcome page of the Installation Wizard starts you on your way to installing Crystal Reports.

    3. Click Next. The license agreement displays (see Figure 3).

  • Chapter 1: Introducing Crystal Reports 5

    Figure 3. You must accept the License Agreement to proceed with installation.

    4. If you do not agree with the license agreement, just click Next and the Installation Wizard will abort. If you agree with the license, select I accept the License Agreement, and then click Next to display the User Information page (see Figure 4).

  • 6 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 4. Enter your personal information in the User Information page.

    5. Enter your name, organization (if you have one), and your CD Key Code from the back of your CD sleeve. Click Next to display the Installation Type page (see Figure 5.)

  • Chapter 1: Introducing Crystal Reports 7

    Figure 5. The Select Installation Type page allows you to select a Typical, Complete, or Custom installation and choose where you want to install Crystal Reports.

    6. I recommend you choose the Custom installation. This allows you to choose which parts of Crystal Reports you want to install such as maps and database drivers. You should also enter the installation path if you dont want to use the default. Once you do this, click Next to display the Select Features dialog box (see Figure 6).

  • 8 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 6. The Select Features page allows you customize the components and features you want to install.

    7. I made some changes to my installation. For example, I made changes to the Data Access option, selecting only the data sources Im interested in using. I also selected all options under Export Support and Geographic Mapping. Once you customize your installation, click Next. The Start Installation page displays (see Figure 7).

  • Chapter 1: Introducing Crystal Reports 9

    Figure 7. The Start Installation dialog box gives you one last chance to confirm your choices before actually starting the installation.

    8. Click Next to begin the installation. The installation progress page displays (see Figure 8).

  • 10 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 8. The installation progress displays once you actually begin the installation.

    9. Once the installation is complete, the Registration Wizard launches. (see Figure 9). Click Next to begin the registration process.

  • Chapter 1: Introducing Crystal Reports 11

    Figure 9. The Registration Wizard walks you through the Crystal Reports registration process.

    10. If you have a registration number, enter it in the Registration Options dialog box (see Figure 10). If you dont have one, you can register your copy of Crystal Reports and obtain a number. I wrote the registration number on my Crystal Reports CD sleeve. That way I have it if I need to reinstall the application. If you dont want to register Crystal Reports at this time, click Register Later.

  • 12 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 10. You will be asked to register your copy of Crystal Reports as part of the installation process.

    11. If you register Crystal Reports or enter the registration number, press Finish to complete the installation process. If you register Crystal Reports, you get a confirmation showing the registration was successful (see Figure 11).

    Figure 11. The registration confirmation displays when you successfully register your copy of Crystal Reports.

  • Chapter 1: Introducing Crystal Reports 13

    12. Your installation is now complete (see Figure 12). Click Finish to close the Installation Wizard.

    Figure 12. The setup dialog displays when you complete the installation.

    Whats changed in version 9? Crystal Reports 9 includes some exciting new features for developers. You will also discover some features found in previous versions are now missing.

    Whats missing? Crystal Decisions removed several developer features in this new release, but dont worry too much about missing functionality. Many of the same functions are implemented in a different way. Some of the removed capabilities include:

    Compiled reports Crystal Dictionary and Crystal SQL Designer Export to Data Interchange Format (DIF), Lotus 123, and Multi-Byte Character Sets

    (MBCS)

  • 14 CrysDev: A Developers Guide to Integrating Crystal Reports

    Developer APIs Cpeaut32.DLL and Crystl32.OCX Database drivers for bound database, Centura SQLBase (Gupta), Essbase, and native

    Microsoft SQL Server

    Whats new? Crystal Reports 9 has many exciting new features for developers, including:

    Enhanced designer. I found report creation to be much easier with Crystal Reports 9. SQL commands. These replace and enhance Crystal SQL Designer. Unicode support. Crystal repository. Makes it easy to reuse report objects. This is a replacement for the

    Crystal Dictionary.

    Custom templates. Its now easy to create and reuse templates, thus giving a common look to all your reports.

    Report parts. Makes is easy to deploy reports to hand-held devices Custom functions. You can now use the Crystal Reports formula languages to create

    your own functions.

    Formula Workshop. An enhancement of the old Formula Expert Side-by-side installation. Different versions of Crystal Reports can now coexist on

    the came computer.

    Improved database support. In-place chart editing. You no longer need to launch the Chart Analyzer to customize

    a chart.

    Improved Report Design Control (RDC) for application integration. Report Application Server (RAS) for web reporting and distribution. Enhanced formulas, experts, cross tabs, and wizards.

    Upcoming chapters As this book is about using Crystal Reports in application development, the rest of the book is devoted to helping you understand how to use Crystal Reports and embed it in your applications.

    In Part 1, you learn how to design a report. In Chapter 2, Touring Crystal Reports, I discuss the Crystal Reports designer and how to set various options to maximize your productivity. Chapter 3, Creating Your First Report, Step-by-Step, walks you through creating a report with drill-down and graphing. Chapter 4, Accessing Data, presents a

  • Chapter 1: Introducing Crystal Reports 15

    discussion on accessing the most common types of data. In Chapter 5, Intermediate Reporting, I show how to sort and group and add graphs, maps, and cross tabs to a report. Chapter 6, Advanced Reporting discusses report parameters, alerts, and embedded OLE objects. Chapter 7, Subreports, covers using subreports. Crystal Reports also has its own programming language. Chapter 8, Using Formulas, discusses this topic in detail.

    Part 2, moves into programming reports and shows you how to embed Crystal Reports in your application. Programmatically controlling Crystal Reports begins in Chapter 9, The RDC: Introduction, Printing, and Databases, where I first present the Report Design Control. The RDC is a big topic and continues in Chapter 10, The RDC: Manipulating Data; Chapter 11, The RDC: Formatting the Report; Chapter 12, Previewing the Report at Runtime; and Chapter 13, The Runtime Design Component.

    Chapter 14, Exporting Reports, explains the different export options and some gotchas to watch out for. Chapter 15, Integrating COM Components with Crystal Reports, shows you how you to call COM components from inside your application. It also discusses hooking into Crystal Reports events.

    Chapter 16, Crystal Reports .NET, discusses Microsofts Visual Studio .NET, which ships with a special version of Crystal Reports. Chapter 17, Web Reporting, moves on to Internet concepts and how to put your report on the web. Chapter 18, Distribution and Licensing, discusses the issues of shipping your reporting solution and additional licensing requirements you may face. Finally, Chapter 19, Crystal Reports Tools, discusses tools that help you with report design and application development.

    Summary This chapter showed you how to install Crystal Reports and gave an overview of the rest of the book.

    Updates and corrections to this chapter can be found on Hentzenwerkes web site, www.hentzenwerke.com. Click on Catalog and navigate to the page for this book.

  • 16 CrysDev: A Developers Guide to Integrating Crystal Reports

  • Chapter 2: Touring Crystal Reports 17

    Chapter 2 Touring Crystal Reports

    Crystal Reports has many features to help you easily create powerful and full-featured reports. This chapter takes you on a tour of the Crystal Reports design environment.

    Users have come to expect software to make things easier for them. Toolbars, wizards, menus, and other application tools help you with using the application. Crystal Reports is no different.

    When you first launch Crystal Reports or create a new report by selecting File | New from the menu or clicking the New button on the standard toolbar, the Crystal Report Gallery dialog box displays. Using the Crystal Report Gallery, you can open an existing report, create a new report using one of the report wizards, called Experts, or create a blank report.

    Report Experts The Crystal Report Gallery shows four different report Experts:

    Standard Cross-Tab Mail Label OLAP These Experts walk you through a series of steps to select the data and format the report.

    The exact steps depend on the type of report you select. Most of the reports you create using the Report Expert will be standard reports. The Standard Expert is the one I have chosen to discuss here. Once you choose an Expert, click OK.

    Data page The Data page is where you select the tables to include in the report. You can select an existing data connection, select items from the Repository, or create a new connection. The Repository is discussed in Chapter 5, Intermediate Reporting. Click Database, to display the Data Explorer. You will briefly see the data page in Chapter 3, Creating Your First Report and it is discussed more thoroughly in Chapter 4, Accessing Data. Once you select the data tables, click Close.

    Links page If you choose multiple tables, Crystal Reports prompts you to link the tables using keys. Crystal Reports initially joins the tables of the first fields that exist in both tables. To change the link, click the link line and delete it, then drag the key field from the parent table and put it on the matching field in the child table. See chapter 4, Accessing Data, for more about Linking.

  • 18 CrysDev: A Developers Guide to Integrating Crystal Reports

    Fields page Once you link the tables, use the Fields page to select the fields to appear in the Detail section on the report. You can use drag-and-drop to move a field from one list to the other or select the field and click Add, Add All, Remove, or Remove All.

    There are several options on this page. The Browse Data option displays the data for the selected field. Find locates a specific field in the Available Fields list. This is useful when you have many fields available.

    Grouping page The Grouping page is where you add any grouping to the report. Use Groups to organize data in a logical way and provide totals for the group. You do not have to group fields selected on the Fields page.

    You can order each field you select for grouping in ascending or descending order. Groups are discussed in detail in Chapter 5, Intermediate Reporting.

    Summaries page Groups are commonly used to generate a subtotal. That is the purpose of the Summary page. For each field you select for a group, the Total page allows you to select the field to subtotal and the type of calculation. For example, in a numeric field you can generate a sum, average, minimum, maximum, and several other summaries. You can also create a percentage of the grand total and optionally add grand totals to the report. More about Summaries can be found in Chapter 5, Intermediate Reporting.

    Group Sorting page When you select a Top or Bottom N, the groups matching your criteria are sorted at the beginning of the report. One example of this powerful feature is being able to see the top salespeople or territories or the top selling brands or products.

    Chart page Use the Chart page to add a chart to the report. I discuss charts in more detail in Chapter 5, Intermediate Reporting.

    Record Selection page You can select a subset of the data by using the Select page. This page equates to the Where clause of a SQL Select statement. You may only want to see data for a particular state or date range. This page is where you can set up this option.

    Template page Finally, Use the Template page to assign a preset design template to a report. When you select one of the templates, formatting is applied to the report objects. Crystal Reports 9 allows you to add your own custom templates. Chapter 5, Intermediate Reporting, discusses this further.

  • Chapter 2: Touring Crystal Reports 19

    Blank reports The Crystal Report Gallery also has an option to create a blank report. When you select this option, you have to create the report from scratch and do all of the formatting yourself. Selecting As a blank report causes the Database Expert to display.

    The Crystal Reports desktop When you launch Crystal Reports, you see the application divided into different areas: menus, toolbars, Field Explorer, Repository Explorer, Report Explorer, design surface, and status bar. The design surface further divides into the Design and Preview areas.

    Menus The Crystal Reports menus make available most of the functionality of the product. Some features are only available from a context menu or from a dialog box. The menu options are:

    File

    New Creates a new report. Open Displays a File Open window to open an existing report. Close Closes the current report. Save Saves the current report. Save As Saves the current report using a different file name. Save Data with Report If selected, the data used for a report is saved in the .rpt file.

    I recommend you turn this option off.

    Save Subreport As Saves the current subreport as a different file. Print Preview Previews the current report. Print (Preview Sample, Printer) Prints or exports the current report. Printer Setup Displays a Printer Setup window. Page Setup Displays a Page Setup window used to change the page margins. Export Displays the Export window. Send To (Mail Recipient, Exchange Folder) Allows you to send a report via email. Options Displays the Crystal Reports Options window (see Setting options

    later in this chapter).

    Report Options Sets options for the current report (see Setting options later in this chapter).

  • 20 CrysDev: A Developers Guide to Integrating Crystal Reports

    Summary Info Displays the Document Properties window to enter the author name, a report description, and other information.

    MRU The Most Recently Used list of reports. Exit Exits Crystal Reports.

    Edit

    Undo Undoes the latest changes. Redo Reapplies the latest undo. Cut Cuts the selected object and places it on the clipboard. Copy Copies the selected object and places it on the clipboard. Paste Pastes the object on the clipboard into a report. Paste Special Pastes an OLE object on the clipboard into the report and creates a

    link back to the owning application.

    Delete Deletes the currently selected object. Select All Selects all the objects in a report. Find Displays the Find window to locate a particular item in a report. Go To Page Displays the Go To Page dialog box. You can go to a specified page

    or the first or last page in the preview.

    Edit Report Object Displays a window for editing selected report object. Subreport Links Displays the Subreport Links window to link the parent report

    to the subreport.

    Browse Field Data Displays a window showing data from a selected field. Object This menu item changes to match the selected object. Choose it to edit the

    current object.

    Links Use to make changes to OLE links.

    View

    Design Sets the current view to the designer. Preview Sets Preview as the current view. This option is only available when the

    preview is selected.

    Other Views Lists any other views or drill-downs you have displayed. This option is only available when the view exists.

  • Chapter 2: Touring Crystal Reports 21

    Close Current View Closes the current active view. You cannot close the Design view.

    Field Explorer Toggles the display of the Field Explorer. Report Explorer Toggles the display of the Report Explorer. Repository Explorer Toggles the display of the Repository Explorer. Toolbars Displays the Toolbars window for selecting the toolbars to display and

    setting options for them.

    Status Bar If selected, the status bar displays. Group Tree When selected, the group tree displays when in preview mode. Zoom Displays the Magnification Factor window to increase or decrease the

    view of the report.

    Rulers (Design, Preview) Toggles the display of rulers in Design and Preview views.

    Guidelines (Design, Preview) Toggles display of guidelines in Design and Preview views.

    Grid (Design, Preview) Toggles display of the grid in Design and Preview views. Tooltips (Design, Preview) Toggles display of tooltips in Design and Preview

    views.

    Insert

    Text Object Inserts a text object into a report. Summary Inserts a summary object into a report. Field Heading Inserts a new field heading for the selected field in the Designer.

    This option is only available for fields that do not currently have a heading.

    Group Inserts a new group into a report. OLAP Grid Inserts an OLAP cube into a report. Cross-Tab Inserts a cross-tab into a report. Subreport Inserts a subreport into a report. Line Inserts a line object into a report. Box Inserts a box into a report. Picture Inserts a picture into a report. Chart Displays the Chart Expert to insert a chart into a report.

  • 22 CrysDev: A Developers Guide to Integrating Crystal Reports

    Map Display the Map Expert to insert a map into a report. OLE Object Inserts an OLE object into a report. Template Field Object Inserts a template field into a report. Template fields are

    useful for creating generic reports because they do not bind to any table or field. Chapter 6, Advanced Reporting, covers more about Template fields.

    Format

    Format Field Displays the Format Editor window for the currently selected report object.

    Hyperlink Displays the Hyperlink page of the Format Editor window for the currently selected report object.

    Use Expert This option changes based on the object you have selected. If an Expert is available for the object, this option will launch it.

    Highlighting Expert Displays the Highlighting Expert window. The Highlighting Expert is discussed in Chapter 5, Intermediate Reporting.

    Line Height -- Allows you to set the measurements for how text is placed between two lines on a report. This option is only available when you are not using free-form placement.

    Text Formatting -- Displays the Text Format dialog box to format selected text. Report Style Expert Displays the Report Style Expert to format a report according to

    a predefined style.

    Move (Backward, To Back, Forward, To Front) Changes the location of a selected item along the z-axis.

    Align (Tops, Middles, Bottoms, Baseline, Lefts, Centers, Rights, To Grid) Aligns the selected objects with the last item selected.

    Size (Same Height, Same Width, Same Size) Sets the height and/or width of selected objects to that of the object you selected last.

    Size and Position Displays the Object Size and Position window to set the size and location of a selected object.

    Pivot OLAP Grid Rotates an OLAP grid. Pivot Cross-Tab Rotates a cross-tab.

  • Chapter 2: Touring Crystal Reports 23

    Database

    Database Expert -- Displays the Database Expert. Set Datasource Location Sets the location of a table. Log On or Off Server Use to log on or off the database or server. Browse Data Displays a browse window containing data for a selected field. Set OLAP Cube Location Sets the location of an OLAP cube. Verify Database Checks that the database is in the specified location and its

    structure has not changed.

    Show SQL Query Displays the SQL SELECT statement used to gather the data. Perform Grouping on Server Instructs Crystal Reports to do a GROUP BY on the

    server instead of locally.

    Select Distinct Records Adds a DISCTINCT clause to the SQL SELECT command.

    Report Select Expert Displays the Select Expert to modify the SQL SELECT statement. Selection Formulas (Record, Group) Displays the Formula Workshop to enter

    selection criteria formulas. See Chapter 8, Using Formulas.

    Formula Workshop Displays the Formula Workshop. Alerts (Create or Modify Alerts, Triggered Alerts) Creates or modifies an existing

    alert or displays any triggered alerts. Alerts are discussed in Chapter 6, Advanced Reporting.

    Report Bursting Indexes Displays the Saved Data Indexes window. These are indexes used with saved data to increase the reporting speed.

    Section Expert Displays the Section Expert. Group Expert Displays the Group Expert. Group Sort Expert Displays the Group Sort Expert. Record Sort Expert Displays the Record Sort Expert. XML Expert Displays the XML Expert. Template Expert Displays the Template Expert. OLAP Report Settings Displays the OLAP Report settings window. This option is

    only available when you are using an OLAP cube.

  • 24 CrysDev: A Developers Guide to Integrating Crystal Reports

    Hierarchical Grouping Options Displays the Hierarchical Options window. Refresh Report Data Reloads the report data. Set Print Date/Time Allows you to specify the print date and time. Performance Information Displays the Performance Information window.

    Window

    Tile Vertically Vertically tiles all open reports. Tile Horizontally Horizontally tiles all open reports. Cascade Cascades all open reports. Arrange Icons Arranges all minimized report windows at the bottom of the Crystal

    Reports window.

    Close All Closes all open reports. Open Windows List Lists each open report window.

    Help

    Crystal Reports Help Opens the main Crystal Reports Help file. Context Help Activates Whats This? Help. Welcome Dialog Displays the Welcome to Crystal Reports window. Additional Help Files Lists additional Help files available on the Crystal

    Reports CD.

    Crystal Decisions On the Web Provides several links to different Crystal Decisions web sites.

    Web Report Samples Lists web report sample files. Register / Change Address Displays the Software Registration Wizard. About Crystal Reports Displays the About window.

  • Chapter 2: Touring Crystal Reports 25

    Toolbars Crystal Reports has four toolbars: Standard, Formatting, Insert Tools, and Expert Tools. You can float each toolbar or dock to the top, left, bottom, or right edges of the screen. In addition, select View | Toolbars from the menu to display the Toolbars dialog box. This is where you show or hide each toolbar and select using large buttons and tooltips.

    The Standard toolbar The Standard toolbar (see Figure 1) contains most of the functionality you will use when designing reports. The controls on this toolbar, from left to right, are:

    Figure 1. The Standard Toolbar contains most of the functions you need.

    New Creates a new report. Open Displays a file open dialog to open an existing report. Save Saves the current report. Print Prints the current report. Print Preview Previews the current report. Export Exports the current report. Refresh Reloads the report data. Cut Cuts the selected object and places it on the clipboard. Copy Copies the selected object and places it on the clipboard. Paste Pastes the object on the clipboard into the report. Undo Undoes the latest changes. Redo Reapplies the latest undo. Toggle Group Tree Toggles the display of the Group Tree when previewing. Field Explorer Toggles the display of the Field Explorer. Report Explorer Toggles the display of the Report Explorer. Repository Explorer Toggles the display of the Repository Explorer. Find Finds the specified report object or data item in preview. Zoom Control Specifies the zoom percentage. Help Toggles Whats This? Help.

  • 26 CrysDev: A Developers Guide to Integrating Crystal Reports

    The Formatting toolbar The Formatting toolbar (see Figure 2) is used to set formatting for a selected object. The controls on this toolbar, from left to right, are:

    Figure 2. Use the Formatting toolbar to apply formatting to various report objects.

    Font Face Changes the font for the selected object. Font Size Changes the font size of the selected object. Increase Font Size Increases the font size of the selected object. Decrease Font Size Decreases the font size of the selected object. Bold Toggles bold font for the selected object. Italics Toggles italic font for the selected object. Underline Toggles underline for the selected object. Align Left Aligns the text of the object with the left-hand border of the object. Align Center Aligns the text of the object in the horizontal center of the object. Align Right Aligns the text of the object with the right-hand border of the object. Justify Justifies the text of the selected object. Font Color Displays a color picker to change the text color of the selected object. Outside Borders Allows you to set any borders you want on the selected object. Suppress Suppresses the selected object. Lock Format Toggles the format lock for the current object. When the lock is

    active, you cannot change the formatting of the object.

    Lock Size/Position Toggles the size and position lock for the current object. When the lock is active, you cannot change the size or position of the object.

    Currency Toggles printing of the currency symbol for the selected object. Thousands Toggles printing of the thousands separator for the selected object. Percent Toggles printing the percent sign for the selected object. Increase Decimals Increases the number decimal positions for the selected object. Decrease Decimals Decreases the number of decimal positions for the

    selected object.

  • Chapter 2: Touring Crystal Reports 27

    The Insert Tools toolbar The Insert Tools toolbar (see Figure 3) contains functionality that lets you quickly insert a new object into the report. The buttons on the toolbar, from left to right, are:

    Figure 3. Use the Insert Tools toolbar to insert new objects.

    Insert Text Object Adds a new text object to the report. Insert Group Adds a new group to the report. Insert Summary Adds a new summary to the report. Insert Cross-tab Inserts a new cross-tab object. Insert OLAP Grid Adds an OLAP grid to the report. Insert Subreport Inserts a subreport. Insert Line Inserts a line. Insert Box Adds a box to the report. Insert Picture Adds a new picture. Insert Chart Adds a chart to the current report. Insert Map Inserts a map.

    The Expert Tools toolbar The Expert Tools toolbar (see Figure 4) allows you to quickly display a specific Expert. The buttons on this toolbar, from left to right, are:

    Figure 4. The Expert Tools toolbar.

    Database Expert Displays the Database Expert. Group Expert Displays the Group Expert. Group Sort Expert Displays the Group Sort Expert. Record Sort Expert Displays the Record Sort Expert.

  • 28 CrysDev: A Developers Guide to Integrating Crystal Reports

    Select Expert Displays the Select Expert. Section Expert Displays the Section Expert. Formula Workshop Displays the Formula Workshop. OLAP Report Settings Displays the OLAP grid settings dialog. Template Expert Displays the Template Expert. Format Displays the Format dialog for the currently selected object. Insert Hyperlink Displays the Hyperlink page of the Format dialog for the currently

    selected object.

    Highlighting Displays the Highlighting Expert for the currently selected report object.

    Status bar The status bar displays (see Figure 5) at the bottom of the Crystal Reports window. You toggle the display of the status bar by selecting View | Status Bar from the menu. The status bar provides useful information. At the left is detailed information about the selected object or the highlighted menu item.

    Figure 5. The Status bar provides information about the currently selected object.

    Next it displays the location and size of the selected item. For example, you may see something like 0.2, 0.9 : 3.3 x 0.2. This tells you the current object is at 0.2 inches on the x-axis and 0.9 inches on the y-axis in the current section. The next set of numbers tells you how big the object is, 3.3 inches horizontally by 0.2 inches vertically.

    The last two pieces of information only show up when previewing the report. First is the number of records processed and finally the percent of record processing that is complete. If you have a large data set to report or if the SELECT statement takes a long time to complete, you will see these numbers change as the data processes.

    Design page The Design page (see Figure 6) is where you do all of your work. On the left side is the section identification area. This shows you the name of each section. You can right-click this area to display a shortcut menu with different options for the sections.

    The rulers are at the top and left of the actual design area. Select View | Rulers from the menu to toggle the display of the rulers.

    The rest of the design page is the actual report. You place different report objects in the specific sections to create the report.

    The controls at the top, right are not enabled in the Designer.

  • Chapter 2: Touring Crystal Reports 29

    Figure 6. The Designer is where you do all of your work.

    Preview page Select File | Print Preview or click the Print Preview button on the Standard toolbar to display the Preview page (see Figure 7). This is a fairly accurate display of what the printed report will look like. You can also do some report design on the Preview page.

    At the far left of the Preview is the Group Tree. You click one of the listed items to jump directly to that item in the preview. Select View | Group Tree from the menu to toggle the display of the Group Tree.

    Next is the section identification area. Just like on the Design tab, right-click to display a shortcut menu for changing section information.

    Rulers are at the immediate left and top of the actual preview area. You toggle the display of the rulers by selecting View | Rulers from the menu.

    The controls at the top, right are available in Preview mode. First is the print date and time, followed by the navigation controls to move from page to page in the report. The current page and total number of pages also displays.

  • 30 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 7. The Preview shows what the report will look like when printed. On the left is the Group Tree you can use to navigate quickly to a specific group.

    Report Sections Crystal Reports divides the report into several areas: report header, page header, group header, detail, group footer, page footer, and report footer. Each report is limited to one report header, page header, detail, page footer, and report footer area. You can have multiple group header and footer areas.

    Each area further divides into sections. For example, you can have multiple detail sections or multiple page header sections. When you place fields, text, and other objects on the report, you place them in a section. You can format and control each section individually.

    Section Expert You use the Section Expert to change settings for a particular section. For example, you can change the position of a particular section, set its background color, or programmatically control how different items work.

    Throughout the rest of this chapter, you will see several dialog boxes. I will not explain each option, but only discuss some of the more interesting or confusing items. The first dialog box is the Section Expert (see Figure 8).

  • Chapter 2: Touring Crystal Reports 31

    Figure 8. You use the Section Expert to customize a particular report section.

    The first item of interest on the Section Expert is the formula button. It looks like an equation with a pencil. When you click a formula button, the Formula Editor displays for you to enter a programmatic expression to control a particular feature. Formulas are discussed in detail in Chapter 8, Using Formulas. Once you enter a formula behind a specific button, the color of the equation text on the button changes from blue to red and the pencil stands at an angle, instead of lying down. You will see the formula buttons in several dialog boxes.

    The Hide (Drill-Down OK) option suppresses the section from displaying in the Group Tree. However, you can still double-click a group on the Preview page to drill down to the detail for the particular item. To hide a section completely in the report, select the Suppress (No Drill-Down) option.

    Widow/orphan control is accomplished with the Keep Together option. When you select this option, Crystal Reports attempts to print the entire section on one page.

    Choosing Underlay Following Sections will cause the selected section to print behind sections that follow it. This is useful for adding watermarks to a report. Simply add a new section near the top of the report, place the graphic in the section, and then select this option.

    Formatting report objects Crystal Reports provides many formatting capabilities. The easiest way to format an object is using the Formatting toolbar. However, this toolbar only offers a subset of the format capabilities. Use the Format Editor to access all the formatting options. There are several ways to display the Format Editor:

  • 32 CrysDev: A Developers Guide to Integrating Crystal Reports

    Right-click an object and select Format Field from the shortcut menu. Select Format | Format Field from the menu. Click the Format button on the Supplementary toolbar. Note that the pages on the Format Editor change depending on the object type and the

    data type of the field. The example shown here is for a regular field containing character data.

    Common page The Common page (see Figure 9) has settings for adjusting the position and size of an object. Check Suppress to hide the object so it doesnt print. To enable widow and orphan control, select Keep Together. You can also rotate the object either 90 or 270 degrees.

    Figure 9. Use the Common page of the Format Editor to specify the location and printing of a particular object.

  • Chapter 2: Touring Crystal Reports 33

    Border page The Border page (see Figure 10) is used to set the borders and background color of the object. The border on each side of an object can be set and you can use a different line type on each. If you set the bottom border, the entire width of the object appears underlined, as opposed to selecting the underline attribute of the font, which only underlines the actual text in the object.

    Use the Tight Horizontal setting to change the length of the horizontal border. If not selected, the length of the horizontal border is the same for each item; in other words, the length of the actual report object. If you select Tight Horizontal, the length of the border changes for each record and is the same length as the actual text.

    Figure 10. Use the Border page of the Format Editor to change border settings for a selected object.

  • 34 CrysDev: A Developers Guide to Integrating Crystal Reports

    Font page The Font page (see Figure 11) is used to set the font for an object. The only item of interest is Character Spacing Exactly. When you select this option, the width of each character is the number of points you specify. The actual width of the printed font does not change, but rather the spacing between characters changes so the space used by each character is exactly what you specify. For example, if you use a 12-point font and set character spacing to 14 points, each character will be 12 points high and 14 points wide.

    Figure 11. You can specify the font settings of a selected object on the Font page of the Format Editor.

  • Chapter 2: Touring Crystal Reports 35

    Paragraph page Paragraph formatting affects the way text prints inside the report object. There are two items of interest on the Paragraph page (see Figure 12). The first is Line Spacing. You can set the line spacing as an exact number of points or as a multiple of the font size.

    The other interesting item is Text Interpretation. There are three settings; none, RTF Text, and HTML Text. This setting tells Crystal Reports how to interpret data saved in a character or memo field. If you set the option as HTML Text, Crystal Reports interprets the data as HTML. This adds powerful formatting options to the report.

    Figure 12. The Paragraph Formatting page of the Format Editor is where you change settings for how text prints inside a selected object.

  • 36 CrysDev: A Developers Guide to Integrating Crystal Reports

    Hyperlink page Finally, the Hyperlink page (see Figure 13) tells Crystal Reports an object is a hyperlink. When the user clicks the object in the Preview, the hyperlink launches.

    Figure 13. You create hyperlinks in the report from the Hyperlink page of the Format Editor.

    Resizing and moving objects When you drop report objects onto the designer, its quite rare when the object ends up in the exact spot or the exact size you want it. This requires you move and resize the object. You can do this with the mouse, keyboard, the Object Size and Positioning dialog box, or by using guidelines.

  • Chapter 2: Touring Crystal Reports 37

    Using the mouse The mouse is the most obvious way to change the location or size of an object. Simply drag the object to the desired location to move it or grab the handles on the sides of the selected object to resize it.

    Using the keyboard The cursor keys on the keyboard provide another method to move or resize an object. Simply press a cursor key and the object moves in the desired direction. Use the Shift key in combination with the cursor key to resize the object.

    The Object Size and Position window The Object Size and Position (see Figure 14) dialog box provides precise location and size abilities. Select Format | Size and Position from the menu to display the dialog box. The X-axis position is relative to the left margin of the report. The Y-axis position is relative to the top of the section.

    Figure 14. The Object Size and Position dialog box allows precise placement and sizing of a report object.

    Guidelines Guidelines let you align the border of multiple objects and change them all at the same time. Small triangles (see Figure 15) on the rulers indicate the location of the guidelines. Select View | Guidelines Design or View | Guidelines from the menu in Preview view to toggle display of guidelines as dashed horizontal and vertical lines on the Design and Preview pages.

    When you lock the border of an object to a particular guideline, the border moves when you move the guideline. To add a new guideline, click the ruler where you want the guideline to be. You may have to move or resize the report object to get it to lock to the guideline. To remove a guideline, click the triangle and drag it off the ruler.

  • 38 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 15. Use guidelines to align and move report objects.

    Setting options Crystal Reports offers many options that affect how the software functions or affect the current report. Application options affect each report you design while report options affect only the current report.

    Application options Application options affect how Crystal Reports functions or looks. Select File | Options from the menu to display the Options dialog box, where you set these options. In my opinion, the settings on the dialog box are not well organized. For example, there are several pages where you can change settings that affect how Crystal Reports works with data. Instead of centralizing these settings, they are spread across several pages.

    Layout page The first page of the Options dialog box is the Layout page (see Figure 16). The first interesting option is Insert Detail Field Headings. When you select this option, column headings are placed in the page header when you drop a field onto the details section of the report.

    Free-Form Placement means when you place an object on the report, it does not have to be placed in a grid.

  • Chapter 2: Touring Crystal Reports 39

    Figure 16. Use the Layout page of the Options dialog box to specify default settings for Crystal Reports.

    Database page The Database page (see Figure 17) affects how Crystal Reports connects to databases and shows database objects.

    The Use Indexes or Server for Speed option tells Crystal Reports to use any index files that are associated with the database. Checking this option causes the record selection to perform faster.

  • 40 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 17. Use the Database page of the Options dialog box to change settings for how Crystal Reports interacts with different databases.

    When selected, the Perform Query Asynchronously option tells Crystal Reports to start loading data into the report before the query completes. This allows you to start producing the report more quickly when a large data set is returned. It also lets you cancel an executing query.

  • Chapter 2: Touring Crystal Reports 41

    Editors page There isnt much of interest on the Editors page (see Figure 18). The settings on this page affect the visual appearance of different items in the Formula Editor.

    Figure 18. You change editor settings on the Editors page of the Options dialog box.

    Data Source Defaults page One of the settings you can change on the Data Source Defaults page (see Figure 19) is the type of native access database files Crystal Reports displays by default. You can set the directory location and the file extensions for both the data and index files.

  • 42 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 19. The Data Source Defaults page of the Options dialog box is where you change settings for native data types and dictionary locations.

    Reporting Several options are available on the Reporting page (see Figure 20). The most important of these is Save Data With Report. When selected, any data used to design the report saves with the report. This makes the RPT file bigger and requires you to refresh the data each time you run the report.

    Update Connected Repository Objects When Loading Reports causes any Repository objects used in the report to be updated with the latest object stored in the Repository. The Repository is a new feature in Crystal Reports 9 and is discussed in Chapter 5, Intermediate Reporting.

  • Chapter 2: Touring Crystal Reports 43

    Figure 20. Use the Reporting page of the Options dialog box to change default settings for converting data and saving a report file.

    You can enter Crystal Reports formulas in Crystal or BASIC syntax. You can change the default syntax to use with the Formula Language setting. Formulas are covered in detail in Chapter 8, Using Formulas.

    Fields There isnt much to say about the Fields page (see Figure 21). When you click one of the buttons, the Format Editor for the particular data type displays.

  • 44 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 21. Selecting a Field format item from the Fields page of the Options dialog box displays the appropriate page of the Format Editor. Use this to change default settings for how a particular data type prints.

    Fonts The Fonts page (see Figure 22) allows you to select default fonts for the different types of fields. When you select one of the field type buttons, the Font dialog box displays so you can set the default font for the field.

  • Chapter 2: Touring Crystal Reports 45

    Figure 22. You change default font settings for different items on the Fonts page of the Options dialog box.

    Smart Tag The Smart Tag options (see Figure 23) are used to define web server and page information when you use Microsoft Office smart tags attached to Crystal Reports objects.

  • 46 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 23. Set the Smart Tag options for linking to Microsoft Office XP Smart Tags.

    Report options In addition to application-wide settings, you can change how Crystal Reports works with the current report. Select File | Report Options to display the Report Options dialog box (see Figure 24). All the settings in this window are also available in the Options dialog box. Again, I recommend Save Data With Report not be selected. By default, this option is selected.

  • Chapter 2: Touring Crystal Reports 47

    Figure 24. Change settings for the current report in the Report Options dialog box.

    Report Parts are portions of a report displayed alone without the rest of the report. Using the Report Options dialog box, you can define the name of the home object and the specific records to use in the home object. Chapter 16, Web Reporting, discusses Report Parts in more detail.

    The report-processing model Before moving on to report design, its important to understand how Crystal Reports processes a report. Crystal Reports is a multi-pass report designer. This means the report processes several times. Each pass processes different pieces of information. Crystal Reports actually makes three passes though the report or data.

  • 48 CrysDev: A Developers Guide to Integrating Crystal Reports

    Pre-pass 1 Before looking at any data, Crystal Reports processes any constant formulas. These are formulas that have the same value throughout the entire reporting process. This pass is also called BeforeReadingRecords.

    Pass 1 During the first pass, the following things occur:

    Records are read from the database. The data is stored in memory or temporary tables and is not read again.

    Formulas that contain references to data fields, but are not used for subtotals or summaries are evaluated. This process is called WhileReadingRecords.

    Records are sorted and grouped. Subtotals and summaries are calculated. Cross-tabs are generated.

    Pre-pass 2 Groups are ordered for Top/Bottom N and Hierarchical grouping.

    Pass 2 Page formatting occurs in pass 2. Crystal Reports does page formatting on demand, meaning the page is not formatted until needed. The following things occur in this pass:

    Group selection formulas are calculated. Running totals are calculated. Formulas marked as WhilePrintingRecords are calculated. These are formulas used to

    calculate subtotals and summaries.

    Charts and maps are processed. Subreports are processed.

    Pass 3 In the final pass, the total page count is calculated.

    Summary You should now be familiar with how Crystal Reports is arranged, how to set the many available options, and how a report is processed. In subsequent chapters, you will see many more dialog boxes and settings as you learn more about the report design process.

  • Chapter 2: Touring Crystal Reports 49

    Updates and corrections to this chapter can be found on Hentzenwerkes web site, www.hentzenwerke.com. Click Catalog and navigate to the page for this book.

  • 50 CrysDev: A Developers Guide to Integrating Crystal Reports

  • Chapter 3: Creating Your First Report 51

    Chapter 3 Creating Your First Report

    Creating reports that include drill-down and graphing in Crystal Reports is easier than you might think. This chapter will help you understand the basics of report design with Crystal Reports.

    You are finished installing Crystal Reports and are ready to create your first report. You launch the application and see a dialog box asking you to create a report using either the Report Wizard or a Blank Report. WOW, a wizard! It must know a lot about reports. So, you select that option and are led on your way to creating a wonderful report. However, you soon find the Report Wizard doesnt provide the control you need. It gives you some default settings, many of which you dont like. Its also not good at helping with existing reports. Maybe it would have been better to select Blank Report.

    That is exactly what this chapter is about, creating a report from scratch. You wont spend a lot of time on formatting and getting the report to look perfect. The Crystal Reports documentation, help files, and other books do a good job with this. However, I think it is important to give you some reporting basics before diving into programmatically controlling Crystal Reports.

    Designing the report Before sitting down with Crystal Reports, you should have an idea of what the finished report should look like. This report will be a sales listing by genre and title for the fictional company WebNet Video. I start by listing the report items, sketching the report layout on paper, and then look at the tables you need for the report.

    Sketching the report I already gave you some information about the report, sales listing by genre and title. This sounds like a good report title. The title gives you a clue that you need genre and sales information. The report should list each sale, subtotal the quantity ordered and total dollar value by title and genre, and have a grand total for each grouping at the end, along with a pie chart comparing the subtotal sales for each genre. Most reports also include the print date and page number. Finally, the company logo should also be included. Table 1 lists the items to include.

  • 52 CrysDev: A Developers Guide to Integrating Crystal Reports

    Table 1. Items needed for the Sales Listing by Genre and Title report.

    Item Report Location Title, Sales by Genre and Title Page Header Logo Page Header Print date Page Header Page number Page Header Genre Group 1 Header Movie title Group 2 Header Customer First name Detail Customer Last name Detail Customer city Detail Customer state Detail Quantity purchased Detail Purchase price Detail Extended price (quantity * price) Detail Total by title Group 2 Footer Total by genre Group 1 Footer Grand Total Report Footer Pie chart Report Footer Now that all the report elements are listed, its time to lay out the different elements. It is easier to sketch the report with each element and its location defined. Figure 1 shows the layout for the report.

    Figure 1. Sketching the report layout makes report design easier.

  • Chapter 3: Creating Your First Report 53

    The report data This report will use FoxPro 2.6 DBF files. Crystal Reports can natively read these files, so there is no need to setup ODBC drivers or an OLE DB connection. Chapter 4, Accessing Data, presents a detailed discussion of using a variety of data sources with Crystal Reports. Table 2 describes the four data tables you will use in the report.

    Table 2. Descriptions of the report data tables.

    Table Description Movies Information for each movie available

    for purchase Customer Customer information OrdHd Order header OrdDet Order detail

    Your first report, step-by-step Now that you have a good idea of how the report should look, Ill walk you through the report design process step-by-step.

    1. Launch Crystal Reports.

    2. If the Welcome to Crystal Reports dialog box displays (see Figure 2), select As a Blank Report, click OK, and then skip to step four. If the Welcome dialog box does not display, proceed to step three.

    Figure 2. The Welcome to Crystal Reports dialog displays when you launch Crystal Reports if Show welcome dialog at startup is selected.

  • 54 CrysDev: A Developers Guide to Integrating Crystal Reports

    Selecting the data source Now that you have selected the type of report you want to create, the next step is to identify the data sources. Use the Database Expert dialog box (see Figure 3) to select the data sources for your report.

    Figure 3. Use the Database Expert to select the data source.

    3. Expand the tree view under Create a Connection, and then under xBase.

    4. Navigate to the file Movies.DBF and click Open in the Open window to select it.

    These files, as well as the completed report, are available for download from the Hentzenwerke website.

    5. Back in the Database Expert, double-click Find Database File to select another table.

    6. Now repeat steps four and five for OrdDet.DBF, OrdHd.DBF, and Customer.DBF. Once you select all the files, click Close.

    7. You have selected the tables to use in the report, but Crystal Reports is not yet using the tables. Select customer.dbf in the Available Data Sources list and click the Move button to move it to the Selected Tables list. You can optionally drag-and-drop the table.

    8. Repeat step seven for the three other tables.

    9. Click the Links tab on the Database Expert to display the Links page.

  • Chapter 3: Creating Your First Report 55

    10. The Links page (see Figure 4) is where you link the tables together. In our example, the tables are linked properly to create a parent-child relation from Movies to OrdDet. When you do this, the arrows may point backwards, but you get the same linking. Click OK to close the dialog box.

    Figure 4. Use the Links page of the Database Expert to connect related tables.

    11. You may get a warning (see Figure 5). If you do, click OK to dismiss it.

    Figure 5. The Database Warning reminds you about adding additional data components to the report.

    Adding data fields to the report Now its time to start placing objects on the report. Ill start with fields from the data tables. Figure 6 shows the Crystal Reports design surface. It is here youll do most of your work. Chapter 2, Touring Crystal Reports, discusses the designer in more detail, including explanation of the menus, toolbars, toolboxes, and different report bands.

  • 56 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 6. Use the Crystal Reports design surface to create a report.

    12. In the Field Explorer (see Figure 7), drill down to the Customer table and drag the field FirstName onto the Details band of the report.

  • Chapter 3: Creating Your First Report 57

    Figure 7. The Field Explorer displays the fields for each table added to the report.

    13. Grab the right side of the FirstName field in the Detail band and drag to the left to make the field width shorter. The column label in the Page Header band resizes automatically.

    14. Drag the Customer.LastName, Customer.City, Customer.State, OrdDet.Quantity, and OrdDet.UnitPrice fields onto the Details band, resizing each field as needed. Your report should now look like Figure 8.

  • 58 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 8. The Design tab shows the added fields from the data tables.

    Adding a calculated field Crystal Reports has very powerful capabilities for calculated fields, called formula fields. I will explain formula fields in detail in chapter 8, Using Formulas. For now, enter a simple formula to calculate the total price for each detail line.

    15. Add a total field for each detail item. Click Formula Field, and then click the

    New button in the Field Explorer (see Figure 9).

  • Chapter 3: Creating Your First Report 59

    Figure 9. Create a new Formula Field in the Field Explorer.

    16. Enter DetailTotal for the Name (see Figure 10), and then click Use Editor.

    Figure 10. Name the new Formula Field.

    17. Use the Formula Editor (see Figure 11) to create and edit formula fields. Chapter 8, Using Formulas, explains formulas in detail. For now, enter {orddet.QUANTITY}*{orddet.UNITPRICE} in the lower right of the Formula Editor window.

  • 60 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 11. Use the Formula Editor to create formula fields.

    18. Close the Formula Editor and answer Yes when it prompts you to save changes.

    19. Drag the DetailTotal field from the Field Explorer onto the Details band of the report. Place this new field to the right of the existing Details band fields.

    Grouping and group totals The report layout required grouping by genre and title along with totals for each group. Adding groups and totals is easy in Crystal Reports.

    20. Select Insert | Group from the menu. In the Insert Group window (see Figure

    12), select Movies.Genre in the top drop-down list for the grouping. Click OK to add the group to the report.

  • Chapter 3: Creating Your First Report 61

    Figure 12. Define a grouping in the Insert Group dialog box.

    21. Click Quantity in the Details band. Select Insert | Summary to display the Insert Summary dialog box (see Figure 13).

    22. Select Group #1: movies.GENRE A for the Summary Location. Click OK to add the subtotal to Group Footer #1.

  • 62 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 13. Use the Insert Subtotal dialog box to add grouping subtotals to the report.

    23. Again, click Quantity on the Details band. Select Insert | Summary. Verify that Summary Location is set to Grand Total (Report Footer), and then click OK to add the grand total to Report Footer band (see Figure 14).

  • Chapter 3: Creating Your First Report 63

    Figure 14. Use the Insert Summary dialog box to define grand totals for a report.

    24. Add a new group of Movies.Title to the report. Crystal Reports creates Group #2.

    25. Add a Quantity subtotal to the Group Footer #2 (Movies.Title).

    26. Click the @DetailTotal field in the Details band and add a new subtotal for each group and a grand total in the Report Footer.

    27. This is a good place to view the report. Select File | Print Preview (see Figure 15).

  • 64 CrysDev: A Developers Guide to Integrating Crystal Reports

    Figure 15. The Print Preview shows how the printed report will look.

    28. Examine the preview. The Group Tree, just to the left of the report, lists each genre. As you select an item in the Group Tree, Crystal Reports displays the details for that item. In Chapter 12, Previewing the Report at Runtime, you will his drill-down capability is available to end users. Note the Quantity subtotals and grand total are formatted incorrectly. They shouldnt have decimals. Also, the column headings are not correct.

    Formatting the report Crystal Reports offers powerful formatting features through the Format Editor.

    29. Select the Design tab to go back to the Designer. Right-click the Quantity field in

    the Group Footer #2 band and select Format Field from the shortcut menu.

    30. Select -1,123 for the field format on the Number tab in the Format Editor (see Figure 16). Click OK to save the format.

  • Chapter 3: Creating Your First Report 65

    Figure 16. Select the field format in the Format Editor.

    31. Change the format for the Quantity subtotals Group Footer #1 and for the grand total in the Report Footer band.

    32. Double-click FIRSTNAME in the Page Header. Delete the text and enter First Name for the column heading. Change the column headings for the other fields to Last Name, City, State, Qty, Price, and Total.

  • 66 CrysDev: A Developers Guide to Integrating Crystal Reports

    Adding the page header The page header prints at the top of each page. You can use text, data, formula fields, or even graphics in any band of the report, including the page header.

    33. Drag the splitter bar between the Page Header and Group Header #1 bands down

    to enlarge the Page Header band. Make it about 1 tall.

    34. Drag each column header down so it is at the bottom of the Page Header band.

    35. Insert the report title. Select Insert | Text Object from the menu. Drag the new text object so it centers horizontally at the top of the Page Header band.

    36. Using the toolbar, set the font to 14-points and bold.

    37. Enter the report title Sales by Genre and Title, and then resize the text field so the entire report title is visible.

    38. In the Field Explorer, drill down in Special Fields, drag Page N of M onto the Page Header band, and place it below the report title.

    39. Right-click Page N of M in the Page Header band and select Format Field from the shortcut menu.

    40. On the Common tab of the Format Editor (see Figure 17), set the Horizontal Alignment to Centered and click OK to save the format settings.

  • Chapter 3: Creating Your First Report 67

    Figure 17. Use the Common tab of the Format Editor to set common field formatting option