Stuff
OS/400 Edition
Volume 2, Number 3 -- January 30, 2003

Working with Visual Basic's Printer Object


by Bob Butcher

[The code for this article is available for download.]

display

I remember a few years ago when I first started creating Visual Basic applications for my clients to view their AS/400 data. They loved the idea that they had a GUI screen to putter around the database with. After a few weeks, one of my clients approached me and said, "Can we print this information to our desktop printer?" I did some research and found that printing within Visual Basic isn't hard at all if you get up to speed with its Printer object.

My objective is to introduce you to the Printer object and some of the most important methods and properties that you'll find for it within Visual Basic. I'll introduce to you the key points and explain how they work and how to use them to generate a simple report. The sample report that I'll design looks like the sample below, with report date and time, header information about what the report represents, column headers, AS/400 database information, and some summary information:

01/17/03
1:25:23 PM

Exam:  Advanced Thermodynamics Engineering – Final  

     Student Name        Grade           Year	

     Foote, Paula        98              12
     Mirabito, Angela    97              12
     Mott, Tiffany       96              12		
     Wesner, Sarah       94              12
     Haney, Amanda       91              11
     Fisher, Jessie      87              11	
     Nordberg, Lynnea    85              11
     Larose, Ashleigh    84              11	
     Borowski, Kristina  78              12	
     Mertz, Jessica      72              11

Total tests graded were 10 and the class average was an 88

Just a quick overview of how the printing is handled behind the scene. I first issue the print statements, which you will see in a moment, in Visual Basic code. Visual Basic buffers the information I send to the printer in memory until the application signals that the print job has ended. Each "report," or document, is considered a print job and is sent to a printer queue. The Windows Print Manager then takes over. It will wait until any earlier print jobs are completed, then it will send your job to the printer. This is the same concept as an AS/400 message queue or data queue. Enough gabbing, let's get coding.

Print Method

The Printer object's main component is the Print method. If you've used Visual Basic before, you've probably run across this while debugging code using the Debug object or you may have used it to print something on your Form object. Before we talk about how the Print method works with the Printer object, I'd like to define some variables that we'll use throughout this lesson. I'll fill some test data into the variables:

Dim mPlayer as string, mGrade as integer, mYear as integer

mPlayer = "Paula Jean Foote"
mGrade = 98
mYear = 12

To print each one of these fields, execute the following code:

Printer.Print mPlayer
Printer.Print mGrade
Printer.Print mYear
Printer.Print "This concludes this test"

With the following results:

Paula Jean Foote
98
12
This concludes this test

The first thing to note is that the first Printer.Print statement executed will create the print job. You can print a variable or literal, as shown above, with the last Printer.Print statement. Notice that each Printer.Print statement generates a new line in your printout. Let's suppose you wanted to print all your information on the same line. There are a few ways to do this. One way is to use concatenation, or the ampersand (&) symbol. Using the variables above, I could do something like this:

Printer.Print mPlayer & " - grade " & mYear & " - test score: " & mGrade

This would print the following:

Paula Jean Foote - grade 12 - test score: 98

Another way is to use a semicolon (;) within a line of code. This does not use a linefeed, so the next literal or variable is printed within the next print position. An example is shown here:

Printer.Print mPlayer; " is a phenomenal basketball player" will generate:

	Paula Jean Foote is a phenomenal basketball player 

Two functions that may help you position some of your text are the SPC(n) function and the TAB(n) function. As you might guess, SPC(n) moves n spaces to the right and TAB(n) moves to Tab column n. So by running the following code, print zones are spaced 14 fixed-width print positions apart:

Printer.Print mPlayer; SPC(20); "is from Sidney, NY"

Will produce:

	Paula Jean Foote                              is from Sidney, NY

Chances are pretty good that the font you are using to print is proportional, which means that the letter w takes up more space that the letter i. Just a heads-up that this may occur when using the SPC(n) function. This may give you undesirable results in the way that your columns line up. I don't cover fonts at all in this article, but you can check out the Fonts object within the Printer object for more details.

Lining Up with CurrentX and CurrentY Properties

Many times when I'm creating reports in Visual Basic, I need to add some columns in specific locations on the screen. This can be done without using the function SPC(n). Remember, if you are using a font that is proportional you'll have a hard time lining up your columns. Have no fear, properties CurrentX and CurrentY are here. Remember those silly X/Y graphs that you had to learn about in the fifth grade? This is a similar concept. The X axis runs horizontally across the page, while the Y axis runs vertically. This is the current location on your printout by the X and Y coordinates. With these coordinates, you can move your text, or even a graphic, to any point on your virtual page. Before we put this into action, let's talk about some new properties.

ScaleMode is a property used to define the unit of measure in our virtual page layout. The default measuring system is twips. To give you an idea of the size of a twip, there are 1,444 twips in an inch. This allows for very precise positioning on our printout. These are the different types of valid values for the ScaleMode property:

0 User Defined
1 Twips
2 Point
3 Pixel
4 Character
5 Inch
6 Millimeter
7 Centimeter 

My personal preference is twips. If you wanted to start printing one inch from the left edge of the page, you would code this:

Printer.CurrentX = 1444

If I wanted to start two inches down from the top (1444 x 2), along the Y axis (which, you remember, runs vertically):

Printer.CurrentY = 2888

You can see that this precise measurement can line up columns nicely in a report. If I wanted to take the variables above and put them in a line on my report:

Printer.currentY = 2888
Printer.CurrentX = 1444     'one inch from left edge
Printer.Print mPlayer

Printer.currenty = 2888
Printer.CurrentX =  5776     '4 inches from left edge
Printer.Print mGrade

Printer.currentY = 2888
Printer.CurrentX = 8664      '6 inches from left side of paper
Printer.Print mYear

That would print something like this:

Paula Jean Foote                 98                 12

Are you starting to see how you can use these methods and properties to generate a report? If you wanted to read records from a database and print them on the report, you could do so by changing the CurrentY property to appear farther down the page, so that you aren't printing over existing data. The CurrentX property would remain the same, because this value represents the column positions. So if you wanted a quarter of an inch between records in twips (or 1444 / 4), you would add 361 to the CurrentY value. You could do something like this:

mValue = mValue + 361
Printer.CurrentY = mValue 

What do I do when I reach the end of the page? It is critical to know the dimensions of your page. This can be found with the ScaleWidth and ScaleHeight properties. Scalewidth will tell you the dimensions of your page width, and ScaleHeight will tell you the dimensions of your page height. You will use ScaleHeight in conjunction with your CurrentY property. If the CurrentY property is greater than ScaleHeight, you can start a new page. A new page can be started with the Printer.NewPage method. This will close the current page and start a new one.

Send It to the Printer

When you have finished everything necessary to complete your report, you send it to the printer, using the Printer.Enddoc method as your last statement. If you don't issue this Enddoc method, when you close your VB program, it will automatically be sent for you.

It's funny how the world is trying to get to a paperless environment, yet everyone still wants a printout. Most of the Visual Basic applications that I've recently designed have the capability to print via the Printer object embedded within the code. Not that anyone will need it, but it is always there as a reserve. I hope that today's lesson has opened your eyes to seeing just how easy printing can be within Visual Basic.


Bob Butcher is an AS/400 consultant with over 15 years' experience in the AS/400 arena. He specializes in client/server applications that deal with the AS/400 and Active Server Pages or Visual Basic. Bob can be reached at butcherb@samicsoft.com.


Sponsored By
WORKSRIGHT SOFTWARE

Do you need to verify that Cappahayden Newfoundland Canada has a postal code of A0A 1S0?

Do you need to retrieve the Canadian city name and province name associated with Canadian postal code A0A 1X0?

WorksRight Software, Inc. can help. Our CPC System contains all 775,000+ Canadian postal codes along with the matching city and province name. We also provide the area code, lat/lon and time zone. Our yearly subscription provides monthly updates and unlimited telephone support.

Visit our Web site www.worksright.com to learn more about CPC
and order a 30-day, no hassle, free trial. Or call 601-856-8337.
We'll be glad to answer all your questions.


THIS ISSUE
SPONSORED BY:

Profound Logic Software
ASNA/Guild Companies
Snap-E Books
WorksRight Software


BACK ISSUES

TABLE OF
CONTENTS
Prompting iSeries Commands in Java

iSeriesMonitorME: An Intro to Wireless Programming with iSeries ToolboxME

Tired of Resetting Terminals on the AS/400? Let Somebody Else Do It, Part 2

Working with Visual Basic's Printer Object


Editors
Shannon O'Donnell
Kevin Vandever

Managing Editor
Shannon Pastore

Contributing Editors:
Howard Arner
Raymond Everhart
Joe Hertvik
Ted Holt
David Morris

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.