Close Code

ECEK

Registered User.
Local time
Today, 17:43
Joined
Dec 19, 2012
Messages
717
Just having trouble in stopping the close when it meets the criteria.
I'm clicking on the X to close the form


Code:
Private Sub Form_Close()
If Me.app_date > #1/1/1900# And action = "Letter" Then
MsgBox "Action cannot be letter. Please amend"
    Exit Sub
End If
End Sub
 
Yes sorry the correct code is:
Code:
Private Sub Form_Close()
If Me.app_date > #1/1/1900# And Me.action = "Letter" Then
MsgBox "Action cannot be letter. Please amend"
    Exit Sub
End If
End Sub

Everything is fine except when I click OK on the message box i want to go back to my form to correct the error.

At the moment when I click OK it still closes the form
 
Not an expert on this and unsure how this interacts with actual close buttons but, if you just click OK it will logically continue what it is doing.

If you get rid of the X and do this with a button you can set a Cancel action to stop what it is doing or an OK action of Form.Close to continue, I think you need to look at VBAYes and VBANo options.

Once you get an ordinary button doing what you want you can then see if an actual close button can be interrupted (or somebody wiser may be able to help).
 
Use the form's unload event and set cancel = true.

Code:
Private Sub Form_Unload(Cancel As Integer)
    If Not IsDate(app_date) Then
        MsgBox "enter a date you doofus"
        Cancel = True
    Else
        If app_date > #1/1/1900# And action = "Letter" Then
            Cancel = True
            MsgBox "Action cannot be letter. Please amend"
        Exit Sub
    End If
End If
End Sub
 
Try something like this

Private Sub Form_Unload(Cancel As Integer)
If Me.app_date > #1/1/1900# And Me.action = "Letter" Then
MsgBox "Action cannot be letter. Please amend"
Cancel=true
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom