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.

326 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

assigns “Beijing” to the variable strVar <strong>and</strong> the statements<br />

datCities.Recordset.Edit<br />

datCities.Recordset.Fields(“city”).Value = “Peking”<br />

datCities.Recordset.Update<br />

change the city field of the current record to “Peking”. (The first statement makes a copy of<br />

the current record for editing. The second statement alters the copy, <strong>and</strong> the third statement<br />

sends the new copy of the record to the database.)<br />

The number of previously accessed records in the table is given by the RecordCount<br />

property. The EOF (End Of File) <strong>and</strong> BOF (Beginning Of File) run-time properties indicate<br />

whether the end or beginning of the file has been reached. For instance, the following two<br />

sets of statements each place the cities into a list box.<br />

datCities.Recordset.MoveLast ‘Needed to set value of RecordCount<br />

datCities.Recordset.MoveFirst<br />

For i = 1 to datCities.Recordset.RecordCount<br />

lstBox.AddItem datCities.Recordset.Fields(“city”).Value<br />

datCities.Recordset.MoveNext<br />

Next i<br />

datCities.Recordset.MoveFirst<br />

Do<br />

While Not datCities.Recordset.EOF<br />

lstBox.AddItem datCities.Recordset.Fields(“city”).Value<br />

datCities.Recordset.MoveNext<br />

Loop<br />

The current record can be marked for removal with the statement<br />

Data1.Recordset.Delete<br />

The record will be removed when a data control navigation arrow is clicked or a Move<br />

method is executed. A new record can be added to the end of the table with the statement<br />

Data1.Recordset.AddNew<br />

followed by<br />

Data1.Recordset.Fields(“fieldName”).Value = entryForField<br />

statements for each field <strong>and</strong> a<br />

Data1.Recordset.Update<br />

statement. Alternately, the AddNew method can be followed by the user typing the information<br />

into text boxes bound to the data control <strong>and</strong> then moving to another record. (Note: When<br />

you add a record <strong>and</strong> then click on the MovePrevious arrow, you will not see the next-to-last<br />

record, but rather will see the record preceding the record that was current when AddNew was<br />

executed.)<br />

■ THE VALIDATION EVENT<br />

<strong>Visual</strong> <strong>Basic</strong> has a device called validation that lets you restrict the values a user can enter into<br />

a table. For instance, if the Cities table is only to contain cities with a population of more than<br />

1 million, you can use validation to prevent any record with a number less than 1 in the pop1995<br />

field from being entered. Validation also allows you to catch (<strong>and</strong> prevent) errors that might<br />

cause a program to crash.<br />

Data controls have an event procedure called Validate that is activated whenever the current<br />

record is about to be changed. For instance, it is called when a navigation arrow is

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

Saved successfully!

Ooh no, something went wrong!