Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to stop a form from closing.

Status
Not open for further replies.

dotolee

Technical User
Jan 27, 2008
134
CA
Hi there. I've posted this before but haven't got an answer yet. Really urgent that i figure this out...
I have subforms embedded in a master form. I don't want them to close the master form until they're finished filling out certain fields on the sub form.

I've tried the following on the master form close event:

Private Sub Form_Close()
'Jan 30. Prevent from closing if key fields missing.
If Me.[Enrolled].Value = "" Then
MsgBox ("You must specify whether this participant has been enrolled or not!")
DoCmd.CancelEvent
Exit Sub
Else
'check form 4
If IsNull(frmInclExclSection4.Controls.Item(6).Value) Then
frmInclExclSection4.SetFocus
MsgBox ("Section 4 hasn't been reviewed.")
DoCmd.CancelEvent

End If
End If

But the DOCMD.CANCELEVENT doesn't stop the form from closing. Where do i have to put this code to prevent the user from saving the record without these key fields?
Thanks.

 
How are ya dotolee . . .

As far as filtering a form (or not) is concerned, [blue]if your closing the form, what does it matter![/blue]

In any case to stop a form from closing, you need to use any event that includes the [blue]Cancel[/blue] arguement.

[blue][tt]Form - Before Insert
Form - Before Update
Form - On Delete
Form - BeforeDeleteConfirm
Form - On Open
Form - On Unload

Control - Before Update
Control - On Exit[/tt][/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
Also faq181-2886
 
Hi TheAceMan1
Thanks for the response. here's my question.
I've been playing around some more with the before_update method.
i've noticed that this method gets called when moving focus from the master form to a sub form. My challenge is that by default, i have no way of putting code in this event handler that I only want to execute when the form is closed. I was thinking of creating a flag of some sort that is set only when the form_close event is run .. but Before_Update is called before form closed!
Just wondering if you have any ideas?
 
dotolee,

Have you tried this one that Ace pointed out?
[blue]Form - On Unload[/blue]

I would imagine it might give you the same results, possibly, or at least get you close. If it works, you would take your current method, and change it to this:

Code:
Private Sub Form_Unload(Cancel as Boolean)
    'Jan 30. Prevent from closing if key fields missing.
    If Me.[Enrolled].Value = "" Then
        MsgBox ("You must specify whether this participant has been enrolled or not!")
        Cancel = True
        Exit Sub
    Else
        'check form 4
        If IsNull(frmInclExclSection4.Controls.Item(6).Value) Then
            frmInclExclSection4.SetFocus
            MsgBox ("Section 4 hasn't been reviewed.")
            Cancel = True        
        End If
    End If
End Sub

The built-in Cancel argument automatically cancels an event from completing - basically, though I'm sure the full definition might be more complex.

--

"If to err is human, then I must be some kind of human!" -Me
 
I was just in the process of trying to move the code to another event with the cancel option.
I'll try playing around with the _Unload first.
Thanks.
 
does this help:
see faq702-2071

.....
I'd rather be surfing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top