Solved

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

Posted on 2014-11-12
3
130 Views
Last Modified: 2015-01-19
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!
0
Comment
Question by:Kevin Smith
3 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40437829
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

0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40437830
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
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40438021
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
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now