Saturday, December 26, 2009

Bound Data Tools

Most of the Visual Basic tools we’ve studied can be used as bound, or data-aware, tools (or controls). That means, certain tool properties can be tied to a particular database field. To use a bound control, one or more data controls must be on the form.
• Some bound data tools are:
Label - Can be used to provide display-only access to a specified text data field.
Text Box - Can be used to provide read/write access to a specified text data field. Probably, the most widely used data bound tool.
Check Box - Used to provide read/write access to a Boolean field.
Combo Box - Can be used to provide read/write access to a text data field.
List Box - Can be used to provide read/write access to a text data field.
Picture Box - Used to display a graphical image from a bitmap, icon, or metafile on your form. Provides read/write access to a image/binary data field.
Image Box - Used to display a graphical image from a bitmap, icon, or metafile on your form (uses fewer resources than a picture box). Provides read/write access to a image/binary data field.
• There are also three ‘custom’ data aware tools, the DataCombo (better than using the bound combo box), DataList (better than the bound list box), and DataGrid tools,
• Bound Tool Properties:
DataChanged - Indicates whether a value displayed in a bound control has changed.
DataField - Specifies the name of a field in the table pointed to by the respective data control.
DataSource - Specifies which data control the control is bound to.
If the data in a data-aware control is changed and then the user changes focus to another control or tool, the database will automatically be updated with the new data (assuming LockType is set to allow an update).
• To make using bound controls easy, follow these steps (in order listed) in placing the controls on a form:
1. Draw the bound control on the same form as the data control to which it will be bound.
2. Set the DataSource property. Click on the drop-down arrow to list the data controls on your form. Choose one.
3. Set the DataField property. Click on the drop-down arrow to list the fields associated with the selected data control records. Make your choice.
4. Set all other properties, as required.
By following these steps in order, we avoid potential data access errors.

The relationships between the bound data control and the data control are:







1. Start a new application. We’ll develop a form where we can skim through the books database, examining titles and ISBN values. Place an ADO data control, two label boxes, and two text boxes on the form.
2. If you haven’t done so, create a data link for the BIBLIO.MDB database following the steps given under Data Links in these notes.
3. Set the following properties for each control. For the data control and the two text boxes, make sure you set the properties in the order given.
Form1:
BorderStyle - 1-Fixed Single
Caption - Books Database
Name - frmBooks
Adodc1:
Caption - Book Titles
ConnectionString - BIBLIO.UDL (in whatever folder you saved it in - select, don’t type)
RecordSource - SELECT * FROM Titles
Name - dtaTitles
Label1:
Caption - Title
Label2:
Caption - ISBN
Text1:
DataSource - dtaTitles (select, don’t type)
DataField - Title (select, don’t type)
Locked - True
MultiLine - True
Name - txtTitle
Text - [Blank]
Text2:
DataSource - dtaTitles (select, don’t type)
DataField - ISBN (select, don’t type)
Locked - True
Name - txtISBN
Text - [Blank]
When done, the form will look something like this (try to space your controls as shown; we’ll use all the blank space as we continue with this example):





Save the application. Run the application. Cycle through the various book titles using the data control. Did you notice something? You didn’t have to write one line of Visual Basic code! This indicates the power behind the data tool and bound tools.

In addition to using the data control to move through database records, we can write Visual Basic code to accomplish the same, and other, tasks. This is referred to as programmatic control. In fact, many times the data control Visible property is set to False and all data manipulations are performed in code. We can also use programmatic control to find certain records.
• There are four methods used for moving in a database. These methods replicate the capabilities of the four arrow buttons on the data control:
MoveFirst - Move to the first record in the table.
MoveLast - Move to the last record in the table.
MoveNext - Move to the next record (with respect to the current record) in the table.
MovePrevious - Move to the previous record (with respect to the current record) in the table.
• When moving about the database programmatically, we need to test the BOF (beginning of file) and EOF (end of file) properties. The BOF property is True when the current record is positioned before any data. The EOF property is True when the current record has been positioned past the end of the data. If either property is True, the current record is invalid. If both properties are True, then there is no data in the database table at all.
• These properties, and the programmatic control methods, operate on the Recordset property of the data control. Hence, to move to the first record in a table attached to a data control named dtaExample, the syntax is:
dtaExample.Recordset.MoveFirst
• There is a method used for searching a database:
Find - Find a record that meets the specified search criteria.
This method also operates on the Recordset property and has three arguments we will be concerned with. To use Find with a data control named dtaExample:
dtaExample.Recordset.Find Criteria,NumberSkipped,SearchDirection
• The search Criteria is a string expression like a WHERE clause in SQL. We won’t go into much detail on such criteria here. Simply put, the criteria describes what particular records it wants to look at. For example, using our book database, if we want to look at books with titles (the Title field) beginning with S, we would use:
Criteria = “Title >= ‘S’”
Note the use of single quotes around the search letter. Single quotes are used to enclose strings in Criteria statements. Three logical operators can be used: equals (=), greater than (>), and less than (<).
• The NumberSkipped argument tells how many records to skip before beginning the Find. This can be used to exclude the current record by setting NumberSkipped to 1.
• The SearchDirection argument has two possible values: adSearchForward or adSearchBackward. Note, in conjunction with the four Move methods, the SearchDirection argument can be used to provide a variety of search types (search from the top, search from the bottom, etc.)
• If a search fails to find a record that matches the criteria, the Recordset’s EOF or BOF property is set to True (depending on search direction). Another property used in searches is the Bookmark property. This allows you to save the current record pointer in case you want to return to that position later.

No comments:

Post a Comment