# open statement

The open statement opens a specified
file name and associates the file with the optional *file.var*.

## Syntax

```
open {"{dict}",} file.ref{to file.var} {then|else statement.block}
```

## Parameter(s)

| dict | Specifies opening the dictionary level of the specified file.var and assigning it to file.var. The optional dict can be expressed as part of the file name as dict file.var. |
| --- | --- |
| file.ref | File to open and associate with the file specified in file.var. If no file.var is specified, the file is opened and assigned to the default file variable. Note: For BASIC: The open statement try to open the requested file in the VME. For FlashBASIC: The open statement will first try to open the requested file in the FSI, then try to open the requested file in the VME. |
| file.var | File to associate with the opened file specified in file.var. For Windows: If the file variable specified is already assigned to a file, the file is closed and the new file is then assigned to the variable. Warning: Copying file variables can be dangerous since a subsequent close on any of the file variables will close the file. Thus, any remaining file variables (containing a copy of the now invalid file handle) may be incorrectly allocated to the wrong file. |

## Description

If the file is opened successfully,
the then clause, if present, is executed. If the
file is not opened successfully, the else clause,
if present, is executed. If the file cannot be opened and no then/else clause
is specified, the program terminates with error message [201].

**For Windows:** When referencing a file in an open statement, if there is no explicit path, the following
rules are applied to determine the current account:

- If the file access is performed by a VME user, the current account is the account where the VME user is logged on.

- If the file access is performed by a non-VME user (for example, VB or remote VME), the current account is the account in which the file is located.

In triggers and CALL correlatives, these rules apply only
to VME users accessing a file on their local computer. Otherwise,
the full path is required in all open statements.

Note: This means that FlashBASIC programs opening the DM account
will open the FSI:DM account. Conversely, non-Flashed programs will
open the VME:DM account. To change this behavior, use the OSFI prefixes
explicitely.

The environment variable @ACCOUNT is set to the account where the file is located, not the account
where the user is. @LOGNAME is set to the logged
account if run from a VME and to "" (null string)
if not.

- For regular FlashBASIC modules run from a VME (not a trigger or a CALL A-Correlative), the current account is the usual.

- For FlashBASIC or BASIC modules run from the D3 File Manager, the current account is the account where the file is located. @ACCOUNT and @LOGNAME both reflect this account.

- For FlashBASIC modules run from MS-DOS, the current account is the one specified by the -D command line switch. @ACCOUNT and @LOGNAME both reflect this account.

Note: A process may only have 32767 files open at once per execute
level. The same file opened twice without an intervening CLOSE counts as two open files.

## Example(s)

The example below opens the data
area of the customer file to the file variable `customer.file`. If the customer file does not exist, it stops and prints error
message [201].

```
open ’customer’ to customer.file else stop
201,’customer file cannot be opened’
```
The example below opens the dictionary of the invoice
file. If successful, it prints `ok`. If not, it stops
and prints error message [201], passing the string `dict invoice`.

```
open ’dict’,’invoice’ to invoice.dict then
print ’ok’
end else
stop 201,’dict invoice’
end
```
 This results in the display:

```
[201] ’dict invoices’ is not a file name
```
The example below demonstrates how the to, then, and else are all optional
in an open statement. Note that *file.var* was left off of the read statement.

```
open "invoice.control"
readv next.invoice-ID from "invoice.counter",1
```

## See also

- [abort statement](https://d3codex.com/pickbasic-flashbasic/abort-statement/)
- [clear-file command](https://d3codex.com/tcl/clear-file-command/)
- [clearfile statement](https://d3codex.com/pickbasic-flashbasic/clearfile-statement/)
- [close statement](https://d3codex.com/pickbasic-flashbasic/close-statement/)
- [Default File Variables](https://d3codex.com/pickbasic-flashbasic/default-file-variables/)
- [error statement](https://d3codex.com/pickbasic-flashbasic/error-statement/)
- [File control block](https://d3codex.com/definitions/file-control-block/)
- [clearfile statement](https://d3codex.com/pickbasic-flashbasic/clearfile-statement/)
- [File variable](https://d3codex.com/pickbasic-flashbasic/file-variable/)
- [if statement](https://d3codex.com/pickbasic-flashbasic/if-statement/)
- [read statement](https://d3codex.com/pickbasic-flashbasic/read-statement/)
- [readnext statement](https://d3codex.com/pickbasic-flashbasic/readnext-statement/)
- [statement blocks](https://d3codex.com/pickbasic-flashbasic/statement-blocks/)
- [Statements and functions](https://d3codex.com/pickbasic-flashbasic/statements-and-functions/)
- [stop statement](https://d3codex.com/pickbasic-flashbasic/stop-statement/)
- [then clause](https://d3codex.com/pickbasic-flashbasic/then-clause/)
- [then/else statement blocks](https://d3codex.com/pickbasic-flashbasic/then-else-statement-blocks/)
- [u21a3 user exit](https://d3codex.com/pickbasic-flashbasic/u21a3-user-exit/)

---
Source: https://d3codex.com/pickbasic-flashbasic/open-statement/ - part of the D3Codex reference.
