See Also Example
Reads from a disk file into a variable.
Get [#] filenumber,[recordnumber],variablename
The Get 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.
recordnumber The largest possible recordnumber is 2^31 - 1, or 2,147,483,647. For files opened in Random mode, recordnumber is the number of the record to be read. For files opened in Binary mode, recordnumber is the byte position at which reading starts. The first byte in a file is at position 1, the second byte is at position 2, and so on. If you omit recordnumber, the next record or byte (the one following the last Get or Put statement or the one pointed to by the last Seek function) is read from. If you omit recordnumber, you must still include the commas. For example:
variablename Name of the variable used to receive input from the file. Any variable can be used except object variables and array variables (variables that describe an entire array). However, you can use a variable that describes a single element of an array.
For files opened in Random mode, these following rules apply:
If the length of the data being read is less than the length specified in the Len clause of the Open statement, Get still reads subsequent records on record-length boundaries. The space between the end of one record and the beginning of the next record is padded with the existing contents of the file buffer. Because the amount of padding data can't be determined with any certainty, it is generally a good idea to have record length match the length of the data being written.
If the variable being read into is a variable-length string, Get reads a 2-byte descriptor containing string length and then reads the data that goes into the variable. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual length of the string.
If the variable being read into is a numeric Variant (VarType 0-7), Get reads 2 bytes identifying the VarType of the Variant and then reads the data that goes into the variable. For example, when reading a Variant of VarType 3, Get reads 6 bytes: 2 bytes identifying the Variant as VarType 3 (Long) and 4 bytes containing the Long data. The record length specified by the Len clause in the Open statement must be at least 2 bytes greater than the actual number of bytes required to store the variable.
If the variable being read into is a String Variant (VarType 8), Get reads 2 bytes identifying the VarType, then reads 2 bytes indicating the length of the string, and then reads the string data. The record length specified by the Len clause in the Open statement must be at least 4 bytes greater than the actual length of the string.
If the variable being read into is any other type of variable (not a variable-length string or a Variant), Get reads only the variable data. The record length specified by the Len clause in the Open statement must be greater than or equal to the length of the data being read.
Elements of user-defined types are read using Get as if each were being read individually except that there is no padding between elements. The record length specified by the Len clause in the Open statement must be greater than or equal to the sum of all the bytes required to write the individual elements.
For files opened in Binary mode, all of the Random rules apply except that:
In Binary mode, the Len clause in the Open statement has no effect. Get reads all variables from disk contiguously, that is, with no padding between records.
Get reads variable-length strings that aren't elements of user-defined types without expecting the 2-byte length descriptor. The number of bytes read equals the number of characters already in the string. For example, the following statements read 10 bytes from file number 1:
VarString$ = String$(10," ")
Get #1, , VarString$
Get Statement Example
The example uses the Get statement to read data from a disk 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 Clientname As String * 20 ' Declare variables.
Dim Indx, Max, Msg, NL
NL = Chr(10) ' Define newline.
' Make a sample random-access file using the Put statement.
Msg = "Enter a name, using whatever form you like." & NL & NL
Msg = Msg & "Press 'Enter' with no name to conclude entry."
Open "TESTFILE" For Random As #1 Len = Len(ClientName)
ClientName = InputBox(Msg) ' Get user input.
Do While ClientName <> String(20, " ")
Put #1, , ClientName ' Write to file.
ClientName = InputBox(Msg) ' Get user input.
' Read the sample random-access file using the Get statement.
Max = Loc(1)
If Max = 0 Then ' Check if records exist.
Msg = "Your file contains no names." & NL
Msg = "Your file contains the following names:" & NL & NL
For Indx = 1 To Max
Get #1, Indx, ClientName ' Read from file.
Msg = Msg & ClientName & NL
Close #1 ' Close file.
Msg = Msg & NL & "Choose OK to remove the created test file."
MsgBox Msg ' Display message.
Kill "TESTFILE" ' Remove file from disk.