Access VBA Preventing form record entry on close - vba

I am working with Access Database VBA.
I have noticed if a user has filled a few of the text boxes in and then clicks the windows close button, the form logs that into the records.
I am wondering what is the best way to prevent the form from entering the uncompleted record on close?
There were a few sites pointing to placing a code in the beforeupdate function.
This is what I have tried.
Private Sub frmRecLog_BeforeUpdate(Cancel As Integer)
DoCmd.SetWarnings False
Me.Undo
Cancel = True
Exit Sub
End Sub
This code does not work at all for me.. I tried my best, haha.
Anything helps.

So, what you need is to insert a command button Save. If user do not hit on Save then it will not save any records. They will get a warning that data is not saved. You need declare a private boolean variable and write codes to save and warning. So full code will be like below.
Option Compare Database
Option Explicit
Private blnSaveRecord As Boolean
Private Sub cmdSave_Click()
blnSaveRecord = True
DoCmd.RunCommand (acCmdSaveRecord)
blnSaveRecord = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not blnSaveRecord Then
Cancel = True
strMsg = "Please save the record.," & _
vbNewLine & "or press ESC from keyboard to cancel the operation."
Call MsgBox(strMsg, vbInformation, "Save Record")
'Me.Undo 'You can set undo option here if you do not want to press ESC.
End If
End Sub

Related

Save changes in form based on temp table using native close button

I have a form where users can edit multiple different pieces of data. This form is based on data pulled from several underlying tables and placed into a temporary table. When a user makes a change and hits the "save" button, any changes made in the form get updated/appended to the underlying data tables and everything in the temp table gets dumped. This all works great.
My problem is with the native close button on the form. If a user makes a change, forgets to hit save, and instead just closes the form, nothing gets saved. I think it's because I need to run my update and append queries (which is what happens with my save button) but I'm unsure how to incorporate them in so they run with the native close button. When I try to add them to the Form_Close event, I get an error message that something in the BeforeUpdate event is preventing Access from saving the data. I know I could remove the close button from the form, but if possible, I would rather leave it in.
Form_BeforeUpdate
txtOld = Nz(Me.Document.OldValue, "")
If Not (Me.NewRecord) Then
If MsgBox("Changes have been made to this record. Save changes?", vbYesNo, "") = vbNo Then
DoCmd.RunCommand acCmdUndo
End If
End If
cmdSave_Click
On Error GoTo Error_Handler
DoCmd.RunCommand acCmdSaveRecord
'check to see if new book field has changed and run append query if it has
If txtOld <> Me.Document.Value Then
DoCmd.OpenQuery "qryUpdateNewBookList"
End If
'This update query should run every time
DoCmd.OpenQuery "qryEditUpdate"
DoCmd.Close acForm, Me.Name, acSaveNo
Form_Close
DoCmd.RunSQL ("DELETE tblEditTbl.* FROM tblEditTbl")
One quick tips. You can use below code to through a message that data is not saved.
Private Sub cmdClose_Click()
If Me.Dirty = True Then
MsgBox "Data is not saved. Save data first then try to close", vbExclamation
Exit Sub
Else
MsgBox "Data saved successfully."
DoCmd.Close
End If
End Sub
Edit: If you want to check it before update then you have to use a variable to check if data is saved or not. Declare a variable at top of form code module like
Option Compare Database
Private blnSaveRecord As Boolean
Use below codes to Save button.
Private Sub cmdSave_Click()
blnSaveRecord = True
DoCmd.RunCommand acCmdSaveRecord
blnSaveRecord = False
End Sub
And finally write below codes to Form_BeforeUpdate event.
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not blnSaveRecord Then
Cancel = True
strMsg = "Please save the record.," & _
vbNewLine & "or press ESC from keyboard to cancel the operation."
MsgBox strMsg, vbInformation, "Save Record"
End If
End Sub
I solved my problem. Harun24HR got me thinking in the right direction. In addition to my existing code, I needed to use the Form_Unload event and create two public boolean variables to check if the save button had been clicked and if any edits had been made in the form.
Form_Unload
If Not SaveClicked And IsDirty Then
MsgBox "Data is not saved. Save data first and then try to close.", vbExclamation
Cancel = True
End If

How to execute code when user closes form?

I want a msg box to appear if certain fields are empty on close.
Private Sub Close_Click()
If IsNull(Me.startDate) Then
MsgBox “You are in Phase 1”
End If
End Sub
I want the pop up to appear when user clicks the exit x. I tried OnClose. Right now the code is attached to and works via a button.
I’d like the code to be executed when the user closes the form.
Edit New Code:
Private Sub Exit_Click ()
If IsNull(Me.startDate) Or IsNull(Me.stepOneA) Or IsNull(Me.stepOneB) Then
MsgBox “You are in Step I”
DoCmd.Close
ElseIf IsNull(Me.stepTwoA) or IsNull(Me.stepTwoB) or IsNull(stepTwoC)
MsgBox “You are in Step II”
DoCmd.Close
Else
DoCmd.Close
EndIf
End Sub
You can use the UnLoad event for this:
Private Sub Form_Unload(Cancel As Integer)
Cancel = IsNull(Me.startDate)
If Cancel = True Then
MsgBox "You are in Phase 1."
End If
End Sub

Updating/adding entry to table only when question is answered?

I'm setting up a database in MS Access 2013, and want to ask the user a yes/no if they want to save or discard their non-saved record or edit before navigating away from the current record in my Access form.
The user should not be met with the question if pressing either the "add record" or "save" buttons.
Can someone point to where my problematic code is / or what I need?
Also, I'm new to Access, so please be gentle.
I have tried a few different guides or other answers around the web, but haven't gotten exactly to where I want to be.
My code is as such (cbotxt_Change and Form_Load relate to other parts of the form)
Private blnGood As Boolean
Option Compare Database
Private Sub cbotxt_Change()
Me.txt1.Value = Me.test1.Column(2)
Me.txt2.Value = Me.test1.Column(3)
Me.txt3.Value = Me.test1.Column(4)
End Sub
Private Sub Form_Load()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub button_addRecord_Click()
blnGood = True
DoCmd.GoToRecord , , acNewRec
blnGood = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
If Not blnGood Then
strMsg = "want to abort?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "Yes") = vbYes Then
Me.Undo
Else
Yes = True
End If
End If
End Sub
Using the code above, "want to abort?" is asked whenever the user is attempting to navigate away from the current record + when "add record" or "save" buttons are pressed. If the user answers "No", then the entry will save and the action in question be performed, except the "add record" button, which only seems to save now, not add a new record.
Seems a little convoluted to me. Try with:
Private Sub button_addRecord_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMsg As String
strMsg = "want to abort?"
If MsgBox(strMsg, vbYesNo + vbQuestion, "New Entry") = vbYes Then
Cancel = True
Me.Undo ' or let the user press Escape.
End If
End Sub

Can I add a UI action confirmation?

I have a form that displays a record (about 40 fields). I have a button to duplicate the record in instances where a slight change is needed (i.e. A541AB becomes A541AC). I've seen a couple of instances of solutions on the interwebs but I couldn't find one that works for this UI action. Is it possible to create a confirmation box asking if they're sure they want to duplicate the record? Currently, the button is designed using a macro since I'm not very good with Access VBA.
You need to do this in VBA by setting a flag to handle the auto-update. Upon clicking the button ask the user and save if the answer is Yes.
'Set a flag for manual update
Private mIsUserUpdate As Boolean 'Flag
'Cancel auto-update
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not mIsUserUpdate Then Cancel = True
End Sub
'Save Button - Change 'YourButtonName'
Private Sub YourButtonName_Click()
If MsgBox("Are you sure you want to duplicate the record?", vbYesNo + vbQuestion, "Confirm") = vbYes Then
mIsUserUpdate = True 'flag ON
DoCmd.RunCommand acCmdSaveRecord
End If
mIsUserUpdate = False 'flag OFF again
End Sub

Closing a bound form without saving changes

Quick question here, hoping for a concise sensible solution.
I have a bound form purely for data entry (cannot browse records, only insert them).
I will have a lot of users that screw up. To avoid dirty data, I want them to confirm that the form is correct before submitting the record.
Problem is, as soon as I input ANYTHING on the form, access creates and saves a record.
I want the record to ONLY be saved and committed if the user clicks 'Submit'. If they click close or exit the application, I do not want that partially completed record in the database.
Without using an unbound form and calling an insert function, is there a simple solution?
An autonumber is there to be unique, not sequential. If you need a sequential number, do not use autonumber. Autonumber should never be shown to the user. It can never be relied upon to be anything but unique, and if you mess about enough, not even that.
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.AText = "Invalid" Then
Me.Undo
Cancel = True
End If
End Sub
Note that a form with a subform may not work with undo, because the record is committed on change from the subform to the main form and vice versa and it all gets quite complicated.
Remou's method is definitely the quickest, here is another based on my comment ;)
Option Explicit
Private blnGood As Boolean
Private Sub cmdSubmit_Click()
blnGood = True
Call DoCmd.RunCommand(acCmdSaveRecord)
blnGood = False
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not blnGood Then
Cancel = True
Call MsgBox(Prompt:="click submit to save the record", Title:="Before Update")
End If
End Sub
You can use the following code to create a clear button in case the user made an errors and wants to clear the entire form and start over.
Private Sub btnClear_Click()
If Me.Dirty = True Then
DoCmd.RunCommand acCmdUndo
Exit Sub
End If
End Sub`
I sometimes find the before_update event to act weird so I generally disable the close (x) button in properties and add a close button of my own that prompts the user if he wants to abandon the data on screen.
Private Sub close_Click()
Dim Answer As Integer
If Me.Dirty = True Then
Dim Response As Integer
' Displays a message box with the yes and no options.
Response = MsgBox(Prompt:="Do you wish to discard data?", Buttons:=vbYesNo)
' If statement to check if the yes button was selected.
If Response = vbYes Then
DoCmd.RunCommand acCmdUndo
DoCmd.Close
Else
Me.Clear.SetFocus
End If
Else
' The no button was selected.
DoCmd.Close
End If
End Sub