Chapter-10 Data Files: TYBSC (IT) Visual Basic
Chapter-10 Data Files: TYBSC (IT) Visual Basic
Q. 1. What is the difference between a Visual Basic project file and a data file?( 4 mrks ) Visual Basic project file Each of our VB project file requires multiple files for the forms , standard code modules, and project modules. In this, some situations we require large quantities of data or information to be processed. Data File Anything that we store on a hard disk is given its own unique name and is called a Data file.
The file is made up of records one record for each entity in the file. Each record can be broken down further into fields
Q. 2 What occurs when open statement is executed ?( 4 rks ) We can use open statement for both i.e. sequential and random access files. Open statement reserves file handle, also called a channel for reading from and writing to a file. The open statement associates number to the handle. A file handle is a unique path to file associated with a number from the open statement. Open statement associates a number to the handle after that we can use the file number to access the file. Similarly, we have to specify the mode in which we want to open the file. These mode can be read , write or both. Q. 3. List and explain the file modes for data files.(6 mrks ) File modes for data files are as follows: i) Append: Opens a file for sequential output starting at the end of file. ii) Input: Opens a file for sequential input, we can read from the file. iii) Output: Opens a file for sequential output, starting from beginning of file. iv) Random: Open a file for random read and write access. This mode allows to read and write to a file at any specified record boundaries.
Q. 4. What is the significance of file number?( 4 mrks ) A file number is used to maintain the files. In the project suppose we have large number of files , so instead of using the various file names we refer to the file numbers. Syntax is as follows: Open filename # 1 For ex. Open Abc.txt # 1 Above statement opens the file Abc.txt. It associates a file number one to abc.txt To refer to this file we can use this file number.
Output Mode Opens a file for sequential output, starting from the beginning of the file. For Ex : Private Sub Command_click() filenum = FreeFile Open C:\ output.txt For Output As filenum Print #filenum , text1.text Close filenum End Sub
Append mode It opens a file for sequential output, starting at the end of the file. For Ex: Private Sub Command_click() filenum = FreeFile Open C:\ output.txt For Append As filenum Print #filenum , text1.text Close filenum End Sub
Q. 6. What is the format for the statements to read and write sequential files?( 6 mrks ) Sequential file contains data elements that are stored one after another in sequence. When we read the data from the disk, it must be read in the same sequence in which it was written. To read any particular element of data, all proceeding must first be read. As data elements are written on disk, string fields are enclosed in quotation marks and fields and separated by, records are generally terminated a carriage written character.
Input statement is used to read the data from the a sequential file. Syntax is : Input # intFileNumber , variable1 , variable2 , Input statement requires file number and variables to hold the data. For ex. Private Sub cmdRead_click() Dim intV1, intV2, intV3, as Integer Open c:/seg.txt for input as # 1 Input # 1 intV1, intV2, intV3 Close # 1 MsgBox Seg.txt Contains &intV1, &intV2, &intV3 End Sub
For writing purpose we use the write # statement to place data into a sequential data file. Before the write # statements can be executed , the file must be opened in either output mode or append mode. For append mode, the file pointer is placed at the end of file. If there are no records, the beginning of end of file. In output mode, the pointer is placed at the beginning of the file. Use output mode to create a new file ot write old data. Syntax is : Write # file number , list of fields Ex. Write # 1 , txtAmount.text , txtDescription.text , txtPrice.text Write # 2 , strAccount
Q.7 . What function can be used to determine an available file number?( 4 mrks ) When we open a file, we must assign a file number to each file. For a small project, with one or two data files, most programmers assign # 1 and # 2.But in larger project selecting a file number can be a problem. If our code will be part of larger project with code from other programmers and other libraries we must make same to avoid any conflicting file number.
To solve this problem we can allow the system to assign the next available file number use the FreeFile function to assign the next available file number to our file and we will never have conflict.
Ex. Dim intFileNum as Integer intFileNum = FreeFile Get next available file number Open Abc.txt for output as # intFileNum
Q. 8. When would an On Error statement be used?( 6 mrks ) We must place an On Error statement at the beginning of the any procedure where error might occur, such as before opening sequential file for input. An on error statement will trap the error and it will direct the execution control to the error trap level. Actually , there are four different statements that we can use when error occurs. i.Resume Next:It tells VB that if an error occurs,the program should simply skip to the next lineof the code and bypass the line where the error has occured. Example: Private sub cmdDelete_Click( ) on Error Resume Next Stop Kill txtFile if err.Number 'Kill statement delete the specified file in the textbox then
MsgBox " such a file doesn't exit " Endif End Sub
ii.Resume (Label Name ):This statement is used , if you want to transfer execution control.To some different location or you just want to ignore the error Example:
private sub cmdOpen_Click( ) OnError GoTo ErrorTrap set db = OpenDatabase("C:\emp.mdb") Normal Exit: Exit Sub Error Trap: Msgbox " Failed to open the file " Resume NormalExit End sub iii.Resume :When Error occurs and if you want to give the user a chance to fix the error , In that caes above statement is used. Example: If your tries to access a floppy without putting it intothe disk drive , you application may ask you to insert it and then continue with the execution again. private sub cmdResume_Click( ) On Error Go To ErrorTrap MsgBox" Length of the file is " & FileLen( txtFile ) Normal Exit: Exit sub Error Trap: If err.Number then If MsgBox (" Floppy drive error try again ") = vbYes then Resume Endif End Sub iv.The errObject:Every time an error occurs vb creates an object, which contain erroe information this error object can be used to retrive the information about the type of error , which has occured the important properties of errorobject are as follows:
Description Identifies the specified error that occurs. Take description of the error. A String identifying the name of the object that are
Q.9. Explain the function and use of Err object.( 6 mrks ) Every time an error occurs VB creates an object, which contains error information. This Err object can be used to retrieve the information about the type of error which has occured.
Description Indentifies specified error that occur. Takes description of the error. A string identifying name of the object that has generated error.
Methods of Err Object are as Follows: Method Clear Description Clears any information contain in the error object. This method can be used to test the error handling code. We can use this method to generate run time errors.
Raise
Q. 10 What are the difference between a random file and a sequential file? ( 6 mrks )
Random Access file is made up of identical size. Each record is made up of identical size.
Sequential Access files are accessed by line by line for applications that manipulate text files. Input, Write statements are used to access the sequential access file. For ex. Private Sub Command1_Click() fileNum= FreeFile Open C:\output.txt for Output As fileNum Print# fileNum , text1.text Close fileNum End Sub
Get and Put statements are used to access random access file. For ex. Private Sub Form_Load() RecorLen = Len(product) FileNum = FreeFile Open c:\Product.dat for Random as FileNum Len= RecordLen
Q. 11. What does updating a data file mean?( 4 mrks ) A file update generally consists of routine to add records, update records, delete records and browse through records. The procedures for these options might be selected from a command button or a menu command. Each routine must display the fields from the file, so we will use one form and refer to it as the data form. This form contains text boxes for each field of a record along with the appropriate labels. Q.12 Explain Get statement and put statement.( 4 mrks ) This two statement are used to access Random Access Files. Get is used to read particular record from a file.Similary,Put is used to write a particular record in a file. Syntax: Put # intFileNum , intRecordNum , variable Get # intFileNum , intRecordNum , variable Both of the above use RecordNum , which indicates RecordNumber ,you want to works with the variable can be of any datatype , it can be array or userdefine datatype. Example: Private sub cmdWrite_Click( ) Dim Ctr As integer
Open" C:\ ABC.txt " for Random As # 1Len=5 for intCtr =1 to 5 Put #1, intCtr , intCtr Next intCtr Close#1 End Sub