Murray Brown
asked on
Access VBA. Buttons to save data in form with embedded form
Hi
I am using the following code to stop data entered in an Access form being saved to a table
unless the user clicks a 'OK" button. I now want to use this in a form that has a sub form embedded
in it. There are two joined tables "Invoices" and "Invoice Detail". I created the form using the Access wizard
so can't even see it in the list of tables and forms on the left. How do I go about this?
I am using the following code to stop data entered in an Access form being saved to a table
unless the user clicks a 'OK" button. I now want to use this in a form that has a sub form embedded
in it. There are two joined tables "Invoices" and "Invoice Detail". I created the form using the Access wizard
so can't even see it in the list of tables and forms on the left. How do I go about this?
Private blnSaved As Boolean
Private Sub btnCancel_Click()
DoCmd.Close
MsgBox "Changes discarded!"
End Sub
Private Sub btnOK_Click()
blnSaved = True
MsgBox "Changes saved!"
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If blnSaved = False Then
Cancel = True
End If
End Sub
Private Sub Form_Current()
blnSaved = False
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@ste5an,
"Requiring an explicit Save button is imho problematic, cause then you will have some forms requiring an explicit Save click, but others will save their data when you press [x]. Same for datagrid views of tables or views."
I have learned, over the past 24 years, that most of my clients prefer explicit Save and Cancel buttons, on all forms, so that they know when they are saving a record. Since almost all of my forms contain code in the Form_BeforeUpdate event to ensure required fields have been filled in, it is easy to prevent the user clicking on the (x) as a way to close and save the data in the form.
"Requiring an explicit Save button is imho problematic, cause then you will have some forms requiring an explicit Save click, but others will save their data when you press [x]. Same for datagrid views of tables or views."
I have learned, over the past 24 years, that most of my clients prefer explicit Save and Cancel buttons, on all forms, so that they know when they are saving a record. Since almost all of my forms contain code in the Form_BeforeUpdate event to ensure required fields have been filled in, it is easy to prevent the user clicking on the (x) as a way to close and save the data in the form.
Yup, me too. And where it was an absolute requirement, the only way are unbound forms. Cause I have seen code corruption, where the code wasn't anymore executed on pressing [x], thus data got saved instead of discarded with bound forms...
Just my 2¢.
Just my 2¢.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I know, but as I said, I have seen corrupted databases where the code wasn't executed and the data was saved. Thus bound forms are not 100% reliable, when the requirement is "only the save button is allowed to save data".
The possibility of potential corruption isn't a reason for using unbound forms. Any object in the database can corrupt. Are you going to avoid all of them? All you need to have complete control over saving is an understanding of what event to use.
ASKER
Thank you all very much. I abandoned the idea of trying to control Access with an OK and Cancel button because of your advice and because of all the errors that cropped up
Actually, I wish they would do what was done in the VFP data environment; allow you to control the buffering mode.
With VFP, you just tell it to hold all updates until you issue an explicit save (an automatic transaction). Working with main/subforms in VFP was drop dead easy.
This is one area where Access has always been lacking.
Jim.