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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FormUnload interfering with Exit Button

Status
Not open for further replies.
May 5, 2000
168
0
0
US
I want to prompt user to save their data and exit.

I have an exit application button that prompts the user with a vbOK message box asking if they want to save and exit.

In addition, if the user clicks on the form's "X" to exit, I also want to prompt them with the same message box.

The problem is that if they click on the exit button, they get the message box twice because after they click OK on the messagebox it goes to the form UNLOAD event.

Is there a way to get around this.


 
Do you have both events set to fire if they exit the form without saving? In other words in the unload event do you have code to ask to save the file? If so that will be easy to remedy. In the very top of your module add

Private OKToClose as Boolean

Now on the on load event of the form set OKToClose = False

So the goal here will be to get OKToClose = True

If the user uses the Save Object then we want to add code in there to make OKToClose = True

Now on the unload event add

IF OKToClose=False Then
Your code here.

With me so far? The user saved via your command object so we don't need to fire here.
Life's a journey enjoy the ride...

jazzz
 
I have pasted my code below. It doesn't seem to be working correctly. Can you see what I am doing wrong here?


Option Compare Database
Private OKToClose As Boolean
-------------------
Private Sub Form_Load()
OKToClose = False

DoCmd.Maximize
TxtLName.SetFocus
cmdSave.Visible = False

End Sub
----------------------
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_form_Unload

If OKToClose = False Then
Dim Response As Integer

msg1 = "Exit Volunteer Application and save new data?"
Response = MsgBox(msg1, vbYesNo, "Exit Volunteer Application")

If Response = vbYes Then
DoCmd.Quit

ElseIf vbNo Then
Cancel = True
End If

Exit_form_Unload:
Exit Sub
Err_form_Unload:
MsgBox Err.Description
Resume Exit_form_Unload
End If
End Sub
------------------------
Private Sub cmdExit_Click(Cancel As Integer)
On Error GoTo Err_cmdExit_Click

Dim Response As Integer

msg1 = "Exit Volunteer Application and save new data?"
Response = MsgBox(msg1, vbYesNo, "Exit Volunteer Application")

If Response = vbYes Then
OKToClose = True
DoCmd.Quit

ElseIf vbNo Then
Cancel = True
End If

Exit_cmdExitClick:
Exit Sub
Err_cmdExitClick:
MsgBox Err.Description
Resume Exit_cmdExitClick
End Sub
 
You are making life TOO complicated for yourself. All you need is


Code:
Private Sub cmdExit_Click()
On Error GoTo Err_cmdExit_Click

DoCmd.Quit

Exit_cmdExitClick:
    Exit Sub
Err_cmdExitClick:
    MsgBox Err.Description, ,Err.Number
    Resume Exit_cmdExitClick
End Sub



Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Err_form_Unload

Dim Response As Integer

  Response = MsgBox("Exit Volunteer Application and save new data?", vbYesNo, "Exit Volunteer Application")
  
  If Response = vbYes Then
      DoCmd.Quit
      Cancel = False  ' No technically needed - but it helps 'self documentation'
  Else
    Cancel = True
  End If

Exit_form_Unload:
    Exit Sub
Err_form_Unload:
    MsgBox Err.Description, ,Err.number
    Resume Exit_form_Unload
End Sub


'ope-that-'elps.




G LS
accessaceNOJUNK@valleyalley.co.uk
Remove the NOJUNK to use.

Please remember to give helpful posts the stars they deserve!
This makes the post more visible to others in need! :-D

 
Understand I don't know what you are trying to accomplish? So I would use the flag to give us the OK to close the app based on where/what you want. For testing I just wanted to close the form.


Unload Event

Dim Response As Integer
Dim strMessage As String

strMessage = "Exit Volunteer Application and save new data?"

If OKToClose = False Then

Response = MsgBox(strMessage, vbYesNo, "Exit Volunteer Application")

If Response = vbYes Then

ElseIf vbNo Then
Cancel = True
End If
End If


Command Object
Dim Response As Integer
Dim strMessage As String

strMessage = "Exit Volunteer Application and save new data?"
Response = MsgBox(strMessage, vbYesNo, "Exit Volunteer Application")

If Response = vbYes Then
OKToClose = True
DoCmd.Close

ElseIf vbNo Then

End If Life's a journey enjoy the ride...

jazzz
 
little smudge, You are right I do tend to make things too complicated. I had my code like yours to begin with, but it doesn't quite work.

when the user uses the command button exit it invokes
docmd.quit which takes you to the unload form event.

Then if the user clicks cancel on the message box, the app still closes because of the docmd.quit event.

Do you see what I mean?

Jazzz do you see what I mean?



 
Don't fully understand what you are trying to accomplish here. Here is how I handle a controlled closing.

Keep all the code as is except in the unload event I would use just this:
Cancel=Not OKTOCLose

removing all your other code in there except your error handlers.

Now on your form property Set the CloseButton property to No *this is located on the format tab on the properties box* this removes the X and forces the user to use your button only. Name on your command object Save/Close Life's a journey enjoy the ride...

jazzz
 
I think I understand what the original intent was but I'm lost as to why everyone's taking the long way:

Private Sub btnSave/Exit_OnClick()
DoCmd.Close acForm, "Volunteer",acSavePrompt
DoCmd.'Whatever's Next'
End Sub

A Big Fan of "KISS"
Cj
 
Thank you for your help, but here is the situation.

Our QA team requires that there be an Exit button, in addition to the Access closebutton (X).

The user needs to be prompted to save the data with a message box (MsgBox("Exit Volunteer Application and save new data?", vbYesNo, "Exit Volunteer Application")).

If I use the code docmd.quit on the exit button, it takes the user to the unload form event which is good.

The msgbox code is in the Unload Form Event. If the user clicks "No" on the vbYesNo message box in the Unload Form Event, the app still closes because of the docmd.quit in the exit button.

Try this code in one of your apps, you will see what I mean.

I tried using the boolean also, and it still did not work.



 
Well, when I tested my code as posted a few days ago the Cancel = True causes the whole thing to stop any returns the user to the form - which is what you seem to be asking for.

OR .. ..
Are you trying to ask the user if they want to SAVE THE DATA given that they are exiting anyway ?

Do you really need a 2 tier approach

Tier 1 Do you want to save this data Yes/No
Okay - i'll go and do the approapriate thing
NOW Tier 2, do you want to leave the app?
Okay - i'll go and execute that.



The English grammer of the question as posed in the msgbox seems confusing.


G LS
accessaceNOJUNK@valleyalley.co.uk
Remove the NOJUNK to use.

Please remember to give helpful posts the stars they deserve!
This makes the post more visible to others in need! :-D

 
The messagebox is just a formality. I know that the data would be saved if they just exit.

QA requires the message box on all of our applications, VB or Access.

My problem is as I described above with the docmd.quit which forces the formunload and then overrides the cancel = true.

Just try the code and you'll see what I mean.
 
Nobody wants to let this die! I'm sorry I didn't get back sooner but I was out of town. I tried your code and now understand what you are trying to accomplish. You are correct on the sequence of events that fire. All you need to do is add docmd.close on your exit button which will then fire the unload event which in turn will launch your code no matter how the user tries to exit your form.

Thanks for the tip...

Life's a journey enjoy the ride...

jazzz
 
I can't let it die, I'm just thankful that you understand the sequence of events.

I had tried using the code you suggest. It works fine if I say "Yes" to the messagebox, if I say "No" though, I get another Access vbOK messagebox saying, "The close Action was Canceled". How can I suppress this.
 
How you are closing the form is perfectly acceptable. First event to fire is your cmdbutton on which you are closing the form. At which time the unload event fires and here is where you ask what the user want they want to do. Allowing them to back out if they so desire. This is perfectly acceptable. Life's a journey enjoy the ride...

jazzz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top