Solved Unable to Stop the bounded form from adding unwanted records (1 Viewer)

mrk777

Member
Local time
Today, 09:55
Joined
Sep 1, 2020
Messages
60
Hi Team, Good evening.

I have a bounded form with about 34 fields and I have a "Save" button (to just save the details to the master table) and a "Home" button (to go back home without saving the record when there are any changes made after it is saved). But some users are messing it up by just adding few fields and if they don't want to add a record, they simply click on the "Home" Button, but the form adds that data too. I have written the below code which is not helping me, can you please help me out?


Code:
Private Sub Command325_Click()
'Home Button
If Me.Dirty And Me.Project_Name = "" Then
    Me.Undo
        'DoCmd.RunCommand acCmdUndo
Else:
    Me.Dirty = False
    'Me.lbl_LastSaved.Caption = "Saved: " & Date & " " & Time
End If

DoCmd.Close acForm, Me.Name, acSaveNo
Call MaximizeForm
End Sub
 

bob fitz

AWF VIP
Local time
Today, 05:25
Joined
May 23, 2011
Messages
4,719
Perhaps some code in the forms Before Update event to ask the user to confirm that changes should be saved.
If they do not confirm then the update event can be canceled.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:25
Joined
Sep 21, 2011
Messages
14,238
I use Esc key when (I do not want to commit the record.
I would use the BeforeUpdate even to stop the Save.

Personally if you want a Save button, make the controls unbound and all the extra work that entails.?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:25
Joined
May 7, 2009
Messages
19,229
you can use Transaction on your validation to Commit or Rollback your dataentry.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:25
Joined
May 21, 2018
Messages
8,525
You may want to use a modify version of this from @arnelgp. I use a version of this often. This is a good way to prompt the user to fill out the required fields. Works real well and super simple to employ. You may want to tweak it so if they purposely want to close they have a way out. You can do this in the close button to return or close without saving.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:25
Joined
Feb 19, 2002
Messages
43,233
This is not a case for a transaction. Use the Form's BeforeUpdate event to validate the data and prevent the record from being saved. When you find a validation error, use:

Cancel = True to cancel the save. The form remains dirty and the user can fix his error. Only use Me.Undo if the user is not allowed to update at all or answers yes to a cancel the save prompt. Undoing what the user typed is not user friendly and should only be done in the two cases I suggested.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:25
Joined
Feb 28, 2001
Messages
27,148
I would point out that any accidental navigation would automatically trigger a SaveRecord operation. The only REAL way that I know to do this reliably is to put something in the Form_BeforeUpdate event routine that will know whether this Save is due to a button click or an accident. That is the only event that would be able to cancel the update.
 

mrk777

Member
Local time
Today, 09:55
Joined
Sep 1, 2020
Messages
60
I would point out that any accidental navigation would automatically trigger a SaveRecord operation. The only REAL way that I know to do this reliably is to put something in the Form_BeforeUpdate event routine that will know whether this Save is due to a button click or an accident. That is the only event that would be able to cancel the update.
Definitely, I’ll try this out and let you the output. 👍🏻
 

mrk777

Member
Local time
Today, 09:55
Joined
Sep 1, 2020
Messages
60
I know there is something in what I'm doing. The below code is not allowing me to save even though I click on the save button.
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Dirty Then
Cancel = True
MsgBox "Please use the save or cancel buttons.", vbOKOnly
Exit Sub
End If

End Sub

For Saving I have used the below code:

Code:
Private Sub Command321_Click()
    If Me.Dirty Then

    If MsgBox("Do you want to save the changes?", vbYesNo + vbQuestion, "Save Changes") = vbNo Then
        Me.Undo
        'DoCmd.RunCommand acCmdUndo
    Else:
        Me.Dirty = False
        Me.lbl_LastSaved.Caption = "Saved: " & Date & " " & Time
    End If

End If
End Sub

As this is a bounded form, I have used these codes, please correct me if I'm going wrong somewhere.
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:25
Joined
Sep 21, 2011
Messages
14,238
Well you have
Code:
Cancel = True
in the form's BeforeUpdate event?????
 

Mike Krailo

Well-known member
Local time
Today, 00:25
Joined
Mar 28, 2020
Messages
1,043
How about this:

Code:
Dim SaveClicked As Boolean

Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Not SaveClicked Then
      Cancel = True
   End If
   SaveClicked = False
End Sub

Private Sub Form_Load()
   SaveClicked = False
End Sub

Private Sub SaveBtn_Click()
   SaveClicked = True
   If Me.Dirty Then

      If MsgBox("Do you want to save the changes?", vbYesNo + vbQuestion, "Save Changes") = vbNo Then
         Me.Undo
         'DoCmd.RunCommand acCmdUndo
      Else
         Me.Dirty = False
         Me.lbl_LastSaved.Caption = "&Saved: " & Date & " " & Time
      End If
   End If
End Sub
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:25
Joined
Feb 28, 2001
Messages
27,148
Several folks have pointed out ideas on this theme. Basically, if you are really serious about stopping unwanted saves, stop them where they occur, in the form's Update action (for which there is no separate events - we only see BeforeUpdate and AfterUpdate). However Mike Krallo's suggestion is almost right. I would reset the form's memory of the button click in the Form_Current event, not the Form_Load event.
 

Mike Krailo

Well-known member
Local time
Today, 00:25
Joined
Mar 28, 2020
Messages
1,043
I see what you mean Doc. Because as soon as you go to the next record, it needs to be reset again.
 

mrk777

Member
Local time
Today, 09:55
Joined
Sep 1, 2020
Messages
60
How about this:

Code:
Dim SaveClicked As Boolean

Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Not SaveClicked Then
      Cancel = True
   End If
   SaveClicked = False
End Sub

Private Sub Form_Load()
   SaveClicked = False
End Sub

Private Sub SaveBtn_Click()
   SaveClicked = True
   If Me.Dirty Then

      If MsgBox("Do you want to save the changes?", vbYesNo + vbQuestion, "Save Changes") = vbNo Then
         Me.Undo
         'DoCmd.RunCommand acCmdUndo
      Else
         Me.Dirty = False
         Me.lbl_LastSaved.Caption = "&Saved: " & Date & " " & Time
      End If
   End If
End Sub
This trick is working perfectly, Thank you @Mike Krailo. All of a sudden, my system had crashed, so was unable to check and reply back. Thank you so much :)
 

Mike Krailo

Well-known member
Local time
Today, 00:25
Joined
Mar 28, 2020
Messages
1,043
Your welcome, and thanks to Doc for making the suggestion. That Form_BeforeUpdate event is a very important and useful one to master. Here is the updated code to take into account Doc's suggestion to use the On Current to reset the variable.

Code:
Dim SaveClicked As Boolean

Private Sub Form_BeforeUpdate(Cancel As Integer)
   If Not SaveClicked Then
      Cancel = True
   End If
   SaveClicked = False
End Sub

Private Sub Form_Current()
   SaveClicked = False
End Sub

Private Sub Form_Load()
   SaveClicked = False
End Sub

Private Sub SaveBtn_Click()
   SaveClicked = True
   If Me.Dirty Then

      If MsgBox("Do you want to save the changes?", vbYesNo + vbQuestion, "Save Changes") = vbNo Then
         Me.Undo
         'DoCmd.RunCommand acCmdUndo
      Else
         Me.Dirty = False
         Me.lbl_LastSaved.Caption = "Last Saved: " & Date & " " & Time
      End If
   End If
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 00:25
Joined
Feb 19, 2002
Messages
43,233
then his Button on the Form is Useless?
@arnel, The OP wants to prevent saving a record if it is incomplete. The only RELIABLE way to do that is to put the validation code in the form's BeforeUpdate event so the save can be cancelled if there is an error. Putting validation code in control events works for some things but NOT for validating presesce. If focus never enters a control NO control events fire. If the control is entered but the control isn't dirtied, the BeforeUpdate event does not fire so you cannot reliably use control level events to ensure that any control that requires a value has one.

The form is bound. It is already wrapped in a transaction. If the form were unbound you could use a transaction around your update code but unless the fields are defined as required, there is no getting around the requirement to validate.

@mrahulkanth, I have told you how to control whether or not a record should be saved and others have concurred and even supplied code. You can of course add a save button if you wish and force the users to use it but I think that is over kill. You want to avoid sending spurious messages to the user. All that does is to train them to click to get past the message and they stop reading the messages which causes them to ignore important ones. Just because a user jumps through the hoop and presses your save button doesn't remove you of the obligation to actually validate data. As the developer you have to ensure that data is as valid as you can. Dates (all of them) need to be checked at a minimum for a valid range. 3/6/202 is a valid date as far as Access is concerned but unless your app is dealing with ancient history, this date would not be logically valid and yet it is a very common typo that you can prevent if you take the effort. If the only valid options are Blue and Red, then the user should not be able to enter Green. You can't do anything about him entering Blue when he meant Red but you've done your duty just getting that far. Every app is different so I can't tell you what you need to edit and what rules you should employ, your users would be a bid help for this. For the Blue/Red issue, I would use a combo that only presents the two choices. That means you can avoid having to edit data entered via combos. The only edit would be if the app required a value then you would ensure that a value was entered but any value from the combo would be valid so as long as you have set limit to list to yes, then you don't have to go any further.

In the BeforeUpdate event, when you find an error, you can give the user two options but make sure your message is clear. One option is to return and fix the error, the second can be to cancel the save. If the user elects the cancel option, you would use Me.Undo to back out the users edits so he can gracefully leave the form.
 

mrk777

Member
Local time
Today, 09:55
Joined
Sep 1, 2020
Messages
60
@arnel, The OP wants to prevent saving a record if it is incomplete. The only RELIABLE way to do that is to put the validation code in the form's BeforeUpdate event so the save can be cancelled if there is an error. Putting validation code in control events works for some things but NOT for validating presesce. If focus never enters a control NO control events fire. If the control is entered but the control isn't dirtied, the BeforeUpdate event does not fire so you cannot reliably use control level events to ensure that any control that requires a value has one.

The form is bound. It is already wrapped in a transaction. If the form were unbound you could use a transaction around your update code but unless the fields are defined as required, there is no getting around the requirement to validate.

@mrahulkanth, I have told you how to control whether or not a record should be saved and others have concurred and even supplied code. You can of course add a save button if you wish and force the users to use it but I think that is over kill. You want to avoid sending spurious messages to the user. All that does is to train them to click to get past the message and they stop reading the messages which causes them to ignore important ones. Just because a user jumps through the hoop and presses your save button doesn't remove you of the obligation to actually validate data. As the developer you have to ensure that data is as valid as you can. Dates (all of them) need to be checked at a minimum for a valid range. 3/6/202 is a valid date as far as Access is concerned but unless your app is dealing with ancient history, this date would not be logically valid and yet it is a very common typo that you can prevent if you take the effort. If the only valid options are Blue and Red, then the user should not be able to enter Green. You can't do anything about him entering Blue when he meant Red but you've done your duty just getting that far. Every app is different so I can't tell you what you need to edit and what rules you should employ, your users would be a bid help for this. For the Blue/Red issue, I would use a combo that only presents the two choices. That means you can avoid having to edit data entered via combos. The only edit would be if the app required a value then you would ensure that a value was entered but any value from the combo would be valid so as long as you have set limit to list to yes, then you don't have to go any further.

In the BeforeUpdate event, when you find an error, you can give the user two options but make sure your message is clear. One option is to return and fix the error, the second can be to cancel the save. If the user elects the cancel option, you would use Me.Undo to back out the users edits so he can gracefully leave the form.
Hi @Pat Hartman, you are absolutely right. But here my concern is to stop the record saving into a table if it is not clicked on the save button. As it is bounded form, as soon as the data is entered by the user, it was saved into the table. So this code helps me avoid entering the data into the table if it is not clicked on Save Button. And I have taken the necessary steps to limit the combo box lists etc., :)
 

Users who are viewing this thread

Top Bottom