vbams-accessms-access-2010

acCmdSaveRecord does not save record


I have a MS Access 2010 application. In a form new records can be entered. Therefore I go to new record on form load:

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

After information is entered, by click a save button, the record shall be saved and the form shall go to a new record.

Private Sub btnSave_Click()
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.GoToRecord , , acNewRec
End Sub

The code gets executed, however, no new record is created. The input controls are linked to the table fields.


Solution

  • I would ensure that the form only attempts a save when it is dirty like below. I have added an error handler to notify the user that the form cannot be saved.

    option explicit
    private sub btnSave_Click()
    on Error goto ErrTrap
    if me.dirty= true then
         DoCmd.RunCommand acCmdSaveRecord
         DoCmd.GoToRecord , , acNewRec
    end if
    
    ErrTrap:
       MsgBox Err.Number & ": " & Err.Description
    end sub
    

    there is a work around to avoid the docmd.runcommand ; it will save the record... but I have never used it :

    if me.dirty=true then
       me.dirty=false
    end if