Input # Statement

See AlsoMBKTKG              ExampleDL10R4>Low

Reads data from a sequential file and assigns the data to variables.


Input # filenumber, variablelist


The Input statement has these parts:

Part               Description


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

variablelist     Comma-delimited list of the variables that are assigned values read from the file.


A variable in variablelist can't be an arrayYPCGZO variable but can be a variable that describes an element of an array.  A variable also can't be a user-defined type variable but can be an element.  All other variables are allowed, except object variables9DGMAH.

Data items in a file must appear in the same order as the variables in variablelist.  Be sure to match data in the file with variables of the proper data type3GYXY7: string data into String or Variant8PHEAW3 variables, numeric data into numeric or Variant variables, and uniquely Variant data (dates and Null1DDW7C0 values) into Variant variables.  Failure to match the data with a variable of the correct data type may produce unpredictable results.

When a variable in variablelist is numeric, the first nonspace character encountered is assumed to be the start of a number.  The end of the number is assumed when a space, a comma, or the end of a line is encountered.  Blank lines are input as zero.  If the data is not valid numeric data, zero is the value assigned to the variable.

When a variable in variablelist is a String, the first nonspace character encountered is assumed to be the start of a character string.  If that first character is a double quotation mark ("), it is ignored, but all characters following it (including spaces and commas) up to the next double quotation mark are input into the String variable.  For strings not delimited by double quotation marks, the end of a string is assumed when a comma, a space, or the end of a line is encountered.  Blank lines are input as zero-length strings.

When the variablelist variable is a Variant, the first nonspace character is assumed to be the start of the data.  The following rules determine how the actual data is treated:

         If the input data is a valid number, the Variant is set to an appropriate numeric VarType7A68ZTZ.

         If the input data consists of no data, only a delimiting comma or a blank line, the Variant is assigned VarType 0 (Empty1L2JEZ4).

         If the input data is the literal #NULL#, the Variant is assigned VarType 1 (Null).

         If the input data is a date literal (a date of the form #yyyy-mm-dd hh:mm:ss#), the Variant is assigned VarType 7 (Date).  Note that when either the date portion or the time portion is present, the other portion is optional.

         If the input data is not a valid number, is not Empty or Null, and is not a valid date, it is input as a String and the Variant is assigned VarType 8.


If the end of file is reached while a data item is being input, the input is terminated and an error occurs.

See Also

Input, Input$ Function512T6F0

Write # Statement1VSTITI

Input # Statement Example

In this example, the Input # statement reads a non-numeric value from a file into a variable.  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.

   MakeDataFile                          ' Create sample data file.

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

   Do While Not EOF(1)

      Input #1, FileData                 ' Read line of data.

      Msg = Msg & FileData & NL          ' Construct message from data.


   Close #1                              ' Close file.

   MsgBox Msg                            ' Display message.

   Kill "TESTFILE"                       ' Remove file from disk.

End Sub


Sub MakeDataFile

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

   Write #1, "This is test line 1."      ' Print some data.

   Write #1, "This is test line 2."

   Close #1                              ' Close file.

End Sub