Full Version: Close Button Question
UtterAccess Forums > Microsoft® Access > Access Forms
Here I thought I understood this command...
Thanks again for your help.
In my database I want to move the user through forms passing some information along and closing the previous form.
HAs an example I have a Form frm_NewCustomerEntry with a button cmdNewOrder that has in its OnClick Event:
Private Sub cmdNewOrder_Click()
DoCmd.OpenForm "frm_NewOrderEntry2", acNormal, , , acFormAdd
Forms!frm_NewOrderEntry2.cboCustomer = Me.ID
Forms!frm_NewOrderEntry2.cboStore = Forms!frm_NewCustomerEntry!StoreInfo_ID
DoCmd.Close acForm, "frm_NewCustomerEntry", acSaveYes
End Sub
It does what I expected Pass the 2 pieces of data along to the new form. Closes and opens the new form.
What I did not expect is that it can execute even if controls with required values of yes are not entered.
What is it I am doing wrong?
Hi Jason,
You could try checking for those required fields in the Form's BeforeUpdate event.
Just my 2 cents...
It just seems that there should have been an easier way... LOL
All that time setting control Properties and validation messages and I bypass them all.
Another way might be to use the Form's Error event to trap the system error that is raised when the user leaves a Required field empty.
Hope that helps...
Slightly off topic but a somewhat related anecdote.
I was building a database for a previous company I worked at and was showing a coworker some of the features I had put in a form. After I get done going through a few things he asks. "What does the 'close' button do?"
At first I thought he was kidding but when I looked at him I could tell he was serious. So I clicked on it and said, "It closes the form." Then I handed him his sign.
/Bill Engval reference.
1. Did you set these validation checks in the Table Design and on the bound Controls on the Form or only on the Form? You should always enforce the validations in the Table Design (more important, IMHO) and the Form Design.
. I think the problem is that you code has the logic wrong way around: your code should commit the CurrentRecord buffer first before trying to open the follow-on Form. This way the validation checks are performed and if necessary, i.e. when validation fails, stop the opening of the follow-on Form if required.
3. Beware that the argument acSaveYes is likely to be wrongly used in the DoCmd.Close statement in your code. Check Access VBA Help for the exact meaning to see if you should use acSaveYes or more likely, acSaveNo.
I have validation rules in both the Table and Form for the same fields as I worried this might be causing the problem. No Joy. Have tried splitting the functions into 2 buttons.
utton 1: cmdSaveRecord has on the OnClick Event.
If Me.Dirty Then
DoCmd.RunCommand acCmdSaveRecord
Forms!frm_CustomerEntry!cmdNewOrder.enabled = True
MsgBox "There were no modifications made to the current record.", vbInformation, "Invalid Save"
End If
Button 2: cmdNewOrder has on the OnClick Event
DoCmd.OpenForm "frm_NewOrderEntry2", acNormal, , , acFormAdd
Forms!frm_NewOrderEntry2.cboCustomer = Me.ID
Forms!frm_NewOrderEntry2.cboStore = Forms!frm_NewCustomerEntry!StoreInfo_ID
DoCmd.Close acForm, "frm_NewCustomerEntry", acSaveNo
Oget a runtime 3316 error not the validation message.
1. What happens when you click button 1 on the dirtied current record buffer that does not have enough data to pass validation.
. What data & steps you took to get Error 3316? What is the Error Description? Which line of code error out?
1. The Button cmdNewOrder is Enabled and the error is generated.
On the case that the First Name is Null (First Name has Validation Rule Is Not Null. Validation Text: "Please Enter First Name") I get an error on line DoCmd.RunCommand acCmdSaveRecord citing the Validation Text ie. Runtime Error '3316': Please Enter First Name.
That's why I always enforce validations in Table and never use the Validation Property of Controls. In the Form, I prefer to code my own validation check in the Form_BeforeUpdate Event if I want simple uncluttered messages to the users.
Try the Form_BeforeUpdate Event with your own code for validations and MsgBoxes...
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.