Which is the best code to use to save a record and exit using a button?

xwnoob

Registered User.
Local time
Today, 14:16
Joined
Nov 7, 2011
Messages
70
Hi,

What is the best code to use in order to save the record and quit the form? I have a main menu so i will save,quit this form and go to main form

I have this code:

Private Sub testingsaveok_Click()
If Me.Dirty Then
Me.Dirty = False

DoCmd.OpenForm "BorrowerSection", acNormal
DoCmd.Close acForm, "AddNewBorrower", acSaveNo

End If
End Sub

Is this the best way? My code actually gets a runtime error if i did not fill in one of the required fields in my form but other than that it saved the form if i entered all required information.
 
If the form is bound to the data source, the record is saved automatically when the form is closed. Simplest code to close the form is
Code:
DoCmd.Close acForm, Me.Name
 
Look at "DemoSaveRecordA2000.mdb" (attachment, zip).
You don't need any code for the SAVE RECORD. Record will be saved automatic
always when you go on the next record, or a new record, or when yu close the form, (for close the form click on the X button in right upper corner on the form). The record will not be saved if you don't type a required field.
Open frmMAIN and try, (look at VBA).
 

Attachments

If the form is bound to the data source, the record is saved automatically when the form is closed. Simplest code to close the form is
Code:
DoCmd.Close acForm, Me.Name

Do i replace the "Name" in Me.Name with the form's name?
 
No. 'Name' is a property of the form, and Me.Name, when executed, returns the name of the form in which code is running. So to write code on a form so it closes itself use ...
Code:
DoCmd.Close acForm, Me.Name
... and it will always work, even if during develpment you rename it.
Cheers,
 

Users who are viewing this thread

Back
Top Bottom