Printing With REALbasic

Introduction
Reporting is the 'dark art' of programming because it involves three separate states for your application data. Many applications show large amounts of data in a listbox or a grid format and is meant to be manipulated graphically by the user via scrollbars and other on-screen controls. That's the easy part.

However, most users want to print the report to paper for a variety of reasons. In the old days with dot matrix printers you'd just throw the data at the printer port and be done with it. Nowadays, applications are expected to show a print preview so the user can adjust printer settings and verify that the data they expect to see is indeed being shown and it all has to be printed exactly as it's seen on-screen. (It's debatable if this really saves paper!)

So that's three separate functions to show the same data. Using a listbox or a grid in your window is easy but how do you display a print preview? Even better: how do you display a print preview that will actually look like your final output on paper? How do you deal with landscape mode versus portrait and how do you deal with the amazing number of paper sizes available around the world?

So what's a programmer to do? Like anything else in REALbasic you start off by saying, "How hard can it be? I can do this easily." You're a smart programmer. You know that printing is nearly the same as drawing to a canvas. So you put a canvas on a window and then you start looking at a report header, then a page header and then the data. Oh, and don't forget page footers and figuring out how many pages are in a report. Do you need to summarize or do other types of mathematical functions on the data?

Yeesh! Before you know it you've got some serious spaghetti code that no one but you understands - maybe. What I'll attempt to do is show you various methods of reporting. Along the way we'll hopefully learn a few things about programming.

What do you want to print (Probably the single hardest question). Is your report a single page or multiple pages? Do you need page headers and footers? What about report headers and footers? Do you need to print a logo or have a bar or pie chart?

Sample Projects
So what we've done is come up with some sample projects that try to imitate a real-world application as close as we can. In all of our examples, we'll use an 'Items' table that could represent any sort of list that we'll report against.


The database we're using is a REALSQL Database that comes standard with REALbasic 2005 and above. The database has only one table called "Items" with the following fields:
iItemid - The unique id for the record
sName - Name string
sType - Type string (Inventory, Non-Inventory, Service, Other Charge, etc.)
dCost - Cost of the item in double
dPrice - Retail price of the item in double format
sMarkup - The Markup method string (Cost + 10%, X 2, etc.)
sPurchaseDescription - Purchase description string
sSalesDescription - Sales description string
sCategory - Item category string (Air Conditioner, Furnace, Sprinkler, etc.)
dReorderPoint - The reorder point in double format (why double? If you have a case of something you might reorder when you have .5 left)
dQtyOnHand - How many do you have in-stock in double format.


Method 1: Graphics Objects

Method 2: Using On-Target Reports