0

When a user closes the form, Access automatically saves the data entered in database. Thus there is no need to having a Save Button.

But, since most of the users are so habitual in pressing Save button, they will still insist that there should be a Save Button.

What code should be written for the Save button for saving all the data entered in the form.

Also care has to be taken for checking if the data has changed or not. Sometimes users will open the form to view data, then even though though they have not made any changes, they will press Save Button.

1
  • Would this code work for you? Private Sub cmdSave_Click() : Msgbox "Data Saved", vbOkOnly : End Sub :) Commented Mar 21, 2016 at 10:34

3 Answers 3

1

The code for Click event of Save Button

Private Sub cmdSave_Click()
    If Me.Dirty Then    'to check if any data has changed.
        DoCmd.RunCommand acCmdSaveRecord
    End If
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

You can also use Dirty only:

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

Comments

0

I am thinking this is what you want based on the same comments that i got from my colleagues... I always begin with asking a question whether they want to save or not, sometimes users are just playful and mess around with records.

    If me.dirty = true then
            If MsgBox("Are you sure you want to save as " & TempVars!gbl_username    & "? 
                       After saving you will be moved to a new record", _
                       vbYesNo + vbQuestion, "Save Changes") = vbNo Then
                'If answer is no
                    'Do nothing
            Else
               DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70                    
            End If
    End If

1 Comment

Microsoft deprecated DoCmd.DoMenuItem twenty years ago. They recommend RunCommand instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.