advr.hlp (Topic list)
OPEN Statement (File I/O) Details
  Summary  Details  Example                Contents  Index  Back
──────────────────────────────────────────────────────────────────────────────
 OPEN file$ [FOR mode] [ACCESS access] [lock] AS [#]filenumber% [LEN=reclen%]
 OPEN database$ FOR ISAM tabletype tablename$ AS [#]filenumber%
 
 Usage Notes
   ■ The arguments file$ and database$ specify an optional device, followed
     by a file name or path conforming to the MS-DOS naming conventions.
     The argument database$ must be the name of an ISAM file. If file$ or
     database$ do not exist, they will be created.
 
   ■ The argument tabletype is the name of a user-defined type that defines
     a subset of the table definition. The argument tablename$ is the name
     of the ISAM table being opened. It follows the ISAM naming conventions.
     See: TYPE Statement  ISAM Naming Conventions
 
   ■ The argument mode is a keyword specifying one of the following
     file modes:
 
         Keyword   Description
         ═══════   ═════════════════════════════════════════════════════════
         APPEND    Sequential output mode and sets the file
                   pointer to the end of file and the record number to
                   the last record of the file. A PRINT # or WRITE #
                   statement then extends (appends to) the file.
         BINARY    Specifies binary-file mode. In binary mode,
                   you can read or write information to any byte
                   position in the file using GET and PUT.
                   In binary mode, if no ACCESS clause is present,
                   three attempts are made to open the file. The
                   attempts follow the same order as those for random
                   files.
         INPUT     Sequential input mode
         OUTPUT    Sequential output mode
         RANDOM    (Default) Specifies random-access file mode, the
                   default mode. In random mode, if no ACCESS clause
                   is present, three attempts are made to open the
                   file when the OPEN statement is executed. Access is
                   attempted in the following order:
                   • Read/write
                   • Write-only
                   • Read-only
 
   ■ The argument access is a keyword that specifies the operation performed
     on the opened file:
 
         Type          Description
         ══════════    ═════════════════════════════════════════════════════
         READ          Opens the file for reading only.
         WRITE         Opens the file for writing only.
         READ WRITE    Opens the file for both reading and writing.
                       This mode is valid only for random and binary
                       files and files opened for append.
 
   ■ The lock argument works in a multiprocessing environment to restrict
     access by other processes to an open file. It can be one of the
     following keywords specifying the lock type:
 
         Keyword           Description
         ═══════════════   ═════════════════════════════════════════════════
         SHARED            Any process on any machine can read from or write
                           to this file. Do not confuse the shared lock type
                           with the SHARED statement or the shared attribute
                           that appears in other statements.
         LOCK READ         No other process is granted read access to this
                           file. This access is granted only if no other
                           process has a previous read access to the file.
         LOCK WRITE        No other process is granted write access to this
                           file. This lock is granted only if no other
                           process has a previous write access to the file.
         LOCK READ WRITE   No other process is granted either read or write
                           access to this file. This access is granted only
                           if read or write access has not already been
                           granted to another process, or if a lock read or
                           lock write is not already in place.
 
   ■ If you do not specify a lock type, the file can be opened for reading
     and writing any number of times by this process, but other processses
     are denied access to the file while it is opened.
 
   ■ The filenum% argument is an integer expression whose value is between
     1 and 255, inclusive. When an OPEN is executed, the file number is
     associated with the file as long as it is open. Other I/O statements can
     use the number to refer to the file.
 
   ■ The argument reclen% is an integer expression less than or equal to
     32,767 bytes. It specifies different settings for random-access or
     sequential files:
 
         File Mode       Description
         ═════════════   ═══════════════════════════════════════════════════
         Random access   reclen% sets the record length (the number of
                         characters in a record); default is 128 bytes.
 
         Sequential      reclen% specifies the number of characters to be
                         loaded into the buffer before the buffer is written
                         to, or read from, the disk. A larger buffer means
                         more room taken from Visual Basic, but faster file
                         I/O; a smaller buffer means more room in memory for
                         Visual Basic, but slower I/O. Default is 512 bytes.
 
   ■ The LEN clause and reclen% are ignored if the file mode is binary.
 
   ■ You must open a file before any I/O operation can be performed on it.
     OPEN allocates a buffer for I/O to the file or device and determines the
     mode of access used with the buffer.
 
   ■ If the file is already opened by another process and the specified type
     of access is not allowed, the OPEN fails and Visual Basic generates the
     error message, "Permission denied."
 
   ■ The ACCESS clause works in an OPEN statement only if you are using a
     version of MS-DOS that supports networking (DOS version 3.0 or later).
     In addition, you must run the SHARE.EXE program (or the network startup
     program must run it) to perform any locking operation. If ACCESS is used
     with OPEN, earlier versions of MS-DOS return the error message,
     "Advanced feature unavailable."
 
   ■ For sequential files, reclen% need not correspond to an individual
     record size, because a sequential file may have records of different
     sizes.
 
   ■ The following devices are supported by Visual Basic and can be named
     and opened with the file argument:
 
         KYBD:, SCRN:, COMn:, LPTn:, CONS:, PIPE:.
 
   ■ The Visual Basic file I/O system allows you to take advantage of user-
     installed devices. (See your MS-DOS manual for information on character
     devices.)
 
   ■ Character devices are opened and used in the same manner as disk files.
     However, characters are not buffered by Visual Basic as they are for
     disk files. The record length for the device files is set to one.
 
   ■ Visual Basic sends only a carriage return at the end of a line. If the
     device requires a linefeed, the driver must provide it. When writing
     device drivers, keep in mind that other Visual Basic users will want to
     read and write control information. Writing and reading of device-
     control data is handled by the IOCTL statement and IOCTL$ function.
     See:
 
   ■ None of the Visual Basic devices directly supports binary mode. However,
     line printer devices can be opened in binary mode by adding the BIN
     keyword:
 
         OPEN "LPT1:BIN" FOR OUTPUT AS #1
 
   ■ Opening a printer in BIN mode eliminates printing a carriage return at
     the end of a line.
 
   ■ In input, random, and binary modes you can open a file under a different
     file number without first closing the file. In output or append mode,
     you must close a file before opening it with a different file number.
 
 Usage Notes (ISAM-specific)
   ■ When you open an ISAM table, the next record is the first record in
     the table and the current index is the NULL index.
 
   ■ Any ISAM operation that closes a table causes transactions to be
     committed. For example, if a type mismatch occurs while you are
     opening an ISAM table, the table is closed and a pending transaction
     is committed.
 
   ■ You may wish to code your programs so they first open all tables,
     then perform all transactions, then close tables. Make sure any
     operation that can close a table occurs outside a transaction.