Full Version: save record with button
UtterAccess Discussion Forums > Microsoft® Access > Access Forms
dwAccessUser32
I have a form with a half dozen textboxes on it. After the user enters the data, I want when they click a button that data is inserted into the table and they are presented with a message that says "Information entered".

I know this is basic, but forgot how to do it. Can anyone tell me?

Thanks.
CyberCow
In the "OnClick" procedure of a command button:

DoCmd.RunCommand accmdSaveRecord
MsgBox "Information Entered."


hope this helps
freakazeud
Hi,
is this a bound form with bound controls?
If so you don't really need to do anything...the record should be stored as soon as it looses focus.
If you do want to ensure a forced save I would suggest using:

If Me.Dirty = True Then
Me.Dirty = False

HTH
Good luck
dwAccessUser32
Thanks!

One more question: How do I then make it clear out the form and present a clean new blank form for the next record?

Edited by: dwAccessUser32 on Wed Feb 14 11:14:52 EST 2007.
freakazeud
Hi,
if this is a bound form which I assume then just navigate to a new record e.g.:

DoCmd.GoToRecord , , acNewRec

With unbound forms you would need to set each control's value to null.
HTH
Good luck
dwAccessUser32
Yes, it is a bound form. I have the following code in the onclick event:

Private Sub Form_Click()
DoCmd.RunCommand acCmdSaveRecord
MsgBox "Information Entered."
DoCmd.GoToRecord , , acNewRec
End Sub


After typing in the data and clicking the button, I get a message box that says: "Information Entered". I click ok
and then the message box goes away. However, the form still shows its old values and does not present a clean new form. Any ideas?

And, thanks again for the help.
freakazeud
Hi,
try the GotoRecord action on a separate button for now to see if it actually functions. You have the AllowAdditions property of the form set to Yes, right?
HTH
Good luck
dwAccessUser32
Yes, AllowAdditions is set to yes. I tried the GoToRecord and I get the message, "You can't go to the next record." I am not sure why this is so.

Edited by: dwAccessUser32 on Wed Feb 14 11:33:12 EST 2007.
freakazeud
Just add a new button and use its on click event to execute:

DoCmd.GoToRecord , , acNewRec

HTH
Good luck
dwAccessUser32
Ok, I added a new button and put the following code in:


Private Sub Command22_Click()
DoCmd.GoToRecord , , acNewRec
End Sub

I save the form and click the button. The button depresses, but nothing happens. The data does not disappear. No record is added in the table. Any ideas?
freakazeud
Mhhh...can you attach a small sample (keep it under 500kb and zip it).
It should be working.
HTH
Good luck
dwAccessUser32
I can't believe I missed this!

My mistake was that I forgot to bound the textboxes to the columns. I tested it after doing this and it works fine. By the way, one last question, how can I get it to when this form opens go to the end of the records instead of the very beginning.

For example, if there are a 100 records in the table, I want it to start at #101 and present a clean new form for data entry instead of starting at record 1 and then the user having to rifle though a 100 records to get to record 101 to enter the new record.

I appreciate your help! Thanks again.
freakazeud
So it was unbound up till now sad.gif
Either add the same line which navigates to a new record to the form's on load event...or set the Data Entry property of the form to Yes.
Keep in mind that the Data Entry form will prepare the form for data entry only...users will NOT be able to move back to old records and edit them. If you want that ability then use the GoToRecord options as described.
HTH
Good luck
dwAccessUser32
Thanks for the great help!
freakazeud
You're welcome.
Good luck on future projects!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.