How to cancel a form close event

T

TK

I have a main form that contains a datasheet subform in
addtion to a 'Close' command button. I have code in the
subform's Form_Unload() procedure to confirm that the form
should be closed, but the main form does not seem to
notice when I set the 'Cancel' property of this procedure
to TRUE.

Is there a way to hold the main form open until the
subform procedure has resolved whether to cancel the
Unload, or does a 'Cancel' condition have to arise in the
same form as the DoCmd.Close method that it cancels?


Thanks in advance,

TK
 
K

Ken Snell [MVP]

Because it's really the main form that is open, not the subform, you should
run the "should I close" code in the main form's module. If you want to use
the existing procedure that is in the subform's module, make that subform's
procedure Public and then call it from the main form's Close event
procedure:

Private Sub Form_eventname()
Dim intReply As Integer
Call Me.SubformControlName.Form_Unload(intReply)
If intReply = True Then
Exit Sub
Else
DoCmd.Close acForm, Me.Name
End If
End Sub
 
K

Ken Snell [MVP]

Sorry -- an error in my syntax (memory failed me!):

Private Sub Form_eventname()
Dim intReply As Integer
Call Form_SubformFormName.Form_Unload(intReply)
If intReply = True Then
Exit Sub
Else
DoCmd.Close acForm, Me.Name
End If
End Sub

Note that you substitute SubformFormName with the name of the form that is
the subform (the name of the form that is serving as the Source Object of
the subform control on the main form).
 

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