Re: Cancel = True still letting user exit



To prevent a form from closing you have to use the Form_Unload event. In
this case you will also need to create a module-level boolean variable that
will be available to all events within the form. The following is air-code,
but should be pretty close to forcing a "good" record to be present in order
for the form to close.

1) At the top of the form's code module (after any Option statements but
before any procedures or functions:
Dim mOKToClose as Boolean


2) In the Form_Unload event:

If mOKtoClose Eqv False Then
MsgBox "You must fix the current record before closing the form.",
vbOKOnly, "Can't Exit"
Cancel = True
End If

3) In your Form_BeforeUpdate event:
(restructured to present multiple messages in a single Msgbox, if
necessary. vbcrlf is a carriage-return & linefeed, placing each message part
on a new line.)
Dim strMessage as String

If AntecedentCheckBoxSum = 0 Then
strMessage = "AT LEAST ONE ANTECEDENT MUST BE CHECKED." & vbcrlf
End If

If BehaviorCheckBoxSum = 0 Then
strMessage = strMessage & "AT LEAST ONE BEHAVIOR MUST BE CHECKED." &
vbcrlf
End If

If ConsequenceCheckBoxSum = 0 Then
strMessage = strMessage & "AT LEAST ONE CONSEQUENCE MUST BE
CHECKED."& vbcrlf
End If

If IntensityCheckBoxSum = 0 Then
strMessage = strMessage & "AT LEAST ONE INTENSITY MUST BE CHECKED."
End If

If Len(strMessage)>0 Then
MsgBox strMessage
Cancel = True 'Prevents record from saving & movement to
another record
mOKToClose = False 'Prevents form from closing
Else
mOKToClose = True
End If

4) In the Form_Current event:
mOKToClose = Not Me.NewRecord
(If this is an existing record then we need to set mOKToClose to
True here or the form can't ever be closed unless the record is modified
(which would force BeforeUpdate to fire eventually).

HTH,


"Christy Wyatt" <ChristyWyatt@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:F0C22148-9B9B-4A28-80D1-7121FF0DEEBA@xxxxxxxxxxxxxxxx
I was coding a form with groups of checkboxes that must have at least one
checked before the user can get out of the form. I used the "Cancel =
True"
code just as I was supposed to. It tells them they must check at least
one,
just like it is supposed to, but if they say OK, it gives them a choice to
either go back into the form or exit without saving. I can't afford to
give
them a choice.

Private Sub Form_BeforeUpdate()

If AntecedentCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE ANTECEDENT MUST BE CHECKED."
Cancel = True
End If

If BehaviorCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE BEHAVIOR MUST BE CHECKED."
Cancel = True
End If

If ConsequenceCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE CONSEQUENCE MUST BE CHECKED."
Cancel = True
End If

If IntensityCheckBoxSum = 0 Then
MsgBox "AT LEAST ONE INTENSITY MUST BE CHECKED."
Cancel = True
End If
End Sub
--
Christy Wyatt


.



Relevant Pages

  • Re: Cancel = True still letting user exit
    ... Private Sub PrintBehReptBut_Click ... If mOKToClose Eqv False Then ... MsgBox "You must fix the current record before closing the form." ... Cancel = True ...
    (microsoft.public.access.forms)
  • Re: Cancel = True still letting user exit
    ... Christy Wyatt ... If mOKtoClose Eqv False Then ... MsgBox "You must fix the current record before closing the form.", ... Cancel = True ...
    (microsoft.public.access.forms)
  • RE: check qry on form close for null entrys
    ... show the MsgBox and not restrict the person from closing. ... I tried setting up a query ... cancel the close and present the records will Nulls in the form. ...
    (microsoft.public.access.gettingstarted)
  • On Close Event
    ... a msgbox is displayed and the form should not be closed. ... How do I tell the On Close event to cancel if a certain condition is not ... the cancel = true statemetn does not stop the form from closing) ...
    (microsoft.public.access.formscoding)
  • RE: AfterUpdate Property
    ... MsgBox "You must enter the time worked on the Pending Bill" ... Cancel = True ... "Melissa" wrote: ... MsgBox "TextBox 2 must be filled if Text Box 1 Is" ...
    (microsoft.public.access.formscoding)