Line Input # Statement
See Also Example
Reads a line from a sequential file into a String or Variant variable.
Line Input # filenumber, variablename
The Line Input # statement has these parts:
filenumber Number used in the Open statement to open the file. It can be any numeric expression that evaluates to the number of an open file. Note that the number sign (#) preceding filenumber is required.
variablename Name of the variable used to receive a line of text from the file.
The Line Input # statement reads all characters in a sequential file up to a carriage return. It then skips over the carriage-return/linefeed sequence.
Line Input # reads a text file one line at a time.
Input # Statement
Line Input # Statement Example
The example uses the Line Input # statement to read and display the first line in the CONFIG.SYS 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 Drive, FileName, Msg, NL, TextLine ' Declare variables.
NL = Chr(10) ' Define newline.
Msg = "Which drive does your operating system boot from?"
Drive = UCase(Left(InputBox(Msg), 1)) ' Get user input.
If Drive = "C" or Drive = "A" Then
FileName = Drive & ":\CONFIG.SYS"
If Dir(FileName) <> "" Then ' Check if file exists.
Open FileName For Input As #1 ' If it does, open it.
Line Input #1, TextLine ' Get complete line.
Close #1 ' Close file.
Msg = "The first line of your CONFIG.SYS file is:"
Msg = Msg & NL & TextLine & "."
Else ' No CONFIG.SYS.
Msg = "Could not find a CONFIG.SYS file on drive "
Msg = Msg & Drive & ":"
Msg = "You did not provide a valid boot drive. Systems "
Msg = Msg & "normally boot from either A: or C: drive."
MsgBox Msg ' Display message.