Undo Auto Save on Close Form

  • Thread starter pushrodengine via AccessMonster.com
  • Start date
P

pushrodengine via AccessMonster.com

On my form I have a Command Button to add a record called "AddIncident", but
if someone enters a value into the form and closes the form it will
automatically save the incomplete record.

I only want records to be added when and only when the "AddIncident" button
is pressed.

How do I prevent the save on close?

Thanks
 
J

Jeanette Cunningham

Hi,
If your main form is read only, when the user wishes to add an incident, you
can open the add incident form as a separate form which is only available
for data entry.
This way, users can only add data if the form for add incident is open in
data entry mode.
There will be other ways to do accomplish the result you want, but if you
set up the main form so they can't add new records to it you are on the way
to making them use the add incident button to add new records.

Jeanette Cunningham
 
J

John W. Vinson

On my form I have a Command Button to add a record called "AddIncident", but
if someone enters a value into the form and closes the form it will
automatically save the incomplete record.

I only want records to be added when and only when the "AddIncident" button
is pressed.

How do I prevent the save on close?

Thanks

Put code in the Form's BeforeUpdate event to set Cancel to true if the record
isn't complete.

You can set the value of a hidden checkbox on the form to True in the
AddIncident button's click event (when you're satisfied that the record is
good to go), and cancel the BeforeUpdate if the checkbox is False.

John W. Vinson [MVP]
 
B

boblarson

Use validation on the Before Update event to check that all fields are
entered and you can use

Cancel = True
Me.Undo

if you don't have a full record. I would include a message box that says
something like "You didn't finish this record. Do you want to?" and then if
they say yes, then use Cancel = True

and if not use Cancel = True and Me.Undo

That will validate even if they move to another record instead of just
closing the form, so you get a complete record.
--
Bob Larson
Access World Forums Super Moderator
Utter Access VIP
Tutorials at http://www.btabdevelopment.com
If my post was helpful to you, please rate the post.
__________________________________
 
P

pushrodengine via AccessMonster.com

How can I add a message box to the “AddIncident†button that asks the user
“Would you like to add the Incident?†The message box would have an OK and
Cancel button.
If OK is pressed the Record is saved. If Cancel is pressed the message box
closes.

If the form is filled out and the user presses the closed button the form
should close without saving the record.

The user must press "AddIncident" button and press "OK" in the message to
save the Record.

Thanks everyone for the help!
 
J

Jeanette Cunningham

Hi,
use the Click event of the add incident button.

Private Sub CmdAddIncident_Click()
Dim strMsg As String

strMsg = "Would you like to add an Incident?"
If vbOK = MsgBox(strMsg, vbOKCancel, "Add incident?") Then
'code here to add the incident
Else
'code here to cancel adding an incident
End If


End Sub

Jeanette Cunningham
 
J

John W. Vinson

How can I add a message box to the “AddIncident” button that asks the user
“Would you like to add the Incident?” The message box would have an OK and
Cancel button.

Jeanette's answer is exactly right... but I'd like to have you reconsider. In
practice, this double confirmation will a) annoy your users ("if I didn't want
to add the incident I wouldn't have clicked the AddIncident button dammit!"),
and b) lead to thoughtless, automatic clicking of the Yes button (even if
that's followed three seconds later by "oops I didn't want to add that
yet...").

If you're trying to prevent addition of partial records, *prevent the addition
of partial records* by validating the data in the Form's BeforeUpdate event,
and canceling any additions that would leave essential data blank.

John W. Vinson [MVP]
 
P

pushrodengine via AccessMonster.com

To all, thank you.

How do I disable the auto save function?

I would only like the record to only be added when the add button is pressed.
I don’t want record to be added when the form’s close button is pressed.

Thanks Again
 
J

John W. Vinson

To all, thank you.

How do I disable the auto save function?

I would only like the record to only be added when the add button is pressed.
I don’t want record to be added when the form’s close button is pressed.

Thanks Again

Put a checkbox chkOKToClose on the form. You can make it invisible if you
want.

In the Click event of the add button set its value to True.

In the form's Current event set it to False.

In the form's Close event, set Cancel to True and issue a warning message if
chkOKToClose is False.

John W. Vinson [MVP]
 
P

pushrodengine via AccessMonster.com

I'm sorry John, but I don’t quite understand. Thank for your patience.
 
J

John W. Vinson

I'm sorry John, but I don’t quite understand. Thank for your patience.


Open the Form in design view. Use the Toolbar checkbox tool to put a checkbox
on the form. Name it chkOKToClose. You can make it invisible if you
want, by viewing its Properties and setting the Visible property to No.

In the Click event of the add button set its value to True: view the button's
Properties, click the ... icon by its Click event, and edit the code to read

Private Sub AddIncident_Click()
Me!chkOKToClose = True
End Sub


In the form's Current event set it to False: view the Form's properties, and
find the Current event on the Events tab. Click the ... icon and edit it to

Private Sub Form_Current()
Me!chkOKToClose = False
End If

In the form's Close event, set Cancel to True and issue a warning message if
chkOKToClose is False. Same drill:

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
If Me!chkOKToClose = False Then
iAns = MsgBox("Please use the Add Incident button to save the record" _
& " or select Cancel to erase the form and start over", vbOKCancel)
Cancel = True ' cancel adding the record
If iAns = vbCancel Then
Me.Undo ' erase the form
End If
End If
End Sub

John W. Vinson [MVP]
 
P

pushrodengine via AccessMonster.com

John,

What is the proper way to merge our codes?

Your Add Button On Click:
__________________________________________________
Private Sub AddIncident_Click()

Me!chkOKToClose = True

End Sub
__________________________________________________
To my current Add Button On Click:
__________________________________________________
Private Sub AddIncident_Click()

On Error GoTo Err_AddIncident_Click

DoCmd.GoToRecord , , acNewRec

Exit_AddIncident_Click:
Exit Sub

Err_AddIncident_Click:
MsgBox Err.Description
Resume Exit_AddIncident_Click
End Sub
__________________________________________________


Your Form On Current:
__________________________________________________
Private Sub Form_Current()

Me!chkOKToClose = False

End If
__________________________________________________

To my Form On Current:
__________________________________________________
Private Sub Form_Current()

PreviousIncident.Enabled = Not Me.CurrentRecord = 1
NextIncident.Enabled = Not Me.CurrentRecord > Me.RecordsetClone.RecordCount

End Sub
_________________________________________________


Your Close Button On Click:
_________________________________________________

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
If Me!chkOKToClose = False Then
iAns = MsgBox("Please use the Add Incident button to save the record" _
& " or select Cancel to erase the form and start over", vbOKCancel)
Cancel = True ' cancel adding the record
If iAns = vbCancel Then
Me.Undo ' erase the form
End If
End If
End Sub
_________________________________________________

To my Close Button On Click:
_________________________________________________
Private Sub Close_Click()
On Error GoTo Err_Close_Click

Dim stDocName As String
Dim stLinkCriteria As String

If MsgBox("You will no longer be able to make changes to the Incident Log.
Are you sure you want to end this Incident Log Session?", vbQuestion +
vbYesNo, "Close Incident Log") = vbYes Then
DoCmd.Close
DoCmd.OpenForm "frmClosing", , , , acFormAdd

End If

Exit_Close_Click:
Exit Sub

Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click

End Sub
___________________________________________________


Thank you!
 
J

John W. Vinson

John,

What is the proper way to merge our codes?

Private Sub AddIncident_Click()

On Error GoTo Err_AddIncident_Click
Me!chkOKToClose = True
DoCmd.GoToRecord , , acNewRec

Exit_AddIncident_Click:
Exit Sub

Err_AddIncident_Click:
MsgBox Err.Description
Resume Exit_AddIncident_Click
End Sub

Private Sub Form_Current()

PreviousIncident.Enabled = Not Me.CurrentRecord = 1
NextIncident.Enabled = Not Me.CurrentRecord > Me.RecordsetClone.RecordCount
Me!chkOKToClose = False

End Sub

_________________________________________________

Your Close Button On Click:

Nope. That's not the Close Button On Click. That's the Form's BeforeUpdate
event. Just use both.
_________________________________________________

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
If Me!chkOKToClose = False Then
iAns = MsgBox("Please use the Add Incident button to save the record" _
& " or select Cancel to erase the form and start over", vbOKCancel)
Cancel = True ' cancel adding the record
If iAns = vbCancel Then
Me.Undo ' erase the form
End If
End If
End Sub
_________________________________________________

To my Close Button On Click:

Do you want the Close button to erase the contents of the form unless the
AddIncident button has been clicked? How do the two buttons interact for the
user?
_________________________________________________
Private Sub Close_Click()
On Error GoTo Err_Close_Click

Dim stDocName As String
Dim stLinkCriteria As String

If MsgBox("You will no longer be able to make changes to the Incident Log.
Are you sure you want to end this Incident Log Session?", vbQuestion +
vbYesNo, "Close Incident Log") = vbYes Then
DoCmd.Close
DoCmd.OpenForm "frmClosing", , , , acFormAdd

End If

Exit_Close_Click:
Exit Sub

Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click

End Sub
___________________________________________________

John W. Vinson [MVP]
 
P

pushrodengine via AccessMonster.com

John, How do I merge?

This is my code:
_________________________________________________________

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
Dim strWhere As String
Dim varResult As Variant

strWhere = "IncidentID Like """ & Format(Date, "yy") & "*"""
varResult = DMax("IncidentID", "tblIncidentLog", strWhere)

If IsNull(varResult) Then
Me.IncidentID = Format(Date, "yy") & "-0001"
Else
Me.IncidentID = Left(varResult, 3) & _
Format(Val(right(varResult, 4)) + 1, "0000")
End If
End If
End Sub
___________________________________________________________

Your code:
___________________________________________________________
Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim iAns As Integer
If Me!chkOKToClose = False Then
iAns = MsgBox("Please use the Add Incident button to save the record" _
& " or select Cancel to erase the form and start over", vbOKCancel)
Cancel = True ' cancel adding the record
If iAns = vbCancel Then
Me.Undo ' erase the form
End If
End If
End Sub
____________________________________________________________

Do you want the Close button to erase the contents of the form unless the
AddIncident button has been clicked? How do the two buttons interact for the
user?

Yes, I would like the Close button to erase the contents of the form.


Thank you very much for your help!
 
P

pushrodengine via AccessMonster.com

I merge the two codes like this:
______________________________________________________________________
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
Dim strWhere As String
Dim varResult As Variant
Dim iAns As Integer

If Me!chkOKToClose = False Then
iAns = MsgBox("Please use the Add Incident Button to save the
Incident" & " or Select Cancel to Re-enter Incident Log", vbOKCancel, "Re-
enter Incident Log")
Cancel = True
If iAns = vbCancel Then
Me.Undo

End If
End If

strWhere = "IncidentID Like """ & Format(Date, "yy") & "*"""
varResult = DMax("IncidentID", "tblIncidentLog", strWhere)

If IsNull(varResult) Then
Me.IncidentID = Format(Date, "yy") & "-0001"
Else
Me.IncidentID = Left(varResult, 3) & _
Format(Val(right(varResult, 4)) + 1, "0000")
End If
End If
End Sub
_____________________________________________________________________

It works, but when the Cancel button is pressed I would like it to return to
the form so that information may be re-entered.

Thanks
 

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

Top