Preventing commits when closing a form

  • Thread starter Thread starter Jeff Stewart
  • Start date Start date
J

Jeff Stewart

I'm working with a data entry form. When I close the form, any
partially-entered record is commited to the form's source table. How can I
configure the form to only commit records once the user navigates to the
next, blank record?
 
Jeff Stewart said:
I'm working with a data entry form. When I close the form, any
partially-entered record is commited to the form's source table. How
can I configure the form to only commit records once the user
navigates to the next, blank record?

It seems to me that your concern is to keep incomplete records from
being added, whether that happens by closing the form or by moving to a
new record. For that, you should put code in the form's BeforeUpdate
event that verifies that all required fields have been filled in, and
cancels the event (with an appropriate message) if not.

Be aware that if you have a "Close" button on the form that calls the
DoCmd.Close method, you should have the code behind that button
explicitly save the record before calling DoCmd.Close. Otherwise an
incomplete or otherwise unsavable record may be discarded silently.
 
Well, actually, silent discarding of incomplete or unsavable records is what
I want. I'm closing the form using the standard Windows "X" button. There
must be no UI prompts if the user closes the form in this way. Only records
that have been committed via my "Commit" button (which moves to a new
record) must be put to the underlying table.
 
Jeff Stewart said:
Well, actually, silent discarding of incomplete or unsavable records
is what I want. I'm closing the form using the standard Windows "X"
button. There must be no UI prompts if the user closes the form in
this way. Only records that have been committed via my "Commit"
button (which moves to a new record) must be put to the underlying
table.

Are you sure? Don't you think you may have users who enter data for a
record but then forget to click the Commit button?

Here's code from a form module that implements the bare bones of what
you describe, but I have serious reservations about discarding the
user's entries without warning:

'----- start of example code for form module -----
Option Compare Database
Option Explicit

Dim mfCommit As Boolean


Private Sub cmdCommit_Click()

mfCommit = True
RunCommand acCmdRecordsGoToNew

End Sub


Private Sub Form_BeforeUpdate(Cancel As Integer)

If Not mfCommit Then
Debug.Print "Not committed", Me.ID
Me.Undo
End If

End Sub


Private Sub Form_Current()

mfCommit = False

End Sub

'----- end of example code for form module -----
 
Dirk Goldgar said:
Here's code from a form module that implements the bare bones of what
you describe, but I have serious reservations about discarding the
user's entries without warning:
[...]

I should have removed the "Debug.Print" line from the Form_BeforeUpdate
procedure before posting it.
 
Dirk Goldgar said:
Are you sure? Don't you think you may have users who enter data for a
record but then forget to click the Commit button?

Yep. This is a pet-project right now and I'm reserving UI considerations
for a time when someone other than myself will use this, which may be never.
I'll eventually have my own "Close" button complete with "Are you sure?"
warnings, or have the Commit button close the form, permitting one entry per
session. I haven't quite gotten to that point yet. But right now most of
my work involves tweaking the database design itself, and all this
in-and-out of the form is putting lots of junk data in my table that I don't
want to clear out every time I'm debugging.

Thanks for the sample code. I'll try that out soon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top