Form Closes Even Though Close is Canceled

JamesJoey

Registered User.
Local time
Today, 12:25
Joined
Dec 6, 2010
Messages
642
I'm checkling to see if a form's data is Dirty when the user attempts to close the form.
I'm using the following to cancel the closing of the form when the user presses 'Cancel'.
When the user presses 'Yes the form closes and when the user presses 'No' the form closes.
It worked fine in my other db. But now when I press 'Cancel' the form closes where as it didn't in the other db. The only difference is that the other db the form is a split form with no underlying form. The current db the form is popup and model and called from an underlying form. There is other area in the forms module that sets the bPreventClose (Bollean) to true or false.
Not sure if anyone will need the rest of the code, but let me know if you do.
I've changed the form to split and reset the Popup and Model to no but it didn't help.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    Dim UserResp As Integer
    UserResp = MsgBox("Record(s) have been added or changed." & vbCrLf & "Save the changes?", vbYesNoCancel, "Data Manager")
    Select Case UserResp
        Case vbNo
            bPreventClose = False
            Me.Undo
            Exit Sub
        Case vbCancel
            bPreventClose = True
            Exit Sub
        Case Else
            bPreventClose = False
    End Select
End Sub
 
Last edited:
Unless I'm missing something, or you are, where does Cancel get set to the value of bPreventClose?

I'm assuming its in Form_Unload().

You're probably going to have to use single step mode (F8), and set a Watch, to track the value of bPreventClose to see it if gets changed back to False somewhere.
 
Last edited:
In the form's declaration section:
Code:
Public bPreventClose As Boolean

Actually bPreventClose gets set to True or False.

The form's AfterUpdate it is set to False.
The form's Dirty it is set to True.
The form's Unload:
Code:
If bPreventClose = True Then
        Cancel = True
End If
 
The code seems to be working now.
I imported the forms with the code into another db.
I really haven't the slightest idea why the code is preventing the form closing like it's supposed to.

James
 
Last edited:
Your code is not the code that was originally created. You made an essential change, and that is why it doesn't prevent the form from closing. You should look at the original code and see the difference.

Alternatively, step though the code - see the section How to debug VBA code in http://www.access-programmers.co.uk/forums/showthread.php?t=149429

A clue: your form closes because bPreventClose is False where it counts. Your code set it to false. Step through the code to find out where. ANd then think why that is - and why your BeforeUpdate handler also is faulty.
 
I haven't used stepping through the code yet.
Is it possible to step through a form's code?
 
I jumped the gun on that.
Right now the problem lies in the BeforeUpdate code.

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)

    Dim UserResp As Integer
    UserResp = MsgBox("Record(s) have been added or changed." & vbCrLf & "Save the changes?", vbYesNoCancel, "Data Manager")
    Select Case UserResp
        Case vbNo
            bPreventClose = False
            Me.Undo
            Exit Sub
        Case vbCancel
            bPreventClose = True
            Exit Sub
        Case Else
            bPreventClose = False
    End Select
End Sub
I when I click 'Cancel' on the message box the message box closes and returns me tot he form for editing. Problem is I can't close the form onc eI do this.
I stepped through by placing a breakpoint at the Case vbCancel. Pressing F8 stopped at Exit Sub and went no further.
 
Last edited:
Sorry nbut yuou were at a disadvantage.
I was setting bPreventClose also in the Dirty, AfterUpdate and Unload also which I failed to post:

Code:
Private Sub Form_Dirty(Cancel As Integer)
    bPreventClose = True
End Sub

Code:
Private Sub Form_AfterUpdate()
    bPreventClose = False
End Sub

Code:
Private Sub Form_Unload(Cancel As Integer)
    If bPreventClose = True Then
        Cancel = True
    End If
End Sub

I needed to add:
Code:
bPreventClose = False
after the Cancel=True in the Form_Unload.

Thanks much for the assistance.
It is appreciated,
James
 
I'm, again, using the db that has the code to cancel the close.
It doesn't seem to work now.
In the message box everything works ok except when I click 'Cancel.'

The form closes even though I have cancel = True.

What is wrong with this code?

The Cancel button works ok on a single form.
The one I'm having trouble with is a Split form.

Any help will be appreciated,
James
 

Users who are viewing this thread

Back
Top Bottom