Print # Statement

See AlsoSDIKO5              Example7HDU0R4>Low

Writes data to a sequential file.


Print # filenumber, [ [{ Spc(n) | Tab(n)}][ expressionlist] [{ ; | ,}] ]


The Print # statement has these parts:

Part                     Description


filenumber            Number used in an Open statement to open a sequential file.  It can be any numeric expression71RISN that evaluates to the number of an open file.  Note that the number sign (#) preceding filenumber is not optional.

Spc(n)                  Name of the Basic function optionally used to insert n spaces into the printed output.  Multiple use is permitted.

Tab(n)                  Name of the Basic function optionally used to tab to the nth column before printing expressionlist.  Multiple use is permitted.

expressionlist       Numeric and/or string expressions1330R89 to be written to the file.

;|,}                       Character that determines the position of the next character printed. A semicolon means the next character is printed immediately after the last character; a comma means the next character is printed at the start of the next print zone.  Print zones begin every 14 columns.  If neither character is specified, the next character is printed on the next line.


If you omit expressionlist, the Print # statement prints a blank line in the file, but you must include the comma.

Because Print # writes an image of the data to the file, you must delimit the data so it is printed correctly.  If you use commas as delimiters, Print # also writes the blanks between print fields to the file.

Also, remember that spacing of data  displayed on a text screen using monospaced characters may not work well when the data is redisplayed in a graphical environment using proportionally spaced characters.

The Print # statement usually writes Variant8PHEAW3 data to a file the same way it writes any other Visual Basic data type3GYXY7. However, there are some exceptions:


         If the data being written is a Variant of VarType7A68ZTZ 0 (Empty1L2JEZ4), Print # writes nothing to the file for that data item.

         If the data being written is a Variant of VarType 1 (Null1DDW7C0), Print # writes the literal #NULL# to the file.

         If the data being written is a Variant of VarType 7 (Date), Print # writes the date to the file using the Short Date format defined in the WIN.INI file.  When either the date or the time component is missing or zero, Print # writes only the part provided to the file.

See Also

Open Statement103I7PS

Print Method9V51E5

Spc FunctionLANSPC

Tab FunctionLANTAB

Write # Statement1VSTITI

Print # Statement Example

The example uses the Print # statement to write data to a test file.  Note the use of the comma and semicolon.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

   Dim FileData, Msg, NL                   ' Declare variables.

   NL = Chr(10)                            ' Define newline.

   Open "TESTFILE" For Output As #1        ' Open to write file.

   Print #1, "This is a test of the Print # statement."

   Print #1,                               ' Print blank line to file.

   Print #1, "Zone 1", "Zone 2"            ' Print in two print zones.

   Print #1, "With no space between" ; "." ' Print two strings together.

   Close #1                                ' Close file.

   Open "TESTFILE" for Input As #1         ' Open to read file.

   Do While Not EOF(1)

      Line Input #1, FileData              ' Read a line of data.

      Msg = Msg & FileData & NL            ' Construct message.


   Close #1                                ' Close file.

   MsgBox Msg                              ' Display message.

   Kill "TESTFILE"                         ' Remove file from disk.

End Sub