Add Save Button to Save data

M

malik

I have a simple form. I want to add a Save Button so when a user is typing in
the feilds of a form, the data shuould not save to the table before the Save
button is press. When a user press save button, than the data shulod post in
table.
How to do that?
 
J

John W. Vinson

I have a simple form. I want to add a Save Button so when a user is typing in
the feilds of a form, the data shuould not save to the table before the Save
button is press. When a user press save button, than the data shulod post in
table.
How to do that?

One way is to put a shared variable in the form's module by typing

Dim OKToClose As Boolean

in the *very top* of the module, before any of the Sub lines. If you open the
code window in form design the top of it should read

Option Explicit
Option Compare Database
Dim OKToClose As Boolean

Set OKToClose to False in the form's Current event (so as each new record is
selected or created it's set to False).

The save button's Click event should just be:

Private Sub cmdSave_Click()
OKToClose = True
If Me.Dirty Then
Me.Dirty = False
End If
End Sub

Setting the form's Dirty property to false saves the record to disk.

In the form's BeforeUpdate event put code

Private Sub Form_BeforeUpdate(Cancel as Integer)
If Not OKToClose Then
Cancel = True
MsgBox "Please use the Save button to save the record", vbOKOnly
End If
End Sub



You may want to add a Cancel button too, in case the user wants to close
without saving. Its code should simply be

Private Sub cmdCancel_Click()
Me.Undo
End Sub
 
M

malik via AccessMonster.com

Now Please tell me What code shuld be added in it if I want to go to a new
record after saving this

Thanks
 
J

John W. Vinson

Now Please tell me What code shuld be added in it if I want to go to a new
record after saving this

Thanks

DoCmd.GoToRecord acDataForm, acNewRecord
 
M

malik via AccessMonster.com

Now I have a problem. All Code u told me above is working very nicely.

But problem is that I have a Subform in this Form in Datasheet view . When I
want to go to the fields of the of subform, It does not allow me to go on
subform, it shows me the MsgBox.

But I want that it let me allow go to Subform and after that it show MsgBox.

Thanks
 
L

Linq Adams via AccessMonster.com

When you move from a new/edited record on a main form to a record on the
subform, Access automatically attempts to save the main form record, hence
the message.

I don't know anyway around this, perhaps John will. The purpose behind this
is so that you can't create a "child" record until you've created and saved
the "parent" record.
 

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