David Morin
David Morin

Reputation: 397

Access 2010 Form won't save record

I have a form with some fields on it. Form was created from the table in question.

I've tried to use the button wizard to create a save button, I've tried to use the following:

Private Sub saveRecord_Click()
   DoCmd.RunCommand acCmdSaveRecord
End Sub

Nothing works, any method I try to save the info doesn't work. I've made sure the form is bound, as far as I can tell it is, it was created off that table.

I have this line when the form loads to make sure the form is on a new record:

Me.Recordset.AddNew

From my limited understanding of the language, setting the form to a new record and then doing a save command should work?

Upvotes: 2

Views: 8306

Answers (1)

HansUp
HansUp

Reputation: 97101

RunCommand acCmdSaveRecord will only do a save if there is something to save. Simply adding a record to the form's recordset doesn't give you anything to save.

Make this change to saveRecord_Click() to confirm whether that is the explanation:

Private Sub saveRecord_Click()
    If Me.Dirty = True Then
        DoCmd.RunCommand acCmdSaveRecord
    Else
        MsgBox "nothing to save"
    End If
End Sub

Some other suggestions ...

Since you want to go to the new record when the form loads, use GoToRecord instead of Me.Recordset.AddNew:

Private Sub Form_Load()
    DoCmd.GoToRecord Record:=acNewRec
End Sub

The version of saveRecord_Click() I suggested earlier was only to explore the problem. But for routine use, it doesn't make sense to allow the user to click a saveRecord button and then tell them there is nothing to save. It is better to only make the button clickable when there is something to save.

So you can disable the command button in the form's On Current event and enable it in the On Dirty event (which means there is something to save).

Private Sub Form_Current()
    Me.saveRecord.Enabled = False
End Sub

Private Sub Form_Dirty(Cancel As Integer)
    Me.saveRecord.Enabled = True
End Sub

Also, as the last step of saveRecord_Click(), you may wish to SetFocus on another control and disable the command button.

Upvotes: 3

Related Questions