Solved Unable to Stop the bounded form from adding unwanted records

mrk777

Member
Local time
Tomorrow, 05:07
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
 
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.
 
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.?
 
you can use Transaction on your validation to Commit or Rollback your dataentry.
 
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.
 
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.
 
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. 👍🏻
 
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.
 
Well you have
Code:
Cancel = True
in the form's BeforeUpdate event?????
 
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
 
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.
 
I see what you mean Doc. Because as soon as you go to the next record, it needs to be reset again.
 
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 :)
 
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
 
@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., :)
 
Not trying to muddy the waters here you have the OP well in hand.

For the rest of you, if you are not familiar what @arnelgp is suggesting you should add it to your quiver. I do not understand why @Pat Hartman is so dismissive of this approach. Many advanced Access developers do not understand that you can do transactions in DAO, and that opens up a lot of possibilities. The code that @arnelgp provides is easy to use and can do things way beyond validating a single record. His demo shows canceling a continuous form update. You could go further and cancel multiple child records.

So I find the below statement wrong

@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.
Clearly @arnelgp has shown the before update is not the only way


Not suggesting the OP should use this since it is a more advanced concept. I do recommend others get familiar for something else in the tool box. I have used this in some cases with great success.
 

Attachments

Users who are viewing this thread

Back
Top Bottom