Save and Close Form (1 Viewer)

access2010

Registered User.
Local time
Today, 03:04
Joined
Dec 26, 2009
Messages
651
Hello, how could I have one control button to save an entry and than close the form?
At the present time we have two control buttons.
Thank you,
Nicole
===
Save Record
===
Private Sub Save_Record_Click()
On Error GoTo Err_Save_Record_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Exit_Save_Record_Click:
Exit Sub
Err_Save_Record_Click:
MsgBox Err.Description
Resume Exit_Save_Record_Click
End Sub

===
Close form
===
Private Sub Com_exit_Click()
If Me.Dirty And IsNull(Me![Envelope#]) Then
MsgBox "Please enter the vendor envelope number."
Else
DoCmd.Close acForm, Me.Name
End If
End Sub
 

Isaac

Lifelong Learner
Local time
Today, 03:04
Joined
Mar 14, 2017
Messages
3,868
Something
Like?

Code:
If [your validation code here] fails then
    Msgbox "Failed"
    Exit sub
Else
    Me.Dirty=False
    Docmd.Close
End If
 

Micron

AWF VIP
Local time
Today, 06:04
Joined
Oct 20, 2018
Messages
3,471
How many buttons you need for a form depends on the process. It's typical to have one that says Save/Close and one for cancel, assuming cancelling input is applicable. In short, you probably need 2 buttons, just not the way you have them.

EDIT - I notice that you only seem concerned about missing data in closing code but not in saving code. Have you taken care of the possibility that the field has no value when an attempt to save the record is executed?
 
Last edited:

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:04
Joined
Feb 19, 2002
Messages
31,153
Validation code belongs in the Form's BeforeUpdate event or sometimes in a Control's BeforeUpdate event. That is the only way you can ensure that you are only saving valid data! Your validation code CANCELS the save when the data is invalid.

When you combine the save and close, which is done by using the command to save followed by the command to close, you should include an error trap. Otherwise you will get an error when the form tries to close if the save has failed.
 

Micron

AWF VIP
Local time
Today, 06:04
Joined
Oct 20, 2018
Messages
3,471
access2010: BTW, save buttons don't make any sense if this is not a data entry (or single record edit) type of form. If it is possible to navigate to another record, a save button is pointless.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:04
Joined
Feb 19, 2002
Messages
31,153
I also don't use Save buttons unless the client insists. Cancel buttons are another matter.
 

Micron

AWF VIP
Local time
Today, 06:04
Joined
Oct 20, 2018
Messages
3,471
save buttons don't make any sense if this is not a data entry (or single record edit) type of form.
Just realized I was thinking of bound forms. Unbound forms would be a different matter.
 

MickJav

Well-known member
Local time
Today, 11:04
Joined
Nov 28, 2005
Messages
2,431
access2010: BTW, save buttons don't make any sense if this is not a data entry (or single record edit) type of form. If it is possible to navigate to another record, a save button is pointless.
I always put code behine nav buttons and the close button on Single forms
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:04
Joined
Feb 19, 2002
Messages
31,153
I don't write code to do things that Access does for me.

It's a lifelong habit and stems from writing CICS transactions that potentially had thousands of concurrent users. Every extra line of code slowed down the entire network. In an Access environment, unless you put the extraneous code/functions in loops or queries, you never see how much overhead they cause.
 

Users who are viewing this thread

Top Bottom