The process of using a file: – A file is used to input as well as output data and show that it is also called data file. There are always three steps that must be taken when a file is used by an application.
- (i) The file must be opened. If the file does not exist, opening it means creating it.
- (ii) Data is either written to the file or read from the file.
- (iii) When the application is finished using the file, the file is closed.
When a visual basic application is actively working with data, the data is located in random access memory, usually in variable and/or control properties. When data is written into file, it is copied from the variables or control properties. When data is read from a file, it is copied from the file into variables and/or control properties.
Open data file: – Before using a data file either for input or for output, it must be opened by the user. A file can be open in a proper mode. The open statement used in visual basic to open a data file. Syntax of the open statement is –
Open <“File Name”> For <File Mode> As <#File Number> [LEN=Record Length]
The open statement contains of multiple parts. These parts are use in different purposes. Function of these part are as fallows –
(1) File name: – This part contains name of actual file. The file name is the name of the file on the disk. The name can include the path to the file as well. For example, in file name “A:\Customer.dat”, the A: if we do not specify a path, visual basic will assume that the files location is the folder containing the application. If we use the string literal for the file name, it must be enclosed in a quotation marks sign. We may also use a string variable for this argument.
(2) Mode: – This is used for specify a mode of operation that will be performed with the file and may be one of the following keywords such as input, output, append, random or binary.
The keyword input used with a sequential input file. Data will read from the file. When an opening a file in input mode, the file must already exist otherwise an error occur, output mode is used with a sequential output file data will be written to the file if the file does not exist, it is created. If the file is already existing, it will be overwritten. Append mode is used for sequential output file. Data will be written to end of the file. If the file does not exist, it is created. If it is already existing, its contains are not erased. Random file used for random input, random output or random input/output access. If the file does not exist, it is created. The binary file is opened in binary mode. If the file does not exist, it is created.
(3) #File Number: – In this part, # used with a number in the range of 1 through 511 that is assigned to the file. Only unique number can be use with a file number. Maximum of 511 files can be opened at a time in any application.
(4) LEN=Record length: – This component is optional for sequential access file but mandatory for random access file. This is used for specify length of a record.
For example, if we want to open a data file in input mode for import data from that file, we can use open statement in the following format.
Open “Customer.dat” For Input As #1
Here Customer.dat is a name of file which is open in input mode. Key number #1 is used for input data from that existing file.
Closing a file: – The opposite of opening a file is closing it. The close statement is used to close one or more file at a time with a file number. This file number must be exactly same as with open statement. syntax of the close statement is –
Close <File Number>[File Number, File Number, _ _ _ ]
Writing data to a file: – Data may be written to a file that is opened in either out or append mode. The write statement is used to write data to an open output file. Syntax of the write statement is –
Write <File Number>,<Item List1>[Item List2,Item List3, _ _ _ ]
Here, write statement uses two components such as file number and item list. File number is a number prefix with # and it must be exactly same as open statement. Item list is one or more item to we written to the file. The items may be numbers or string. Multiple items must be separated with commas. The angular bracket indicates that at least one item is mandatory and square bracket indicate that more than one items are optional.
Reading data from file: – Data may be read from a file that already exist and opened in input mode. The input statement is used to read data from opened input file. If file is not opened, input statement return an error message. Syntax of the input message is –
Input <File Number>,<Item list1>[Item list2,Item list3, _ _ _ ]
EOF: – The EOF function used in visual basic programming for end of file testing. This function continues the loop until end of file encountered. Syntax of EOF function is –
EOF (File Number)