yronium Level: Moderator

 Registered: 14-04-2002 Posts: 907
|
Re: Add Command not working
You miss a Confirm button.
It seems to me that there is a misunderstanding of the Add/Edit database procedure.
When you add a record, the steps are:
0 - at the beginning, the db is in some locked status, to prevent accidental editing of data
1 - entering in Edit mode: the db is notified of the adding attempt ("...Hey, db, I'm about to add a new record..."), so the engine 'unlocks' it
2 - A new line is created at the end of the recordset, meaning the empty fields are prepared to be filled with the values of the new record (see note B below)
3 - Pause. At this point the engine stops. There is nothing more to do unless the user has filled the fields.
4 - When the user has finished entering the values (How can we know it? See note A below) the db has to save the new record with the new values, and then lock again the db. The Edit mode is finished. When you edit a record instead of adding, the only difference is that there is no new line added.
So, even if some dbms make all this unlock/adding/lock process without the user can see it, this mechanism has to be taken in mind. For instance, ADO is transparent to the user during the process, but if you want to walk you own way, you must take care of these steps to do. So you have to manually
UNLOCK
ADD (an empty row)
FILL (the row)
UPDATE
LOCK (again)
Even using some ADO controls you can do your operation manually, but in this case some steps can be grouped. The only step that is really separated by others is the filling phase, because is not you (the program) who do it, but the user, so we can't really know when he's finished.
A) This is the reason because many applications provide a Confirm/Cancel couple of buttons: after insertion the user has to notify the db he's finished, so the db engine can start running again to save or discard changes, and lock the recordset again. Many db apps show the editing tools disabled during the locked status, and enable them only after the user has explicitly declared his intentions for Add/Edit operations, by clicking in an Add or an Edit button. And also the navigation commands are usually disabled during the edit status, as ADO automatically updates changes and exit the Edit status when moving to another record. You already did it.
So finally the user can Confirm or Cancel the changes to the new record. But if he choose to Cancel, the new record has to be deleted. Sometimes this can cause some troubles with indexes.
B) An alternative way is not to create any new empty row, but keeping the new values into the memory. Clicking the Add button just disconnects the textboxes from the recordset and clear them: only at the end, if the user clicks on Confirm, I can effectively unlock, create a new row in the recordset, fill it, save it and lock again. And relink the textboxes.
This is the way I use.
So I say:
- it has to happen nothing when you click the cmdAdd button, just clear out the textboxes;
- when clicking on it, disable all the navigation and editing controls. Also disconnect them from the recordset;
- provide a Confirm and a Cancel button: clicking on Cancel just clear the textboxes and reconnect them to the recordset. If Confirm is clicked you do all the steps above, then reconnect the textboxes and move the recordset to the end (the new added record).
You already do many of these things, but you miss a Confirm button, and I would put the complete code to add the new record in it.
Check if you have to explicitly call the Update method or not. I spoke about ADO for instance, but you have to check the features of the provider you use.
Hope it's clear and helpful.
____________________________
Real Programmer can count up to 1024 on his fingers
|