Can't cancel command button close.

K

kagard

Greetings:

I've set up all my forms to check for data in their respective
controls. I've also provided a command button on each form to close
the form. I have a global variable call

The close button has this code behind it:

Private Sub cmdClose_Click()
DoCmd.Close
End Sub

This triggers the form's before update event, which I was hoping would
stop the close process, but it doesn't.:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not FormComplete(Forms!frmCOs) Then
FormReady = False
Cancel = True
End If
End Sub

FormComplete is a function that returns TRUE if the form is complete
and FALSE if it is incomplete. Since that didn't work, I searched this
group and found mention of the form's Unload event. I tried to keep
the form from closing using the following code:

Private Sub Form_Unload(Cancel As Integer)
Dim CloseAnyway As Boolean
If Not FormReady Then
CloseAnyway = MsgBox("You are closing this form with
incomplete information." _
& vbCrLf & "If you close this form, the data you've
entered will be disgarded." _
& vbCrLf & "Do you want to close the form anyway?",
vbYesNo, "Warning")
If CloseAnyway = vbNo Then DoCmd.CancelEvent
End If
End Sub

I used DoCmd.CancelEvent here because Cancel=True did nothing to stop
the form from closing. (Neither did this method.) Can anyone tell me
what I'm doing wrong? Thanks for your help.

Keith
 
K

kagard

Greetings:

I've set up all my forms to check for data in their respective
controls. I've also provided a command button on each form to close
the form. I have a global variable call

called FormReady...

Sorry.


K
 
G

Guest

I believe the Close method will take place before the Unload method occurs,
so that's probably why your form closes regardless of the response/cancel
event.

Try placing your code in the cmdClose.

BTW, you are missing an End If from what you have typed below.

If you get an error message on the cancel event (like "the form action was
cancelled" or something like that), then put something like this into your
error handling:

Exit_cmdClose:
Exit Sub
Err_cmdClose:
If err.number = 2501 then
resume Exit_cmdClose
Else
msgbox err.description
resume Exit_cmdClose
end if

Of course, you'll need a On Error GoTo Err_cmdClose statement at the
beginning of your routing.

HTH
 

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

Similar Threads


Top