Cancel = True still letting user exit

G

Guest

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
 
G

George Nicholson

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,
 
G

Guest

I'm going to try this in the next couple of days and get back with you.
Thanks for writing.
 
G

Guest

Thank you for writing back. You put a lot of thought into what you sent in.
I think I have done everything you listed, and yet the following things
happen.

When I try to exit an empty record, the last warning regarding antecedents
comes up. It prevents me from exiting the form. I can go back in, and enter
anything at all in any field and it lets me close without any warning. This
is the code for the form:

Dim mOKToClose As Boolean

Private Sub PrintBehReptBut_Click()
On Error GoTo Err_PrintBehReptBut_Click


DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
DoCmd.PrintOut acSelection

Exit_PrintBehReptBut_Click:
Exit Sub

Err_PrintBehReptBut_Click:
MsgBox Err.Description
Resume Exit_PrintBehReptBut_Click

End Sub
Private Sub CloseBehRptBut_Click()
On Error GoTo Err_CloseBehRptBut_Click

DoCmd.Close

Exit_CloseBehRptBut_Click:
Exit Sub

Err_CloseBehRptBut_Click:
MsgBox Err.Description
Resume Exit_CloseBehRptBut_Click

End Sub

Private Sub Form_AfterUpdate()
If mOKToClose Eqv False Then
MsgBox "You must fix the current record before closing the form."
vbOKOnly , "Can't exit"
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim strMessage As String
If AntecedentCheckBoxSum = 0 Then
StrMssage = "AT LEAST ONE ANTECEDENT MUST BE CHECKED." & vbCrLf
End If
If BehaviorCheckBoxSum = 0 Then
StrMssage = "AT LEAST ONE BEHAVIOR MUST BE CHECKED." & vbCrLf
End If
If ConsequenceCheckBoxSum = 0 Then
StrMssage = "AT LEAST ONE CONSEQUENCE MUST BE CHECKED." & vbCrLf
End If
If IntensityCheckBoxSum = 0 Then
StrMssage = "AT LEAST ONE INTENSITY MUST BE CHECKED." & vbCrLf
End If
If Len(strMessage) > 0 Then
MsgBox strMessage
Cancel = True
mOKToClose = False
Else
mOKToClose = True
End If

End Sub

Private Sub Form_Current()
mOKToClose = Not Me.NewRecord
End Sub

Private Sub Form_Unload(Cancel As Integer)
If mOKToClose Eqv False Then
MsgBox "You must fix the current behavior report before closing.",
vbOKOnly, "Can't Exit"
Cancel = True
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top