Sunteți pe pagina 1din 3

TYBSC (IT) Visual Basic

Chapter 11. Accessing Data files


Q1. Assume you have database containing the names and phone numbers of your
friends. Describe how the terms file, table, row, column, record, field and key apply to
database.
Ans:
1. For maintaining the names and phone numbers we required a Database File. This can
be Sql, Fox Pro, Microsoft Access file (extension .mdb). Example phonenumbers.mdb.
2. This database file contains multiple Tables which stores Names and Phone numbers.
Each table can be viewed as spreadsheet with rows and columns.
3. Each Row in a table represents one single item or a record.
4. Each Column is used to store element of data. Like in this example columns can be
Names, Phone Numbers.
5. The element which represents columns are called as Fileds.
6. Most of the table use a Key Field to identify each record. The key field is often a id or
name etc. in this case name key field can be Names field.
Q2. Explain deference between a data control and data-bound control.( 6 mrks )
Ans:
Data control

Data-bound

Using 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

You create the controls, such as labels


and text boxes, to display the actual data.
Each control is a bound to particular field
in the table. In this example the label is
called a data bound control and
automatically displays the contents of
bound field when the project runs.

Data control generally links one form with


one table.

If you want to have data-bound controls


on second form, you must place a data
control on that form.

Prefix of data control is dat

For data-bound control prefix depends


upon the control which you are using.

Data control to work you need to set some


properties to connect with database like
connect property, database name
property, record source property.

To display data on the data-bound control


that you are using like labels or textboxes.
You need set its data source property and
data field name which is column name
form the table.

Q3. Which controls can be data bound?(4 mrks )


Ans:
1. You can bound data to data bound controls like Labels, Text Boxes, Checkbox. If you
dont want user to change the data you use Label control, where user can only view the
data else you can use Text boxes.
2. If you want to display data in the form of True or False you can have Check Box control.

TYBSC (IT) Visual Basic

Q4. Explain how BOF and EOF properties are set and how they might be used in a
project.( 6 mrks )
Ans:
1. Two handy properties of the record set 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 record set. This condition happens when first record is current and the user
choose MovePrevious. The BOF property is also true if the record set is empty
2. The EOF property is similar to BOF; it is true when the record pointer moves beyond the
last record in the record set and when the record set is empty.
3. When you are doing your own navigation in code, you need to check for BOF and EOF
so that run-time errors do not occur. If the user clicks MoveNext when on the last record,
what do you to do? Have the program cancel with a run time error? Display a message?
Wrap around the first record? Keep the record pointer to last record?
4. In the following example, we will wrap-around method. If the user clicks on the
MoveNext button from the end of the table, the first record becomes active record.
Private Sub cmdNext_Click()
Move to Next record
datBooks.Recordset.MoveNext
If datBooks.Recordset.EOF Then
datBooks.Recordset.MoveFirst
End If
End Sub
Q6. What are the steps needed to add a new record to database?( 4 mrks )
Ans:
1. When you want to add new record you have couple of choices. If you are using data
controls navigation buttons, you can allow Visual Basic to do the adds automatically. Set
the data controls EOF action property to 2 AddNew. When the user moves to end of
the file and clicks the arrow buttons, the Update method is automatically executed and
the new record is added.
2. You need a different approach when you use code to accomplish record navigation.
Assume that you have a command button to menu choice to add a new record. In the
click event for the command button use AddNew method:
3. datBooks.Recordset.AddNew
4. 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, it the user moves
to anther record, the update method is automatically executed.
5. You may want to use two buttons for adding a new record an Add button and s save
button. For the Add button, use an AddNew method; for the Save button, use the Update
method.
6. When adding new records, some conditions can cause errors to occur.
7. For example, if the key field is blank on a New record, a run time error holts the program
execution.
Q7. What are the steps needed to delete a record from database?( 4 mrks )
Ans:
1. The delete method deletes current record. The user should display the record to delete
and click a Delete command button or menu choice.
2. When a record is deleted, the current record is no longer valid. Therefore, a delete
method must be followed by MoveNext method.

TYBSC (IT) Visual Basic

With datBooks.Recordset
.Delete
.MoveNext
End With
3. But what if the record being deleted is the last record in the table?
4. Remember that if the navigation buttons are used to navigate, moving beyond EOF just
resets the current record to the last record.
Q8. What steps are needed to change the data in a database?( 4 mrks )
Ans:
The record set object has an Update method, which you can use to save any changes in the
data. Most of the time, updating is automatically executes the Update method any time the user
clicks one of the navigation buttons or one the Move methods executes.

S-ar putea să vă placă și