See Also Example
Reads data from a sequential file and assigns the data to variables.
Syntax
Input # filenumber, variablelist
Remarks
The Input statement has these parts:
Part Description
filenumber Number used to open the file with the Open statement. It can be any numeric expression 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 array A variable also can't be a user-defined type variable but can be an element. All other variables are allowed, except object variables .
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 type : string data into String or Variant variables, numeric data into numeric or Variant variables, and uniquely Variant data (dates and Null 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 VarType .
If the input data consists of no data, only a delimiting comma or a blank line, the Variant is assigned VarType 0 (Empty ).
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.
Input, Input$ Function
Write # Statement
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.
Loop
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