Your SlideShare is downloading. ×
Chapter10
Chapter10
Chapter10
Chapter10
Chapter10
Chapter10
Chapter10
Chapter10
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Chapter10

113

Published on

visual basic

visual basic

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
113
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. TYBSC (IT) Visual Basic Chapter-10 Data FilesQ. 1. What is the difference between a Visual Basic project file and a data file?( 4 mrks ) Visual Basic project file Data FileEach of our VB project file requires multiple Anything that we store on a hard disk is givenfiles for the forms , standard code modules, its own unique name and is called a Data file.and project modules.In this, some situations we require large The file is made up of records one record forquantities of data or information to be each entity in the file. Each record can beprocessed. broken down further into fieldsQ. 2 What occurs when open statement is executed ?( 4 rks ) We can use open statement for both i.e. sequential and random access files. Openstatement reserves file handle, also called a channel for reading from and writing to a file. Theopen statement associates number to the handle. A file handle is a unique path to fileassociated with a number from the open statement. Open statement associates a number tothe handle after that we can use the file number to access the file. Similarly, we have to specifythe 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. 1
  • 2. TYBSC (IT) Visual BasicQ. 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 numberof 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.txtTo refer to this file we can use this file number.Q. 5. Explain difference between the Output and Append modes.( 4 mrks ) Output Mode Append modeOpens a file for sequential output, starting It opens a file for sequential output, starting atfrom the beginning of the file. the end of the file. For Ex : For Ex:Private Sub Command_click() Private Sub Command_click()filenum = FreeFile Open “C: output.txt” filenum = FreeFile Open “C: output.txt”For Output As filenum For Append As filenumPrint #filenum , text1.text Print #filenum , text1.textClose filenum Close filenumEnd Sub End SubQ. 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 waswritten. 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 andfields and separated by, records are generally terminated a carriage written character. 2
  • 3. TYBSC (IT) Visual Basic 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 modeor append mode. For append mode, the file pointer is placed at the end of file. If there are norecords, the beginning of end of file. In output mode, the pointer is placed at the beginning of thefile. 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 , strAccountQ.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, withone or two data files, most programmer‟s assign # 1 and # 2.But in larger project selecting a filenumber can be a problem. If our code will be part of larger project with code from otherprogrammers and other libraries we must make same to avoid any conflicting file number. 3
  • 4. TYBSC (IT) Visual Basic To solve this problem we can allow the system to assign the next available file numberuse the FreeFile function to assign the next available file number to our file and we will neverhave conflict. Ex. Dim intFileNum as Integer intFileNum = FreeFile „ Get next available file number Open “Abc.txt” for output as # intFileNumQ. 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 errormight 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 errortrap 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 thenext lineof the code and bypass the line where the error has occured. Example: Private sub cmdDelete_Click( ) on Error Resume Next Stop Kill txtFile Kill statement delete the specified file in the textbox if err.Number then MsgBox " such a file doesnt exit " Endif End Sub ii.Resume (Label Name ):This statement is used , if you want to transfer executioncontrol.To some different location or you just want to ignore the error Example: 4
  • 5. TYBSC (IT) Visual Basic 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 theerror , In that caes above statement is used. Example: If your tries to access a floppy without putting it intothe disk drive , youapplication 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 erroeinformation this error object can be used to retrive the information about the type of error , whichhas occured the important properties of errorobject are as follows: 5
  • 6. TYBSC (IT) Visual Basic property Description 1.Number Identifies the specified error that occurs. 2.Description Take description of the error. 3.Source A String identifying the name of the object that aregenerated the errorQ.9. Explain the function and use of Err object.( 6 mrks ) Every time an error occurs VB creates an object, which contains error information. ThisErr object can be used to retrieve the information about the type of error which has occured. Properties of Err Object are as follows: Property Description Number Indentifies specified error that occur. Description Takes description of the error. Source A string identifying name of the object that has generated error. Methods of Err Object are as Follows: Method Description Clear Clears any information contain in the error object. Raise This method can be used to test the error handling code. We can use this method to generate run time errors.Q. 10 What are the difference between a random file and a sequential file? ( 6 mrks ) Random Access File Sequential Access File 6
  • 7. TYBSC (IT) Visual BasicRandom Access file is made up of identical Sequential Access files are accessed by linesize. Each record is made up of identical size. by line for applications that manipulate text files. Get and Put statements are used to access Input, Write statements are used to access therandom access file. sequential access file.For ex. For ex.Private Sub Form_Load() Private Sub Command1_Click()RecorLen = Len(product) fileNum= FreeFile OpenFileNum = FreeFile “C:output.txt” for Output As fileNumOpen “c:Product.dat” for Random as FileNum Print# fileNum , text1.textLen= RecordLen Close fileNum End SubQ. 11. What does updating a data file mean?( 4 mrks ) A file update generally consists of routine to add records, update records, deleterecords and browse through records. The procedures for these options might be selected from acommand button or a menu command. Each routine must display the fields from the file, so we will use one form and refer to itas the data form. This form contains text boxes for each field of a record along with theappropriate 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 aparticular record in a file. Syntax: Put # intFileNum , intRecordNum , variable Get # intFileNum , intRecordNum , variable Both of the above use RecordNum , which indicates RecordNumber ,youwant 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 7
  • 8. TYBSC (IT) Visual Basic Open" C: ABC.txt " for Random As # 1Len=5 for intCtr =1 to 5 Put #1, intCtr , intCtr Next intCtr Close#1 End Sub 8

×