I have a form where a user can enter information, hit a cmd button and have that information saved into a table. I am trying to make my form more robust and I'm running into a few problems.
I want to make it so when the user hits the close button (the X in the top right corner), and if there is information in the form, a msgbox will pop up and ask the user if he/she really wants to close the form. If he hits "Yes" then everything works fine. The form closes and the information is not saved in the table. However, if the user says "No" then the form stays open, but the information is added to the database table. Any ideas on how to get this to not happen? My code is a little confusing, but I'll put some up here. Thanks!
'### FORM BEFORE UPDATE ###
Private Sub Form_BeforeUpdate(Cancel as Integer)
If(FormClean=true) or (recsaved=true) Then
'Skip this next part
Else
UserResponse = MsgBox ("Do you want to close?",vbYesNoCancel)
If (UserResponse = 6) Then '### YES ###
Me.Undo
keepopen=False
Else '### NO/CANCEL ###
keepopen=true
GetFormVariables 'Function to store all entries in form in variables
End If
End if
End Sub
'### FORM UNLOAD ###
Private Sub Form_Unload(Cancel as Integer)
If (keepopen=true) then
Cancel=True
SetFormVariables 'Function to return variables to controls on form
ClearFormVariables
Else
Cancel=False
End if
keepopen=false
End Sub
I want to make it so when the user hits the close button (the X in the top right corner), and if there is information in the form, a msgbox will pop up and ask the user if he/she really wants to close the form. If he hits "Yes" then everything works fine. The form closes and the information is not saved in the table. However, if the user says "No" then the form stays open, but the information is added to the database table. Any ideas on how to get this to not happen? My code is a little confusing, but I'll put some up here. Thanks!
'### FORM BEFORE UPDATE ###
Private Sub Form_BeforeUpdate(Cancel as Integer)
If(FormClean=true) or (recsaved=true) Then
'Skip this next part
Else
UserResponse = MsgBox ("Do you want to close?",vbYesNoCancel)
If (UserResponse = 6) Then '### YES ###
Me.Undo
keepopen=False
Else '### NO/CANCEL ###
keepopen=true
GetFormVariables 'Function to store all entries in form in variables
End If
End if
End Sub
'### FORM UNLOAD ###
Private Sub Form_Unload(Cancel as Integer)
If (keepopen=true) then
Cancel=True
SetFormVariables 'Function to return variables to controls on form
ClearFormVariables
Else
Cancel=False
End if
keepopen=false
End Sub