Write# Statement

See Also21SHTBE              ExampleZ7B4WL>Low

Writes data to a sequential file.


Write # filenumber [, expressionlist]


The Write # 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 required.

expressionlist             Comma-delimited numeric and/or string expressions1330R89 to be written to the file.  If you omit expressionlist, the Write # statement writes a blank line to the file.


The file identified by filenumber must be opened in Output or Append mode.

Unlike the Print # statement, the Write # statement inserts commas between items and quotation marks around strings as they are written to the file.  You don't have to put explicit delimiters in the list.  Write # inserts a newline character after it has written the final character in expressionlist to the file.

Usually, the Write # statement writes Variant8PHEAW3 data to a file in 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), Write # writes nothing to the file for that data item.  However, if multiple items are written to the file, the delimiting commas themselves are written to the file.

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

         If the data being written is a Variant of VarType 7 (Date), Write # writes the date to the file using the fixed date format #yyyy-mm-dd hh:mm:ss#.  When either the date or the time component is missing or zero, Write # writes only the part provided to the file.

See Also

Open Statement103I7PS

Print # Statement12JHDL9

Write # Statement Example

The example uses Write # to write two variables to a sequential file.  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 Msg, NL, UsrAge, UsrName                ' Declare variables.

   NL = Chr(10)                                ' Define newline.

   ' Make a sample data file.

   Open "TESTFILE" For Output As 1             ' Open file for output.

   Msg = "Enter your name."

   UsrName = InputBox(Msg)                     ' Get user name.

   Msg = "Enter your age."

   UsrAge = InputBox(Msg)                      ' Get user age.

   Write #1, UsrName, UsrAge                   ' Write data to file.

   Close #1                                    ' Close file.

   ' Read the sample data file

   Open "TESTFILE" For Input As #1             ' Open file for input.

   Input #1, UsrName, UsrAge                   ' Read data.

   Close #1                                    ' Close file.

   Msg = "The name '" & UsrName & "' was read from the data file. "

   Msg = Msg & "'" & UsrAge & "' is the age you gave."

   Msg = Msg & NL & NL & "Choose OK to remove test file."

   MsgBox Msg                                  ' Display message.

   Kill "TESTFILE"                             ' Remove file from disk.

End Sub