Don't save a record if nothing changed (have pre-filled data from another form)?

Kevin Smith
Kevin Smith used Ask the Experts™
on
I have a form with a button to add new activity to the current record. In my activity form, I have a field that gets it's case ID from the open form. If a user closes the form, it creates a new record because that field was technically "filled out" with the ID. How do I tell it to not create a new record if nothing changed or was entered?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Director, Practice Manager and Computing Consultant
Awarded 2014
Top Expert 2014
Commented:
Test for it in the Form_BeforeUpdate:

Private Sub Form_BeforeUpdate(Cancel As Integer) 
   If <<ConditionsForNotSaving>> then
        Me.Undo 
  End If
End Sub

Open in new window

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
Don't set the value of the ID. Instead, assign the number as the DefaultValue of the ID. This way, no data are added before the user starts writing.

/gustav
Distinguished Expert 2017

Commented:
Never, ever dirty a record yourself with code.  If you need to populate foreign keys or data fields, do it in the form's BeforeInsert event.  That way your code will only run for new records and it will only run after the user has dirtied the record.  That prevents "empty" records from being created or the user from getting error messages he doesn't understand because he "knows" that he didn't change anything.

Or you can use the form's Dirty event but in that case, you MUST check to determine if it is a new record because you don't want to overlay existing data.
If Me.NewRecord = True Then
    populate the fields
End If

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial