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
Related
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
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
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
Q: What is recommended way of notifing user about loss of functionality unless User change macro security settings?
What I do now:
I display warning on the first sheet user see after opening Workbook, with explanation why things WONT work unless proper settings are set.
And I hide it on start up. (Which wont happen unless settings are OK)
But its not perfect solution:
That message is just one time. (While user could send that sheet to somebody else with different settings...)
Hiding and showing those few rows is treaded by Excel as changing document. (So just opening and closing excel will generate Save changed warning!)
Ok, here is best answer I could think off
(And big thx to #brettdj for suggestion!)
Sub HideMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = True
ThisWorkbook.Saved = Status
End Sub
Sub ShowMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = False
ThisWorkbook.Saved = Status
End Sub
Those are for showing and hiding macros. Saved state is preserved so only user actions can trigger "unsaved changes" dialogbox.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_Open()
Call HideMacroSecWarning
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Call HideMacroSecWarning
End Sub
Those assure that no matter the state of user settings warning will ALWAYS be displayed in SAVED FILE. (So even if for user Exel do not display that warning, somebody else who will open it on different machine will see that warning.)
I have been searching the internet for this all day. But no luck.
Can you setup events for when you enter/leave a tab.
I.E.
OnExit(Tab1)
Do something
Thanks
Depending on your program flow, you might try:
Private Sub Combo3_Exit(Cancel As Integer)
If IsNull(Me.Combo3) Then
MsgBox "No exit"
Cancel = True
End If
End Sub
Private Sub Form_Current()
Me.Combo3.SetFocus
''Or to refer to a subform from the main form
Me.subformcontrolname.Form.Combo3.SetFocus
End Sub
Does the tab contain a subform or only controls from the main form?
A subform has an Exit event, so if you are only concerned that once you have entered the subform you should not leave without completing data, you could:
Private Sub subformcontrolname_Exit(Cancel As Integer)
If IsNull(Me.subformcontrolname.Form.Combo3) Then
Me.subformcontrolname.Form.Combo3.SetFocus
MsgBox "No exit"
Cancel = True
End If
End Sub
While this is not exactly what you want, you could instead of handling the exit simply prevent the user from clicking somewhere else. To do this, attach some code to your combobox1 that bascially set .Enabled=Xfor all elements outside your tab view - where X is determined by the state of the combobox...