See Also Example
Sets the position in a file for the next read or write.
Syntax
Seek [#]filenumber, position
Remarks
The Seek statement has these parts:
Part Description
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.
position Numeric expression that indicates where the next read or write should occur. The value of position must be between 1 and 2,147,483,647 (equivalent to 2^31-1), inclusive.
For files opened in Random mode, position is the number of a record in the file.
For files opened in Binary, Input, Output, or Append mode, position is the byte position relative to the beginning of the file. The first byte in a file is at position 1, the second byte is at position 2, and so on. After a Seek operation, the next file input/output operation starts at the specified byte.
Note Record numbers in Get and Put statements override file positioning done by Seek.
Performing a file write after doing a Seek operation beyond the end of a file extends the file. If you attempt a Seek operation to a negative or zero position, an error occurs.
Get Statement
Open Statement
Put Statement
Seek Function
The example uses the Seek statement to set the file position, and the Seek function to return the current file position. 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 NameField As String * 20 ' Declare form variable.
Dim I, Max, Msg ' Declare variables.
' Create sample data file.
Open "TESTFILE" For Random As #1 Len = Len(NameField)
' Get user input to fill records.
For I = 1 To 3
NameField = InputBox("Enter student name: ")
Put #1, I, NameField ' Put record on disk.
Next I
Close #1 ' Close data file.
' Open data file.
Open "TESTFILE" For Random As #1 Len = Len(NameField)
Max = LOF(1) \ Len(NameField) ' Calculate # of records.
For I = Max To 1 Step -1 ' Read file backwards.
Seek #1, I ' Set file position.
Get #1, , NameField ' Get record.
Msg = "Record #" & (Seek(1) - 1) & " contains: "
Msg = Msg & NameField
MsgBox Msg ' Display student name.
Next I
Close #1 ' Close file.
Msg = "Choose OK to remove the test file."
MsgBox Msg ' Display message.
Kill "TESTFILE" ' Remove file from disk.
End Sub