19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

population (in descending order). When an item in the list box is double-clicked, the list box’s Text property<br />

is used to display the name of the country’s unit of currency in a label. (Recall that the value of the<br />

Text property is the currently highlighted item.) The data control is made invisible since it is not needed<br />

by the user.<br />

Object Property Setting<br />

frmCountries Caption Large Countries<br />

datCountries Caption Countries<br />

Database MEGACTY2.MDB<br />

Name<br />

Record Countries<br />

Source<br />

dblCountries RowSource datCountries<br />

ListField Country<br />

cmdSort Caption Sort by 1995<br />

Population<br />

lblCurrency Caption Currency:<br />

lblUnit Caption (blank)<br />

BorderStyle 1- Fixed Single<br />

Private Sub CmdSort_Click()<br />

datCountries.RecordSource = “SELECT * FROM Countries “ & _ “ORDER by pop1995<br />

DESC”<br />

datCountries.Refresh<br />

End Sub<br />

Private Sub dblCountries_dblClick()<br />

datCountries.Recordset.FindFirst _<br />

“Country =” & “‘“ & dblCountries.Text & “‘“<br />

lblUnit.Caption = datCountries.Recordset.Fields(“currency”).Value<br />

End Sub<br />

[Run, click on the comm<strong>and</strong> button, <strong>and</strong> then double-click on Japan.]<br />

When used to enhance data entry, a data-bound list or combo control is usually linked to two<br />

data controls. The first data control is used to fill the list <strong>and</strong> the second to update a field in a<br />

table. As demonstrated in Example 2, the first data control fills the list as designated by the<br />

data-bound control’s ListField <strong>and</strong> RowSource properties. The second control updates a field in<br />

a table specified by the data-bound control’s DataSource <strong>and</strong> DataField properties. Another<br />

property of the data-bound control, the BoundColumn property, specifies the name of a field<br />

in the first data control’s recordset. Once the user chooses one of the items in the list (<strong>and</strong> thereby<br />

one of the rows), the value in the row’s specified field is passed to the field to be updated.<br />

Usually, the BoundColumn property has the same setting as the DataField property.<br />

EXAMPLE 3<br />

Creating <strong>and</strong> Designing Databases 341<br />

The following program adds cities to the Cities table of MEGACTY2.MDB. Of course, only cities in a<br />

country found in the Countries table are acceptable. A data-bound combo box is employed to show the<br />

user a list of the acceptable countries. (The experienced data-entry person can just type a name directly<br />

into the text box portion of the combo box, whereas the novice must refer to the list.) When the program

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!