Tuesday, March 14, 2017

Seek Function

Seek Function

Note: The function, method, object, or property described in this topic is disabled if the Microsoft Jet Expression Service is running in sandbox mode, which prevents the evaluation of potentially unsafe expressions. For more information on sandbox mode, search for "sandbox mode" in Help.

Returns a Long specifying the current read/write position within a file opened using the Open statement.

Syntax

Seek( filenumber )

The required filenumberargument is an Integer containing a valid file number.

Remarks

Seek returns a value between 1 and 2,147,483,647 (equivalent to 2^31 – 1), inclusive.

The following describes the return values for each file access mode.

Mode

Return Value

Random

Number of the next record read or written

Binary,
Output,
Append,
Input

Byte position at which the next operation takes place. The first byte in a file is at position 1, the second byte is at position 2, and so on.


Example

Note: Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.

This example uses the Seek function to return the current file position. The example assumes 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 number of 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.
' Print record number to the Immediate window.
Debug.Print Seek(1)
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 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.
' Print byte position to the Immediate window.
Debug.Print Seek(1)
Loop
Close #1 ' Close file.

No comments:

Post a Comment