Coding the Commit Button (Add)
To commit an add, we place the dynaset in add mode using
DbAddNew. We then assign the new data to the dynaset fields and update the database using DbUpdate. To make our program robust, we validate some fields before attempting to add them to the database.
Our Commit_Click() event procedure for adding records does the following:
- Checks that at least the Employee Number and Department fields are not null.
- Checks that the new Employee Number is not a duplicate entry.
- Places the dynaset in add mode using DbAddNew.
- Assigns entered data to dynaset fields using the Fields().Value property.
- Updates database with new record, using DbUpdate.
- Disables the Commit button.
- Enables the Add button.
To check for duplicate entries in Step 2, we create a local dynaset with the NOCACHE option, using a SQL statement that counts the rows matching the entered Employee Number field. If we get a match (row count greater than 0), the entered employee number is a duplicate entry and an error is displayed. In this case where the SQL select statement returns only a number, creating the dynaset without a cache is a more efficient error check than catching a duplicate entry error from the server.