cancel close buttons save

Bagel

Registered User.
Local time
Today, 22:52
Joined
Jun 13, 2003
Messages
15
CAN SOMEONE PLEASE HELP ME?


I HAVE A FORM ON WHICH I HAVE A FIELD THAT MUST HAVE DATA ENTERED INTO IT.

TO CLOSE THE FORM THE USER MUST CLICK THE BUILT IN CLOSE BUTTON. IF THERE IS NO DATA ENTERED INTO THE FIELD THEY ARE ASKED IF THEY WOULD LIKE TO CANCEL OR RETURN TO THE FORM AND ENTER DATA INTO THE FIELD. IF THEY SELECT TO CLOSE THE FORM 'UNDOES' AND EXITS WHICH IS FINE HOWEVER IF THEY WANT TO ENTER DATA INTO THE FIELD THE FORM RETURNS THE ERROR MSG "ACCESS ENCOUNTED ERRORS WHILE TRYING TO SAVE, DO YOU WANT TO CLOSE ANYWHO"
I DIDN'T WANT TO SAVE AND I ASSUME IT IS A BUILT IN PROCEDURE FOR THE CLOSE BUTTON. I DONT WANT TO EDIT THE BUILT IN CLOSE BUTTONS PROCEDURES BUT STOP THIS MESSAGE FROM COMING UP.
MY EVENT IS AS FOLLOWS;

Private Sub Form_BeforeUpdate(Cancel As Integer)
If IsNull(Me.CityName) Or Me.CityName = 0 Then
If MsgBox("You must Enter a City Name, Do you want to Cancel this New City", vbYesNo, "City Name not Entered") = vbNo Then
Cancel = True
Exit Sub
Else
Me.Undo
End If
End If
Exit Sub
End Sub

ANY SUGGESTIONS?
 
Have you considered using a boolean variable public to the form that if the save/update button is clicked then the variable is set to true and then evaluate the varible in your beforeupdate event to see if it is true for update or false to exit the sub.
Code:
'public declaration
dim blnUpdateRec as Boolean

Private Sub cmdClose_OnClick()
    blnUpdateRec=True
End Sub

Private Sub Form_BeforeUpdate(Cancel As Integer)
    if blnUpdateRec =False then exit sub
    If IsNull(Me.CityName) Or Me.CityName = 0 Then 
        If MsgBox("You must Enter a City Name, Do you want to Cancel this New City", vbYesNo, "City Name not Entered") = vbNo Then 
            Cancel = True 
            Exit Sub 
        Else 
            Me.Undo 
        End If 
    End If 

End Sub

I hope this makes sense, and I understood your question?
 
HI CALVIN

THANKS FOR THE REPLY

I HAVE TRIED YOUR SUGGESTION, AS YOU MAY HAVE GUESSED I AM BUT AN AMATEUR (FRUSTRATED) BUT HAVE HAD NO SUCCESS.
ON PRESSING THE CLOSE BUTTON(BUILT IN) I STILL RECIEVE THE SAME MESSAGE "BLAH BLAH ERRORS ENCOUNTED WHILE SAVING", IS THERE NO WAY OF CANCELLING THE EVENT THAT THE CLOSE BUTTON HAS? ANY FURTHER SUGGESTIONS?
 
Boy I blew a gasket on this one, please forget my previous reply.

It looks like regardless of the answer to the msgbox you need to cancel the update event.

so try something like this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.CityName) Or Me.CityName = 0 Then 
        Cancel = True 
        If MsgBox("You must Enter a City Name, Do you want to Cancel this New City", vbYesNo, "City Name not Entered") = vbNo Then 
            Exit Sub 
        Else 
            Me.Undo 
        End If 
    End If 

End Sub
 
You may need to rethink the series of events that take place from clicking on the Close button to the form unloading. Why not trap for updating the record when you click on the Close button before it even gets to the update event?

What does your cmdClose_OnClick event look like?
 
Last edited:
THANKS AGAIN FOR THE REPLY

I DONT HAVE A cmdClose_OnClick EVENT, MAYBE THIS IS MY PROBLEM. I ASSUMED THAT BECAUSE THE CLOSE BUTTON IS BUILT IN RATHER THAN ME CREATING ONE, THERE WOULD NOT BE SUCH AN OPTION, JUST THE ONCLOSE FOR THE FORM.

WHAT DO YOU SUGGEST?


BAGEL
 
Did you try the Cancel=True outside the if statement like I posted last?
 
If we think about it long enough there should be a solution to the way you want to do it but I usually use a custom close button and set the form property Close Button=No (i assume this is a popup form)

if you want to try a custom close button try this:
Code:
Private Sub cmdClose_Click()
    If IsNull(Me.CityName) Or Me.CityName = 0 Then
        If MsgBox("You must Enter a City Name, Do you want to Cancel this New City", vbYesNo, "City Name not Entered") = vbYes Then
            Me.Undo
            DoCmd.Close acForm, Me.Name
        End If
    End If

End Sub
 
IS SOMETHING WRONG WITH YOUR CAPS LOCK?

It's much easier to read a post when you just write in a normal style.

Using upper case lettering can be deemed rude as it is seen as shouting.
 

Users who are viewing this thread

Back
Top Bottom