These functions commit or discard changes to buffered data.
lSuccess = TableUpdate( [ lAll | nUpdateWhat [, lForce
[, cAlias | nWorkArea
[, ErrorRecordsArray ] ] ] ] )
nAffected = TableRevert( [ lAll [, cAlias | nWorkArea ] ] )|
Parameter |
Value |
Meaning |
|
lAll |
Logical |
Should we update or revert all records or just the current record? Relevant only when using table buffering. |
|
nUpdateWhat |
0 |
Update only the current row. |
|
1 |
If table buffering is on, update all rows. If row buffering is on, update the current row. Give up as soon as an error occurs, leaving whatever has already been updated changed. |
|
|
2 |
Same as 1, except if an error occurs, log it in ErrorRecordsArray and continue. |
|
|
lForce |
Logical |
Should we insist on our changes going through, even if others have changed the record in the meantime? |
|
cAlias |
Character |
Commit or discard changes for table cAlias. |
|
Omitted |
If nWorkArea is also omitted, commit or discard changes for the table in the current work area. |
|
|
nWorkArea |
Numeric |
The work area containing the table to have changes committed or discarded. |
|
Omitted |
If cAlias is also omitted, commit or discard changes for the table in the current work area. |
|
|
ErrorRecordsArray |
Array Name |
If nUpdateWhat is 2, contains a list of record numbers for which the update was unsuccessful. |
|
lSuccess |
.T. |
All changes were successfully committed. |
|
.F. |
Some changes could not be committed. |
|
|
nAffected |
Numeric |
The number of records for which changes were discarded. |
When you're working with buffered data, sooner or later you need to either copy the data from the buffer to the original table or view, or throw away the changes and restore the original data. That's what these two functions are about.
TableRevert() is simpler. It's the one you want when the user presses Cancel. You might also use it when you encounter a conflict and the user chooses to keep the other guy's changes. (You could also handle that case on a field-by-field basis—see GETFLDSTATE().)
For both functions, the lAll parameter is relevant only when you're using table buffering. With row buffering, whichever value you pass, you still affect only the current record. TableUpdate() offers an alternative approach to specifying what to update, too. You can pass 0, 1 or 2 for nUpdateWhat. 0's easy—it means update only the current row and corresponds to passing .F. for lAll. 1 is the same as passing .T. for lAll—it says to try to update all the records, but give up as soon as any record can't be committed. The problem with this approach is that it can leave you with some records updated and some not updated. Unless you're wrapped in a transaction, this is a recipe for disaster.
Fortunately, in VFP 5 and later, you have another alternative. Passing 2 for nUpdateWhat says to go ahead and commit all the changes you can, and create a log of the records that couldn't be changed. ErrorRecordsArray is a one-column array containing the record numbers for all those records.
The lForce parameter offers two attitudes toward updating. You can be polite and check whether anyone else has changed the data in the meantime, or you can be rude and ride roughshod over other people's changes.
Your application can approach updates from two perspectives. You can give it a shot and cope if the update fails. In this case, you'll probably want lForce to be .F. The other choice is to check for conflicts first and resolve them, then commit your changes with lForce = .T. when you've taken care of all the problems.
In later versions of VFP, failing to either commit or revert changes before closing a form is the same as reverting the changes.
* The Click code for a Cancel button may be as simple as:
=TableRevert()
* For a Save button, there are several approaches. If you choose
* to resolve conflicts first (see GETFLDSTATE() for one way to
* do so for a single record), you can finish up with a call
* to TableUpdate():
IF NOT TableUpdate(2, .T., ALIAS(),aSaveErrors)
* Force changes on all rows, but just in case, track
* any errors in an array.
* You'll want something a little more sophisticated here.
WAIT WINDOW "Unexpected problem with " + ;
ALEN(aSaveErrors,1) + " records"
ENDIFBuffering, CurVal(), GetFldState(), GetNextModified(), OldVal()

