Open Form to new Record and don't save (1 Viewer)

JPR

Registered User.
Local time
Today, 06:28
Joined
Jan 23, 2009
Messages
122
Hello friends, back for some other useful help.
On a form I have placed a button to go to a new records: DoCmd.GoToRecord , , acNewRec.
It cold happen that users don't want to save this entry or accidentally erroneously clicked on the New Records Button.
Is there a way, you can recommend me to avoid this? Maybe some code that checks if no data has been entered, closed the forms without saving it. Thank you
 

arnelgp

error reading drive A:
Local time
Today, 22:28
Joined
May 7, 2009
Messages
11,536
the safest way is to use Transaction to your form.
 

JPR

Registered User.
Local time
Today, 06:28
Joined
Jan 23, 2009
Messages
122
Thank you. But honestly I have never heard about it.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 14:28
Joined
Jul 9, 2003
Messages
12,458
Your question reminded me of a previous question on the forum which I answered here:-


Extract:-
The method sets the textbox default value. Now you can see the information and vet it without saving it to the table.

This method allows you to open a form, ready to enter a new record with some of the text boxes already filled with information. The user can then update the text. The action of making an update to the text saves the record, your changes, and the "default values".
 

Gasman

Enthusiastic Amateur
Local time
Today, 14:28
Joined
Sep 21, 2011
Messages
7,745
Will try. Thank you
If you do not want to investigate/use the transaction method, then research the Form BeforeUpdate event.
 

missinglinq

AWF VIP
Local time
Today, 09:28
Joined
Jun 20, 2003
Messages
6,402
Using transactions seems a bit of overkill. I agree with Gasman...and always use this
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
  If Me.NewRecord Then
      If MsgBox("Would You Like To Save This Record?", vbQuestion + vbYesNo + vbDefaultButton1, "Save This Record ???") = vbNo Then
        Me.Undo
      End If
  End If
End Sub

Linq ;0)>
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 09:28
Joined
Feb 19, 2002
Messages
31,102
Since we do not have access to the Microsoft code that powers a form, we cannot implement a form level transaction. Within a form, a transaction would be implemented within a single event. A reason might be that you have a complicated update process that requires two tables to be updated and you don't want to take a chance of the update failing after only one part of the action has happened.

The correct method of controlling whether or not a record is saved is the Form's BeforeUpdate event as Gasman alluded to. The BeforeUpdate event is like the flapper at the end of a funnel. If the flapper is open, the record is saved. If it is closed, the record is NOT saved. YOU have complete control. Add validation code in the BeforeUpdate event to ensure that required fields are present and other fields have valid values if you have rules such as DOB can't be > today.

Personally, I don't recommend prompting people for each save. I use validation code so I can be sure that the data being saved is valid and I don't want to desensitize the user with prompts. If they get used to always clicking OK because I prompt every time they save a record, they will robotically click OK and blow by errors and warning messages also.

Records that are not dirtied do NOT get saved. If your code is dirtying a record before the user types something, then you are causing a problem. If you want to auto populate controls, wait and put the code into the form's BeforeInsert event. That event won't run until the user has typed the first character into some control.
 

JPR

Registered User.
Local time
Today, 06:28
Joined
Jan 23, 2009
Messages
122
Thank you for your great help.
 

Users who are viewing this thread

Top Bottom