I have spent the whole day trying to figure this thing out and must say my patience has run out.
This is very simple logic. I have a form that opens as a popup and a new record when the user clicks a button (open form).
If the record is new and no data is inputted, when the user closes the form (either by clicking X or clicking a close form button) no record is created in the table (this is fine)
If the user opens an already existing record to view information and make no changes, the form should close just as it opened without any prompts.
However if a new record is created and details inputted into the form, if the user closes without hitting the save button, i want a prompt box to popup asking if they wish to exit without saving. If they hit yes, the created record is deleted and the form closes. If they hit no, the form close action is cancelled. The same is also true if they open an already existing record and make changes to it before closing.
I have tried every last scenario of code and some create records where they shouldnt be and other ask to delete existing records (as part of my code - see below) where i dont want them to. Can someone please help with this.
sample code
This above may or may not make sense so please let me know if you need any more info on this.
Thank you.
This is very simple logic. I have a form that opens as a popup and a new record when the user clicks a button (open form).
If the record is new and no data is inputted, when the user closes the form (either by clicking X or clicking a close form button) no record is created in the table (this is fine)
If the user opens an already existing record to view information and make no changes, the form should close just as it opened without any prompts.
However if a new record is created and details inputted into the form, if the user closes without hitting the save button, i want a prompt box to popup asking if they wish to exit without saving. If they hit yes, the created record is deleted and the form closes. If they hit no, the form close action is cancelled. The same is also true if they open an already existing record and make changes to it before closing.
I have tried every last scenario of code and some create records where they shouldnt be and other ask to delete existing records (as part of my code - see below) where i dont want them to. Can someone please help with this.
sample code
Code:
Private Sub Form_Unload(Cancel As Integer)
If Me.Form.CurrentRecord Then
Cancel = False
Else
If Me.Form.NewRecord = True Then
Cancel = False
Else
If Me.Form.NewRecord = False Then
response = MsgBox(prompt:="All unsaved data will be lost are you sure you want to close?", buttons:=vbYesNo)
If response = vbYes Then
DoCmd.RunCommand acCmdDeleteRecord
Cancel = False
Else
If response = vbNo Then
Cancel = True
Else
Cancel = True
End If
End If
End If
End Sub
This above may or may not make sense so please let me know if you need any more info on this.
Thank you.