The Print Method – Part 3
Using the Tab(n) function
Note: The formatting option for Print discussed in this topic is only meaningful if the object you are printing to is set to a monospaced font (like Courier or Fixedsys). The output will not look right if the object is set to a proportional font (like the default MS Sans Serif, Times New Roman, etc.).
The Tab function can be used only in the expression list of a Print statement and is written Tab(n). Tab is the name of the function; the expression in parentheses is called the argument. The argument n refers to a position on a line. The value n indicates an absolute position, not a relative one, on the output line. The first position on a line is Tab(1), the thirtieth position is Tab(30), and the fiftieth position is Tab(50), and so on. For example, the statement
Print Tab(10); "ORD NBR"; Tab(25); "PRODUCT"
causes the following output to be generated:
1 1 2 2 3 3 4 4 5 5
1...5....0....5....0....5....0....5....0....5....0....5...
ORD NBR PRODUCT
The characters ORD NBR start in column 10 and the characters PRODUCT start in column 25.
If the number specified in the Tab function precedes the current position, the item is printed at that tab position on the next line. For example, the statement
Print Tab(25); "ORD NBR"; Tab(10); "PRODUCT"
causes the following output to be generated:
1 1 2 2 3 3 4 4 5 5
1...5....0....5....0....5....0....5....0....5....0....5...
ORD NBR
PRODUCT
Consider the following program segment:
Print Tab(23); "ADMIN PHONE LIST"
Print "ADMIN NAME"; Tab(27); "EXTENSION"; Tab(45); "DEPARTMENT"
Print "JIM JOHNSON"; Tab(29); "8968"; Tab(45); "FINANCE"
Print "KAY KLEINMAN"; Tab(29); "7724"; Tab(45); "HUM RES"
Print Tab(10); "END OF REPORT"
The Print statements above would cause the following output to be generated :
1 1 2 2 3 3 4 4 5 5
1...5....0....5....0....5....0....5....0....5....0....5...
ADMIN PHONE LIST
ADMIN NAME EXTENSION DEPARTMENT
JIM JOHNSON 8968 FINANCE
KAY KLEINMAN 7724 HUM RES
END OF REPORT
Note: In all the "Tab" examples above, only string constants were used in the expression list, but of course numeric constants and string or numeric variables can certainly be used (and the "leading sign / trailing space" rule would still apply for numeric items).
If you wish, set up another "Try It" project, and place the sample Print statements in the cmdTryIt_Click event, as follows:
Private Sub cmdTryIt_Click()
Print Tab(23); "ADMIN PHONE LIST"
Print "ADMIN NAME"; Tab(27); "EXTENSION"; Tab(45); "DEPARTMENT"
Print "JIM JOHNSON"; Tab(29); "8968"; Tab(45); "FINANCE"
Print "KAY KLEINMAN"; Tab(29); "7724"; Tab(45); "HUM RES"
Print Tab(10); "END OF REPORT"
End Sub
When you run the project and click the "Try It" button, the output should look like this:
Download the VB project code for the example above here.