Chapter 11 Homework Solutions
Transcript of Chapter 11 Homework Solutions
-
7/31/2019 Chapter 11 Homework Solutions
1/3
Chapter 11 Homework Solutions:
Chapter 11-Accessing Database Files
ANSWERS TO REVIEW QUESTIONS
1. Assume you have a database containing the names and phone numbers of yourfriends. Describe how the termsfile, table, row, column, record, field, andkey apply to
your database.
The names and phone numbers for your friends are stored in a table with rows (horizontal)
and columns (vertical). An Access database file can contain multiple related tables. Each
row in the table contains the information for one friend. Each row is called a record. The
information about the friends is separated into individual elements, called fields. In this
case, there are two elements in the friends database, name and phone number. Each of
these elements is called a field and is represented by the columns of the table. The key is
the unique field that is used to identify the record. The table is usually arranged by the key
field. In the friends database the key field would be the name field.
2. Explain the difference between a data control and a data-bound control.A Data control is created using the DataControl tool from the toolbox. A Data control
generally links one form with one table from a database. Using a data control is a two-step
process. First, you place a data control on a form and set the properties to link it to a
database file and table. Data-bound controls, such as text boxes and labels, display the
actual data from the field in the attached database table. Data-bound controls will
automatically display the contents of the bound field when the project runs.
3. Which controls can be data bound?The controls which can become data-bound (data aware controls) are labels, text boxes,
check boxes, list boxes, combo boxes, images, picture boxes, data-bound list boxes, data-
bound combo boxes and data-bound grids.
4. Explain how the BOF and EOF properties are set and how they might be used in a
project.
Two handy properties of the Recordset object are BOF (beginning of file) and EOF (end of
file). The BOF property is automatically set to True when the record pointer is before the
first record in the Recordset. This happens when the first record is current and the user
chooses MovePrevious. The BOF property is also true if the Recordset is empty (contains
no records).
The EOF property is similar to BOF; it is True when the record pointer moves beyond the
last record in the Recordset, and when the Recordset is empty.
When you are doing your own navigation in code, you need to check for BOF and EOF, so
that run-time errors dont occur.
5. Which properties must be set to bind a combo to a field in a database and display a
dropdown list of the choices for that field?
To bind a combo box to a field in a database you must set the Name and Text properties as
usual. The DataSource property should be changed to the name of the data control on the
current form that is attached to the database table. The DataField property must be set to
indicate the field of the table that will be attached to the combo box. The List property
must be updated to display the list of the choices for that field.
-
7/31/2019 Chapter 11 Homework Solutions
2/3
6. Which steps are needed to add a new record to a database?
To add a new record to a database you can use the data control's navigation buttons (rather
than your own code for navigation). Visual Basic will do the adds automatically if you set
the data control's EOFAction property to 2AddNew. When the user moves to the end of
the table and clicks the arrow for Next Record, an add operation begins. The data in all
bound controls is cleared so that new data can be entered. Then, when the user clicks oneof the arrow buttons, the Update method is automatically executed, and the new record is
written in the file.
Another approach for adding records is needed when navigation is accomplished with code
(rather than the navigation buttons on the data control). You can use a command button for
adding records. Use the Recordset's AddNew method in the click event for the command
button (example: datBooks.Recordset.AddNew). When this statement executes, all bound
controls are cleared so that the user can enter the data for the new record. After the data
fields are entered, the new record must be saved in the file. You can explicitly save it with
an Update method; or, if the user moves to another record, the Update method is
automatically executed. (example: datBooks.Recordset.Update).
7. Which steps are needed to delete a record from a database?The user should display the record to delete and click a Delete command button or menu
choice. The Delete method can then be used to delete the current record (example:
datBooks.Recordset.Delete). When a record is deleted, the current record is no longer valid.
Therefore, a Delete method must be followed by a MoveNext (or any other Move) method.
8. Which steps are needed to change the data in a database record?Updates (changes) for data that is displayed from a database will be handled automatically.
The Recordset object has an Update method, which you can use to save any changes in the
data. Most of the time, updating is automatic, since Visual Basic automatically executes
the Update method any time the user clicks one of the navigation buttons or one of the
Move methods executes. If you don't want the user to be able to change the data, you must
set the ReadOnly property of the data control to True. You can also keep the user from
making changes to the data by displaying fields in labels rather than text boxes.
9. How can you check for the user deleting the only record in a recordset?
When a record is deleted, the current record is no longer valid and the Delete method must
be followed by a MoveNext (or any other Move) method. If the record being deleted is the
last record in the table, a MoveNext causes an EOF condition. The program should then do
a MovePrevious. When the move is made to the previous record, a check must be made for
BOF just in case all of the records in the recordset have been deleted. If the recordset is
empty, a message box should display warning that all records have been deleted. Any
other desired action may then be taken for the empty recordset.
-
7/31/2019 Chapter 11 Homework Solutions
3/3