Re: Cancel = True still letting user exit
- From: "George Nicholson" <GeorgeNJunk@xxxxxxxxxxx>
- Date: Mon, 19 Feb 2007 16:53:11 -0600
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
.
- Follow-Ups:
- Re: Cancel = True still letting user exit
- From: Christy Wyatt
- Re: Cancel = True still letting user exit
- Prev by Date: Re: help creating shortened form of a field
- Next by Date: Re: No fields when creating forms
- Previous by thread: Losing Focus
- Next by thread: Re: Cancel = True still letting user exit
- Index(es):
Relevant Pages
|