Siebel VB Language Reference > VB Language Reference >

Open Statement


This standard VB statement opens a file for input or output.

Syntax

Open filename [For mode] [Access access] [lock] As [#]filenumber [Len = reclen]

Argument
Description

filename

A string or string expression representing the name of the file to open

mode

A keyword indicating the purpose for which the file is opened

access

A keyword indicating the method of access to the file

lock

A keyword designating the access method allowed to the file by other processes

filenumber

An integer used to identify the file while it is open

reclen

In a Random or Binary file, the length of the records

Returns

A file opened in the specified manner.

NOTE:  The file opens in the default code page of the local operating system. File I/O does not support Unicode.

Usage

The following keywords are used for mode, access, and lock:

Keyword
Consequences

Mode Keywords

Input

Reads data from the file sequentially

Output

Puts data into the file sequentially

Append

Adds data to the file sequentially

Random

Gets data from the file by random access

Binary

Gets binary data from the file

Access Keywords

Read

Reads data from the file only

Write

Writes data to the file only

Read Write

Reads or writes data to the file

Lock Keywords

Shared

Read or write is available on the file

Lock Read

Only read is available

Lock Write

Only write is available

Lock Read Write

No read or write is available

A file must be opened before any input/output operation can be performed on it.

If filename does not exist, it is created when opened in append, binary, output, or random modes.

If mode is not specified, it defaults to random.

If access is not specified for random or binary modes, access is attempted in the following order: Read Write, Write, Read.

If lock is not specified, filename can be opened by other processes that do not specify a lock, although that process cannot perform any file operations on the file while the original process still has the file open.

Use the FreeFile function to find the next available value for filenumber.

The reclen argument is ignored for Input, Output, and Append modes.

Example

This example opens a file for random access, gets the contents of the file, and closes the file again. The second subprogram, CreateFile, creates the file c:\temp001 used by the main subprogram.

(general) (declarations)
Option Explicit
Declare Sub CreateFile

Sub CreateFile
      ' Put the numbers 1-10 into a file
   Dim x as Integer
   Open "c:\temp001" for Output as #1
   For x = 1 to 10
      Write #1, x
   Next x
   Close #1
End Sub

Sub Button_Click
   Dim acctno as String * 3
   Dim recno as Long
   Dim msgtext as String
   Call CreateFile
   recno = 1
   newline = Chr(10)
   Open "c:\temp001" For Random As #1 Len = 3
   msgtext = "The account numbers are:" & newline
   Do Until recno = 11
         Get #1,recno,acctno
         msgtext = msgtext & acctno
         recno = recno + 1
   Loop
   Close #1
      Kill "c:\temp001"
End Sub

See Also

Close Statement
FreeFile Function

Siebel VB Language Reference