Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

The Print Method

The Printer object's Print method handles almost all printed output. Print supports several different formats. With Print, you can print messages, variables, constants, and expressions on the printer. The Print method is by far the most commonly used printing method in Visual Basic.

Here is the format of the Print method:

[Printer.]Print [Spc(n) | Tab(n)] Expression
					

The format makes Print look more confusing than it really is, but the portion of the Print method that appears to the right of Print takes some explanation. The next several sections explain the various options available for the Print method.

Printing Literals

The Print method easily prints string and numeric literals. To print a string or numeric literal, place the literal to the right of the Print method. The following methods send the numbers 1, 2, and 3 to the Printer object for output:

Printer.Print 1
Printer.Print 2
Printer.Print 3

When execution hits these three lines of code, Visual Basic sends 1, 2, and 3 to the Printer object with each number appearing on a subsequent line. Every Print method sends a carriage return and line feed sequence to the printer. A lone Print method on a line by itself, such as the following, sends a blank line to the printer:

Printer.Print

The following code sends two lines of text to the Printer object:

Printer.Print "Visual Basic makes writing programs"
Printer.Print "for Windows easy."

When the Windows print spooler obtains these two lines of output, the following appears on the printer's paper:

Visual Basic makes writing programs
for Windows easy.

Printing Variables and Controls

In addition to literals, the Print method prints the contents of variables and controls. The following initializes a string variable and an integer variable and then prints the contents of the variables on the printer:

FirstName = "Charley"
Age = 24
Printer.Print FirstName
Printer.Print Age

Here is the output produced by these Print methods:

Charley
24

Printing Expressions

If you could print only individual strings, numeric constants, and variables, Print would be extremely limiting. Of course, Print isn't that limited. You can combine literals, variables, and expressions to the right of Print methods to produce more complex printed output. The following Print method prints 31:

Printer.Print 25 + (3 * 2)

The expression can contain variables, controls, and constants, like this:

Printer.Print sngFactor * lblWeight.Caption + 10

If you want to send special characters to the printer, you can do that by using the Chr() function. The following expression produces a message that includes embedded quotation marks inside the printed string:

Printer.Print "She said, " & Chr(34) & "I do." & Chr(34)

When execution reaches the former Print method, this is what the print spooler writes to the printer:

She said, "I do."

Printing Multiple Values

When you need to print several values on one line, you can do so by separating those values with semicolons and commas. The semicolon forces subsequent values to appear right next to each other in the output. The comma forces values to appear in the next print zone.

A print zone occurs every 14 columns on the page.

The following two messages print on different lines:

Printer.Print "The sales were"
Printer.Print 4345.67

By using the semicolon, you can force these values to print next to each other:

Printer.Print "The sales were "; 4345.67

The semicolon also acts to keep automatic carriage returns and line feeds from taking place. The following Print method ends with a trailing semicolon:

Printer.Print "The company name is ";

The trailing semicolon keeps the printer's print head at the end of the message for subsequent output. Therefore, the subsequent Print statement shown next, no matter how much later in the code the Print appears, would print its output right next to the previous Print's output:

Printer.Print lblComName.Caption   ' Finish the line

The semicolon is nice for printing multiple values of different datatypes on the same line. The following Print prints all its data on the same line of output:

Printer.Print "Sales: "; curTotsales; "Region:"; intRegNum

The comma is used to force subsequent values to print in the next print zone. The following Print prints a name every 14 spaces on the printed line:

Printer.Print strDivNamel, strDivName2, strDivName3

No matter how long or short each division name is, the next division name will print in the next print zone. The previous Print might produce output similar to the following:

North        NorthEast     South

When you print lists of numbers or short strings, the comma enables you to easily align each column.

Using the Fonts

Most Windows-compatible printers support a variety of fonts. The font-related properties are often useful for printing titles and other special output messages in special font sizes and styles.

You can add special effects to your printed text by using the font modifying properties in Table 16.1. For example, the following code first puts the printer in a boldfaced, italicized, 60-point font (a print size of one full inch), and then prints a message:

Printer.FontBold = True
Printer.FontItalic = True
Printer.FontSize = 60
Printer.Print "I'm learning Visual Basic!"

Better Spacing with Spc() and Tab()

The Print method supports the use of the embedded Spc() and Tab() functions to give you additional control over your program's output. Spc() produces a variable number of spaces in the output as determined by the argument you send to Spc(). The following Print method prints a total of 10 spaces between the first name and the last:

Printer.Print strFirstName; Spc(10), strLastName

The argument you send to the embedded Tab() function determines in which column the next printed character appears. In the following Print, the date appears in the 50th column on the page:

Printer.Print Tab(50), dteDateGenerated

As these examples show, if you print values before or after the Spc() and Tab() functions, you separate the functions from the surrounding printed values using the semicolon.

Listing 16.2 contains some code that computes and prints two housing pricing taxation values.

Example 16.2. Using Spc() and Tab().

 1: Taxl = TaxRate * HouseVal1
 2: Tax2 = TaxRate * HouseVal2
 3:
 4: TotalVal = HouseVal1 + HouseVal2
 5: TotTaxes = TaxRate * TotalVal
 6:
 7: Printer.Print "House Value"; Tab(20); "Tax"
 8: Printer.Print Format(HouseVal1, "Currency");
 9: Printer.Print Tab(20); Format(Taxl, "Currency")
10: Printer.Print Format(HouseVal2, "Currency");
11: Printer.Print Tab(20); Format(Tax2, "Currency")
12:
13: Printer.Print  ' Prints a blank line
14: Printer.Print "Total tax:"; Spc(5); Format(TotTaxes, "Currency")
15: Printer.NewPage
16: Printer.EndDoc

Here is a sample of what you may see after Listing 16.2 executes:

House Value     Tax
$76,578.23      $9,189.39
$102,123.67     $12,254.81

Total tax:      $21,444.20

The Tab(20) function call ensures that the second column, which contains the tax information, is aligned. Also, notice that the trailing semicolons let you continue the Print methods on subsequent lines without squeezing long Print method values onto the same line. The code uses Spc() to insert five spaces between the title and the total amount of tax. The last two lines ensure that the printing stops properly.

Share ThisShare This

Informit Network