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.

clicked or a Move, Update, Delete, or AddNew method is executed. The general form of the<br />

Validate event procedure is<br />

Private Sub Data1_Validate(Action As Integer, Save As Integer)<br />

statement(s)<br />

End Sub<br />

The value of Action identifies the specific operation that triggered the event <strong>and</strong> the value of<br />

Save specifies whether data bound to the control has changed. You can change the value of the<br />

Action argument to perform a different action. Some values of the Action argument are shown<br />

in Table 11.3.<br />

TABLE 11.3<br />

Some Values of the Action Argument<br />

Value Description<br />

1 MoveFirst method<br />

2 MovePrevious method<br />

3 MoveNext method<br />

4 MoveLast method<br />

5 AddNew method<br />

6 Update operation (not UpdateRecord)<br />

7 Delete method<br />

10 Close method<br />

If you assign 0 to the Action argument, the operation will be canceled when the Validate<br />

event procedure is exited.<br />

The value of Save is –1 (True) if the data in any control attached to the data control have<br />

changed <strong>and</strong> is 0 (False) otherwise. If you set the value of Save to 0 in the Validate event procedure,<br />

any changes will not be saved.<br />

Consider the form created in the walkthrough. Suppose the contents of txtPop1995, the<br />

1995 population text box, is changed to .8 <strong>and</strong> then a navigator arrow is clicked in an attempt<br />

to move to another record. The following code prevents the move.<br />

Private Sub datCities_Validate(Action As Integer, Save As Integer)<br />

Dim strMsg As String<br />

If val(txtPop1995) < 1 then<br />

strMsg = “We only allow cities having a population ” & _ “at least one<br />

million.”<br />

MsgBox strMsg“City too small!”<br />

Action = 0<br />

End If<br />

End Sub<br />

If the statement<br />

Action = 0<br />

is changed to<br />

Save = 0<br />

the move will take place, but the previous record will retain its original values. That is, the number<br />

.8 will not appear in the table.<br />

EXAMPLE 1<br />

An Introduction to Databases 327<br />

The following program is a general database manager for the Cities table in the MEGACTY1.MDB database.<br />

It allows the user to edit the Cities table as needed <strong>and</strong> to locate information based on the city name.<br />

(In the Validate event procedure, the inner If block keeps the message box from appearing when the first<br />

or last record is deleted.)

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

Saved successfully!

Ooh no, something went wrong!