Coding the Commit Button (Update and Add)
The procedure for committing an update is similar for committing an Add, except that we place the dynaset in edit mode using DbEdit and then assign the new dynaset values.
Since we use the same commit button and the same commit event procedure to add and update, we introduce two global flags DoAdd and DoUpdate to distinguish between adding and updating. The Add and Update click event procedures set these flags.
The Commit event procedure for adding and updating does the following:
- Validates entered data. Handles errors when necessary.
- Uses DbAddNew to add records, else uses DbEdit for updates.
- Assigns entered data to dynaset fields, using the Fields().Value property.
- Updates database with new record, using DbUpdate.
- Disables the Commit button.
- Re-enables all other functional buttons including the Add and Update button.
- Sets the DoUpdate and DoAdd flags to false.
For clarity, we placed the data validation checks in Step 1 in a separate subroutine called DoValidationChecks(). We also moved the code that changes button and flag states in Steps 5 through 7 to a subroutine called SetAfterCommitFlags().
The code for the Commit function is broken into the following routines:
Commit_Click() Event Procedure
DoValidationChecks() Function
Update_Dynaset_Fields() Function
SetAfterCommitFlags() Subroutine