SlideShare a Scribd company logo
TYBSC (IT) Visual Basic


                                                  Chapter-10
                                                  Data Files



Q. 1. What is the difference between a Visual Basic project file and a data file?( 4 mrks )

              Visual Basic project file                                       Data File

Each of our VB project file requires multiple              Anything that we store on a hard disk is given
files 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 for
quantities of data or information to be                    each entity in the file. Each record can be
processed.                                                 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.



                                                       1
TYBSC (IT) Visual Basic


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.



Q. 5. Explain difference between the Output and Append modes.( 4 mrks )



                   Output Mode                                       Append mode

Opens a file for sequential output, starting        It opens a file for sequential output, starting at
from 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 filenum
Print #filenum , text1.text                         Print #filenum , text1.text
Close filenum                                       Close filenum
End Sub                                             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.

                                                2
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 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 programmer‟s 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.


                                                    3
TYBSC (IT) Visual Basic


       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    'Kill statement delete the specified file in the textbox

                       if err.Number     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:

                                                   4
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 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:


                                                  5
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 are
generated the error

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.



       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
TYBSC (IT) Visual Basic


Random Access file is made up of identical            Sequential Access files are accessed by line
size. 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 the
random access file.                                   sequential access file.

For ex.                                               For ex.
Private Sub Form_Load()                               Private Sub Command1_Click()
RecorLen = Len(product)                               fileNum= FreeFile                Open
FileNum = FreeFile                                    “C:output.txt” for Output As fileNum
Open “c:Product.dat” for Random as FileNum           Print# fileNum , text1.text
Len= RecordLen                                        Close fileNum
                                                      End Sub



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


                                                  7
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

More Related Content

PPT
Linux basics
sirmanohar
 
PDF
File handling in Python
BMS Institute of Technology and Management
 
PPT
File handling(some slides only)
Kamlesh Nishad
 
DOCX
Unit 5 dwqb ans
Sowri Rajan
 
PPTX
basics of file handling
pinkpreet_kaur
 
PDF
Python - Lecture 8
Ravi Kiran Khareedi
 
PPTX
COM1407: File Processing
Hemantha Kulathilake
 
PDF
Mc7404 np final
moorthy muppidathi
 
Linux basics
sirmanohar
 
File handling(some slides only)
Kamlesh Nishad
 
Unit 5 dwqb ans
Sowri Rajan
 
basics of file handling
pinkpreet_kaur
 
Python - Lecture 8
Ravi Kiran Khareedi
 
COM1407: File Processing
Hemantha Kulathilake
 
Mc7404 np final
moorthy muppidathi
 

What's hot (20)

PDF
File and directories in python
Lifna C.S
 
PDF
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
PDF
Python File Handling | File Operations in Python | Learn python programming |...
Edureka!
 
PPTX
File handling in Python
Megha V
 
PPT
Unit5 C
arnold 7490
 
PPTX
File Handling and Command Line Arguments in C
Mahendra Yadav
 
DOCX
Linux 系統程式--第一章 i/o 函式
艾鍗科技
 
DOCX
Filehandling
Muhammad Fahad
 
DOCX
python file handling
jhona2z
 
PPT
Filehandlinging cp2
Tanmay Baranwal
 
PDF
Python-files
Krishna Nanda
 
PPT
17 files and streams
Docent Education
 
PDF
Python - File operations & Data parsing
Felix Z. Hoffmann
 
PPTX
Data file handling in c++
Vineeta Garg
 
PDF
File handling and Dictionaries in python
nitamhaske
 
PPTX
Chapter 08 data file handling
Praveen M Jigajinni
 
PPTX
Functions in python
Santosh Verma
 
PPT
File handling in_c
sanya6900
 
File and directories in python
Lifna C.S
 
Python programming : Files
Emertxe Information Technologies Pvt Ltd
 
Python File Handling | File Operations in Python | Learn python programming |...
Edureka!
 
File handling in Python
Megha V
 
Unit5 C
arnold 7490
 
File Handling and Command Line Arguments in C
Mahendra Yadav
 
Linux 系統程式--第一章 i/o 函式
艾鍗科技
 
Filehandling
Muhammad Fahad
 
python file handling
jhona2z
 
Filehandlinging cp2
Tanmay Baranwal
 
Python-files
Krishna Nanda
 
17 files and streams
Docent Education
 
Python - File operations & Data parsing
Felix Z. Hoffmann
 
Data file handling in c++
Vineeta Garg
 
File handling and Dictionaries in python
nitamhaske
 
Chapter 08 data file handling
Praveen M Jigajinni
 
Functions in python
Santosh Verma
 
File handling in_c
sanya6900
 
Ad

Viewers also liked (17)

PPS
Sand Stone
Mahmood Mufti
 
PPT
Service
Alice JamSky
 
PPTX
Rapunzel created yessy ernidha sharry
Yesi Sari
 
DOCX
Rev kkn
Arie Azizi
 
PDF
Mitaka lab cover
Mitaka Lab
 
PPTX
RAPUNZEL
Yesi Sari
 
PDF
2012 ARQUO Marketplace
arquo_account
 
PDF
ZGarci
ZGar
 
PPTX
Tools
Alice JamSky
 
PDF
12 2007-qd-btnmt
lehung0521
 
PPT
Strategy of Emperer Saga & Kukai on Kyoto - Daikakuji-temple, Osawanoike-pond
Tsukasa Kawahara
 
PPTX
Slidesharetest
성규 어
 
PDF
Korona.POS Cloud Presentation
Hiawatha Colbert
 
PDF
Genius Overview
Hiawatha Colbert
 
PPTX
Sebi
Daiju Alex
 
PPTX
Practicum Presentation
radfatoma
 
PDF
เศรษฐกิจพอเพียง เรื่อง ฝายกั้นน้ำ
maytakul
 
Sand Stone
Mahmood Mufti
 
Service
Alice JamSky
 
Rapunzel created yessy ernidha sharry
Yesi Sari
 
Rev kkn
Arie Azizi
 
Mitaka lab cover
Mitaka Lab
 
RAPUNZEL
Yesi Sari
 
2012 ARQUO Marketplace
arquo_account
 
ZGarci
ZGar
 
12 2007-qd-btnmt
lehung0521
 
Strategy of Emperer Saga & Kukai on Kyoto - Daikakuji-temple, Osawanoike-pond
Tsukasa Kawahara
 
Slidesharetest
성규 어
 
Korona.POS Cloud Presentation
Hiawatha Colbert
 
Genius Overview
Hiawatha Colbert
 
Practicum Presentation
radfatoma
 
เศรษฐกิจพอเพียง เรื่อง ฝายกั้นน้ำ
maytakul
 
Ad

Similar to Chapter10 (20)

PPTX
Filehandling
Amandeep Kaur
 
PDF
Smiley011
kishorshelke27
 
PDF
Vba functions
Wongyu Choe
 
PDF
Com ed 5 final
bluejayjunior
 
PPT
File handling
SabahtHussein
 
PPTX
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Suraj Kumar
 
PPT
Chapter 11
Terry Yoast
 
PPT
Vb introduction.
sagaroceanic11
 
DOC
Ps4 lesson 1
erwinparamio
 
DOCX
Visual basic bt0082
Divyam Pateriya
 
PPT
Ms vb
sirjade4
 
PPTX
Presentation on visual basic 6 (vb6)
pbarasia
 
DOCX
VBS control structures for if do whilw.docx
Ramakrishna Reddy Bijjam
 
PDF
Vb6 ch.7-3 cci
Fahim Khan
 
PPTX
9 Files
Kwan Lee
 
PDF
Vb6 ch.8-3 cci
Fahim Khan
 
PDF
Delphi L05 Files and Dialogs
Mohammad Shaker
 
PDF
Ppt on visual basics
younganand
 
Filehandling
Amandeep Kaur
 
Smiley011
kishorshelke27
 
Vba functions
Wongyu Choe
 
Com ed 5 final
bluejayjunior
 
File handling
SabahtHussein
 
VISUAL BASIC 6 - CONTROLS AND DECLARATIONS
Suraj Kumar
 
Chapter 11
Terry Yoast
 
Vb introduction.
sagaroceanic11
 
Ps4 lesson 1
erwinparamio
 
Visual basic bt0082
Divyam Pateriya
 
Ms vb
sirjade4
 
Presentation on visual basic 6 (vb6)
pbarasia
 
VBS control structures for if do whilw.docx
Ramakrishna Reddy Bijjam
 
Vb6 ch.7-3 cci
Fahim Khan
 
9 Files
Kwan Lee
 
Vb6 ch.8-3 cci
Fahim Khan
 
Delphi L05 Files and Dialogs
Mohammad Shaker
 
Ppt on visual basics
younganand
 

Chapter10

  • 1. TYBSC (IT) Visual Basic Chapter-10 Data Files Q. 1. What is the difference between a Visual Basic project file and a data file?( 4 mrks ) Visual Basic project file Data File Each of our VB project file requires multiple Anything that we store on a hard disk is given files 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 for quantities of data or information to be each entity in the file. Each record can be processed. 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. 1
  • 2. TYBSC (IT) Visual Basic 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. Q. 5. Explain difference between the Output and Append modes.( 4 mrks ) Output Mode Append mode Opens a file for sequential output, starting It opens a file for sequential output, starting at from 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 filenum Print #filenum , text1.text Print #filenum , text1.text Close filenum Close filenum End Sub 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. 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 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 programmer‟s 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. 3
  • 4. TYBSC (IT) Visual Basic 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 'Kill statement delete the specified file in the textbox if err.Number 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: 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 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: 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 are generated the error 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. 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 Basic Random Access file is made up of identical Sequential Access files are accessed by line size. 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 the random access file. sequential access file. For ex. For ex. Private Sub Form_Load() Private Sub Command1_Click() RecorLen = Len(product) fileNum= FreeFile Open FileNum = FreeFile “C:output.txt” for Output As fileNum Open “c:Product.dat” for Random as FileNum Print# fileNum , text1.text Len= RecordLen Close fileNum End Sub 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 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