Saving Records without a Save Command Button

Richie2837

Registered User.
Local time
Today, 10:47
Joined
Jan 30, 2007
Messages
88
Is it possible to assign code for savinge a new record to an exisitng command button? Ideally I want the close command button to also save any changes made, also the new record button (i.e. when entering a list of new records, each click of the "new Record" button will save the last new record and open up a blank form), also the next record command on the record selectors toolbar. This will remove the need for having a seperate "save" button.
 
Hi, Richie!

I think you can use events on form to be used for your porpose. E.g. OnClose event of the form you can assign the code sequence that saves your record into db.

Krava
 
Records are saved when you close a form or move to a new record anyway
 
If you put the following line of code into the OnClick event of your close command button, it should save the record that you're working on.

Code:
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
 
Well, you are probably right. But it depends on if the fields of the form are bound. If not the save procedure is needed. Some of client applications require to work kind of "Offline" to prevent unwanteed chages. But most of them not.

Krava
 
Actually if the form is unbound the normal save command won't do anything, also worthy of note is that DoMenuItem has been obsolete for years and should be replaced with the RunCommand eqv.
 
Well, I did not mean a normal save command. What I meaned was the code sequence to save the data from unbound form, e.g. update query or recordset update or somethng else.

Krava
 
I'm afraid I can't get the code to work Agehoops. When I click the close button I get the message "The command or action 'SaveRecord' isn't available now."
 
What exactly is the situation then? Are the fields bound or unbound? If they're unbound then that's probably why it won't work. If they are bound, then as someone has said earlier, it will automatically save the record when the form is closed or a new record is created. It really depends on what you have setup?
 
I'm afraid I can't get the code to work Agehoops. When I click the close button I get the message "The command or action 'SaveRecord' isn't available now."

Is this because the record has already been saved?

I have the same problem. I put a save record comand (DoCmd.RunCommand acCmdSaveRecord) in the On Open Event and it works when there is a newly entered record to save but crashes when there is not.

HTH

Tyler
 
I put a save record comand (DoCmd.RunCommand acCmdSaveRecord) in the On Open Event and it works when there is a newly entered record to save but crashes when there is not.

A save record command has absolutely no business in the OnOpen event! This event fires when a form is first loading! It's just a miracle that it doesn't always bomb out!
 
I put a save record comand (DoCmd.RunCommand acCmdSaveRecord) in the On Open Event and it works when there is a newly entered record to save but crashes when there is not.

A save record command has absolutely no business in the OnOpen event! This event fires when a form is first loading! It's just a miracle that it doesn't always bomb out!

And yes, using a save record command on an unbound form will error out because there is never a record to save. An unbound form never contains a record, only data waiting to be written to a table.

As to the original question, you'll have to use the same code as you would for a save button. You cannot add code to the native Form Close button, but rather you'd have to place it in the OnClose event for the form. If the form is unbound, there will be no New Record button, for the reason stated above, unbound forms don't have records!

To be honest, that fact that you have to ask these questions is a very good indication that you have no business using unbound forms for data entry. This is something that should only be attempted by the very, very experienced Access developers.
 
All is revealed in this thread:

http://www.access-programmers.co.uk/forums/showthread.php?t=150648

I have one form onto which data is entered and another form which displays the data (with other stuff). If the data entered on the first form is not saved prior to the opening of the second form it is not displayed.

Regards

Tyler

The problem is that you misunderstood MStef's answer. He didn't say to put it in the On Open event - he said to put it in the click event BEFORE the code to open the other form.
 
Code moved to the top of the click event of the button which opens the second form.

Database functioning properly now.

Thanks for everyone's input.

Tyler
 

Users who are viewing this thread

Back
Top Bottom