Seek function

Category: File / I-O & Environment

Summary

Get/Set file position.

Syntax

Seek(filenumber) The required filenumber argument is an Integer containing a valid file number.

Example

Example
This example uses the Seek function to return the current file position. The example assumes that TESTFILE is a file containing records of the user-defined type Record.
Type Record ' Define user-defined type.
ID As Integer
Name As String * 20
End Type


For files opened in Random mode, Seek returns the number of the next record.
Dim MyRecord As Record ' Declare variable.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
Do While Not EOF(1) ' Loop until end of file.
Get #1, , MyRecord ' Read next record.
Debug.Print Seek(1) ' Print record number to the Immediate window.
Loop
Close #1 ' Close file.



For files opened in modes other than Random mode, Seek returns the byte position at which the next operation takes place. Assume that TESTFILE is a file containing a few lines of text.
Dim MyChar
Open "TESTFILE" For Input As #1 ' Open file for reading.
Do While Not EOF(1) ' Loop until end of file.
MyChar = Input(1, #1) ' Read next character of data.
Debug.Print Seek(1) ' Print byte position to the Immediate window.
Loop
Close #1 ' Close file.

Microsoft Support Page

https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/seek-function

Back to Functions