Close with the "X" button during Form data entry / edit

  • Thread starter Thread starter Paul Mars
  • Start date Start date
P

Paul Mars

Several fields have validation. Others are required before record can be
saved. Others will cascade deleted or changed data. If user tries to save,
move to other record, close form using the btnClose, or move to other field;
I have user friendly MsgBoxes that inform the user and sometimes give the
user a choice(YesNoCancel). This also works with the btnClose. That is, if
user tries to close the form before leaving the field, my MsgBox will show,
then whatever button on my MsgBox the user chooses, the close is canceled.
Not canceled by me, it just happens that way and this is good. HOWEVER, if
user clicks the blasted "X" another error message appears after my MsgBox.
This message allows the user to cancel the close, or close without saving.
Either one is ok, except this message confuses the users. How can I not show
this message can cancel the close? (mimic the BtnClose event)

I know that I can set a Global variable as Boolean, but then I must either
set it to false on load, check it on Un_Load, and then check all validated
and required fields on close before setting it to true, or just set it to
false if fail validation / required and set it to true if pass.

Is there an easier way?? I tried setting above variable to false on
Form_Error, but that did not work. Maybe it needs to be a field error, but
that takes me back to individual fields code. Is there a way without adding
event code to all effected fields?

Paul
 
I did.

Below is my Field_BeforeUpdate event. Following this is my Form_BeforeUpdate
event, then is my Form_BeforeDelConfirm, then Form_Delete. All behave the
same. That is, they all work fine, unless the user hits the "X" after making
changes and before leaving the field, as detailed in my first post.

Private Sub Types_BeforeUpdate(Cancel As Integer)
'if user removed the entry, replace generic msg and restore value:
If IsNull(Types) Then
MsgBox ("This can not be empty." & Chr(13) & Chr(13) & _
"To delete this Massage type, click the 'Record Selector'" _
& Chr(13) & Chr(13) & "to its left and press the 'Delete' key.")
Cancel = True 'stops generic msg and allows Undo to restore value
Me!Types.Undo
Else
If Types.OldValue <> Types.Value Then
'tell user how many related records and allow user to cancel:
If RelatedRecords(Types.OldValue) > 0 Then
If MsgBox("Are you sure that you wish to change this?" & _
Chr(13) & Chr(13) & "There are: " & _
RelatedRecords(Types.OldValue) & " Contact(s) with this
Massage type." _
& Chr(13) & Chr(13) _
& "If you choose 'OK', these Contact(s) will be changed
to match." _
, vbOKCancel, "WARNING") = vbOK Then
Else
Cancel = True
Me!Types.Undo
End If
End If
End If
End If
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
'either Client or Business ckBox needs to be Yes:
If Client = "0" And Business = "0" Then
MsgBox ("Either Client or Business should be checked," & _
Chr(13) & "otherwise this entry will receive no mail or e mail.")
Cancel = True
'CheckClose = False
End If
End Sub

Private Sub Form_BeforeDelConfirm(Cancel As Integer, Response As Integer)
'turn off default warnings and supply consistant English warning:
If MsgBox("Are you sure that you wish to delete this record?" & _
Chr(13) & "Once deleted, Undo will not bring it back.", _
vbOKCancel, "WARNING") = vbOK Then
DoCmd.SetWarnings False
Else
Cancel = True
End If
End Sub

Private Sub Form_Delete(Cancel As Integer)
'tell user how many related records and allow user to cancel,
'and turn off default "related records" warning:
If RelatedRecords(Types.Value) > 0 Then
If MsgBox("Are you sure that you wish to delete this?" & _
Chr(13) & Chr(13) & "There are: " & RelatedRecords(Types.Value)
_
& " Contact(s) with this Massage type." & Chr(13) & Chr(13) _
& "If you Choose 'OK', this Massage type will be removed from
these Contact(s)" _
& Chr(13) & Chr(13) & "and Undo or re-entering this Massage type
will not re-enter it for these Contact(s)." _
, vbOKCancel, "WARNING") = vbOK Then
DoCmd.SetWarnings False
Else
Cancel = True
End If
Else
DoCmd.SetWarnings False
End If
End Sub

Private Sub Form_AfterDelConfirm(Status As Integer)
'reactive default warnings after Form_Delete below:
DoCmd.SetWarnings True
End Sub
 
Hi Paul,
ok, now i got what you mean. what exactly this message says:
"user clicks the blasted "X" another error message appears after my MsgBox"
?
 
you can't save this record at this time.

MS Access may have encountered an error while trying to save a record.
If you close this object now, the data changes that you made will be lost.
Do you want to close the db object anyway?

Choices Yes, No.

There are no changes, since this msgbox comes up after my msgbox which the
user responded to before this above box showed. So choosing No just stops
Form from closing, Yes lets the form close. Data is the same either way.
Just somehow the Form things there is data changed. If I put a Dirty =
False, that does not help.
 
well, if it says "you can't save this record at this time" - then some field
is null or say some combo not filled. so i think you can try to either set
default values for required fields, or in beforeupdate event check if all
required fields entered and if no - then cancel event. then you will not see
this message
 
neither condition needs to be present for me to get this MsgBox.

1-I make a data change to the Field.
2-I tab out of the field.
3-If there are related records, them my MsgBox warns the user and the user
has a choice to keep their change to this field or cancel the data change.
After the user chooses one of these options, it is processed in code and all
it done.

OR

1-Same as above.
2-Hit the Form "X" and #3 above happens.
3-If user choose to accept the changes (my MsgBox Yes) then the Form closes,
However if user choice to cancel the data changes (MsgBox Cancel) then user
is presented with that Access "you can't save this record at this time"
MsgBox. Since the user choice Cancel, my code has already processed this. So
the user can choose Yes or No to the Access MsgBox, either way the data is
left as my code already processed it.

Same with Delete Field data.




If there are related
 
i think you can to make a form-level variable, if case record cant be
saved - you set it to true and then assign cancel to it in unload form
event. something like this
 
Back
Top