Вы находитесь на странице: 1из 2

Chapter 10 Homework Solutions:

Chapter 10-Data Files ANSWERS TO REVIEW QUESTIONS 1. What is the difference between a Visual Basic project file and a data file? A Visual Basic project file (.vbp) is a small text file that holds the names of the other files in the project, as well as some information about the VB environment. Data files are different; they contain actual data, such as names and addresses, inventory amounts, account balances, and so on. Explain what occurs when an Open statement is executed. When a data file is opened, the following actions are taken. 1) The directory is checked for the named file. If the file does not exist, a directory entry is created for this file, with the exception of Input mode that will cause an error message if the file does not exist. 2) (Sequential files) A buffer is established in memory. This is simply setting aside an area of main storage. As the program instructs Visual Basic to write data on the disk, the data is actually placed in the buffer. Not until the buffer is filled is the data physically written to the disk. The size of the buffer for a random file is the number specified in the Len = clause of the Open statement. 3) A file pointer is created and set to the beginning of the file for all modes except Append, in which it is set to the end of the file. The pointer is always updated to indicate the current location in the file. 4) The file is given a file number for future reference. Each file used in a project must be assigned a unique number; however, the numbers need not begin with one. After a file is closed, the number may be reused for a different file. List and explain the file modes for data files. Output File Mode: Data is output from the project and written on the disk. New data is written at the beginning of the file, overwriting any existing data. Input: Data is input into the project from the disk. This mode will read data previously stored on the disk. Append: Data is output from the project and written on the disk. The new data is added to the end of the file. Random: Data can be input or output and records may be accessed in any order. What is the significance of a file number? When you open a file, you must assign it a file number. Multiple files can be open at the same time so it is important that each file must be assigned a unique file number. Files are accessed using one of the various file modes. The file modes use the file number (rather than the disk file name) when accessing the file. Explain the differences between the Output and Append modes. Both the Output and Append modes allow data to be output from the project. With the Output mode the new data will be written at the beginning of the file, overwriting any existing data. The Append mode will add new data to the end of the file.

2.

3.

4.

5.

6.

What is the format for the statements to read and write sequential files? Input #FileNumber, ListOfFields Write #FileNumber, ListOfFields Each file is assigned a unique file number when it is opened. The list of fields may be string expressions, numeric expressions, or both and may be separated by commas or semicolons. What function can be used to determine an available file number? Use the FreeFile function to assign the next available file number to your file, and you will never have any conflicting file numbers in your project. When would an On Error statement be used? You must place an On Error statement at the beginning of any procedure where errors are expected, such as before opening a sequential file for input. Explain the function and use of the Err object. The Err object holds information about an error that has occurred. You can check the properties of the Err object to determine the error number and a description of the error. The name of the object or application that caused the error is stored in the Source property. The Number property contains the error number (ranging from 0 to 65,535), which is described in the Description property. Explain the differences between a random file and a sequential file. The primary difference between sequential files and random files is that you may read and write the data in any order in a random file. With sequential files, it is always necessary to start at the beginning of the file and proceed in order through the file. Random files offer greater speed as well as the capability for random access. Each record in a random file is exactly the same size. The fields within the record are fixed in length and position. Sequential files have variable length fields and records. Before reading or writing a random file, the record structure or layout must be defined. The Type/End Type statements are used to set up record structures. The only modification needed is to use fixed length strings. What does updating a data file mean? A file update generally consists of routines to add records, edit records, delete records, and browse through records. Give examples for using the InputBox function. The InputBox function provides a quick and easy way to pop up a new form where you can display a message, called a prompt, and you can allow the user to type input into a text box. The following example could be used when requesting the users phone number: strPhoneNumber =InputBox(Please enter your phone number) The following example could be used when requesting an employees number of hours worked: intLaborHours = InputBox (How many hours did you work last month?),(Input Requested)

7.

8.

9.

10.

11.

12.

Вам также может понравиться