code for command button to update and to add current record

G

Guest

Hi,
I know this is probably simply, but I'm stumped...
What's the VBA code for a command button to update the current record?
What's the VBA code for a command button to add the current record?
Thanks,
Lynn
 
A

Allen Browne

Can we assume you have already set up your form so it is bound to the table
(or query) that has the fields you want updated, and you have the text boxes
and other controls on the form bound to the correct fields?

If so, you can save the changes (write the record) with:
RunCommand acCmdSaveRecord
or:
If Me.Dirty Then Me.Dirty = False

The second one has the advantage that it works even if the form does not
have focus.
 
A

Allen Browne

Okay, here's an explanation:

A bound form has a Dirty property.

It is True if there are uncommitted edits in the form, or False if there is
nothing that needs to get written to the table.

If you set the property to True, Access saves the record.

If you attempt to set the property to True, and Access says the propety
cannot be set, the record is not valid (e.g. a required field is not filled
in, or a validation rule is not met.)

If you get a message saying a form has no Dirty property, the form is
unbound.
 
A

Allen Browne

Sorry: If the property is true, the record needs saving.
If you set it to FALSE, it saves the record.

Sorry for the miscommunication.
 
P

ploddinggaltn

Hi Allen,

I've searched the questions to look for my answer and came across this
string of very informative messages...I now understand the Dirty function. I
do have a question that is quite similar so I was wondering if you could tell
me if your guidance would work for me.

I have a form used for data entry with numerous bound fields on it. I don't
want the information in these fields saved until the saved button is clicked.
Currently the data is saved to the table as it is entered. Is there some
code that I can use that will only save the record after the Save button is
clicked. I want to prevent partially completed data from saving to the file.

Thank you,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top