Share via

VBA Macro for Save Button

Anonymous
2016-03-18T13:15:39+00:00

Hi There,

I am using the following VBA code for save button to save record and make the form ready for the new one.  But the problem is that when I open the form from 0 level, it is populated with the information of first record entered by it. Please help me resolve this issue.

I'll be much more most thankful.

The code is below.

Dim strMsg As String, strTitle As String

strMsg = "Do You Want To Save This Record?"

strTitle = " Save Record ?"

If MsgBox(strMsg, vbQuestion + vbYesNo, strTitle) = vbNo Then

Me.Undo

End If

DoCmd.GoToRecord , , acNewRec

End Sub

Regards,

Khurram

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

5 answers

Sort by: Most helpful
  1. Anonymous
    2016-03-18T14:20:16+00:00

    If what you want it entering new records only and not to edit existing ones change the form's Data Entry property to Yes.

    5 people found this answer helpful.
    0 comments No comments
  2. ScottGem 68,810 Reputation points Volunteer Moderator
    2016-03-19T13:00:41+00:00

    Just note, that using Bill's solution means the form can ONLY be used to enter new records. It cannot be used to view existing records. You will need to create another form for that.

    I also suggest you look at Ken's response about Save buttons.

    0 comments No comments
  3. Anonymous
    2016-03-19T09:01:16+00:00

    If what you want it entering new records only and not to edit existing ones change the form's Data Entry property to Yes.

    Dear Bill,

    Thank You very much. Rather very very very much. Rather Thank You Very much more most much.

    I have done it using your tip.

    May I talk to you in future..? I think it will be helpful for me. I hope you wont mind it.

    May GOD ALMIGHTY keep you in HIS shelter.

    Regards,

    Khurram

    0 comments No comments
  4. Anonymous
    2016-03-18T23:28:34+00:00

    Forcing the user to save a record only via a command button is rather more complex then merely inserting code behind the button.  You need to take account of the user attempting to save the record in some other way, including by closing the form.  Take a look at SaveDemo.zip in my public databases folder at:

    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to  amend the form design accordingly.  

    If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.

    This little demo file enables/disables button's depending on the current state of the record, in a similar way to that described by Scott, but also prevents the user from saving the record or closing the form other than via the command buttons.

    If you want the form to be at an empty new record when opened, and to move to a new record after being saved then add the following line to both the form's Load event procedure, and the cmdSave button's Click event procedure:

       DoCmd.GoToRecord Record:=acNewRec

    0 comments No comments
  5. ScottGem 68,810 Reputation points Volunteer Moderator
    2016-03-18T15:40:50+00:00

    When you open a form, you have a choice to open it Add, Edit or Read only modes. The default is Edit mode. So, when you open in Edit mode it will display the first record in the current sort order for the form. 

    One thing you can do is use the On Current and On Dirty Events of the form to govern your Save button. For Example:

    Me.cmdSave.Enabled = Me.Dirty

    or 

    Me.cmdSave.Visible = Not Me.Dirty

    So if the Form does not have pending edits, the button will either be disabled or hidden. Once the form is "dirtied" (meaning it has uncommitted edits), then the button becomes enabled or visible. 

    P.S. VBA and macros are different things in Access.

    0 comments No comments