OnUnload event question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am trying to force our users to close out of an application correctly. I stumbled upon the Upload event that will handle users trying to exit the program. Below is the code I used

Private Sub Form_Unload(Cancel As Integer
' declare an integer to hold the results of our dialo
Dim intResult As Intege
' tell the user they cannot exit this wa
intResult = MsgBox("Please exit the application correctly", vbOKOnly
If intResult = vbOK The
'force user back to exit correctl
Cancel = Tru
End I

The only problem with this code is that it will display the message even if they exit correctly- Now it will let them exit correctly, but they get the message before they are out.

Is there a way for me to check if the user is exiting correctly, and then disable the message if they do

Thank
jrk
 
Have a look where you put the message.
put it after the test

jrk said:
I am trying to force our users to close out of an application correctly.
I stumbled upon the Upload event that will handle users trying to exit the
program. Below is the code I used:
Private Sub Form_Unload(Cancel As Integer)
' declare an integer to hold the results of our dialog
Dim intResult As Integer
' tell the user they cannot exit this way
intResult = MsgBox("Please exit the application correctly", vbOKOnly)
If intResult = vbOK Then
'force user back to exit correctly
Cancel = True
End If

The only problem with this code is that it will display the message even
if they exit correctly- Now it will let them exit correctly, but they get
the message before they are out.
Is there a way for me to check if the user is exiting correctly, and then
disable the message if they do?
 
Declare a variable at the top of your module

eg)
Option Compare Database
Dim bolLetMeOut as Boolean

Set this variable to True, perhaps in the OnClick() event
of the form's 'close' command button?

bolLetMeOut = True

Check the value of this variable before you set Cancel =
True in your example (instead of intResult)

If bolLetMeOut Then
'do nothing
Else
MsgBox "Not so fast!", vbExclamation, "Scumbag Alert"
Cancel = True
End If

-----Original Message-----
I am trying to force our users to close out of an
application correctly. I stumbled upon the Upload event
that will handle users trying to exit the program. Below
is the code I used:
Private Sub Form_Unload(Cancel As Integer)
' declare an integer to hold the results of our dialog
Dim intResult As Integer
' tell the user they cannot exit this way
intResult = MsgBox("Please exit the application correctly", vbOKOnly)
If intResult = vbOK Then
'force user back to exit correctly
Cancel = True
End If

The only problem with this code is that it will display
the message even if they exit correctly- Now it will let
them exit correctly, but they get the message before they
are out.
Is there a way for me to check if the user is exiting
correctly, and then disable the message if they do?
 
If intResult = vbOK Then

'force user back to exit correctly

Msgbox "This message only runs if intResult is true"

Cancel = True

End If
 
But isn't your example the same as: (as long as I have declared bolLetMeOut AND I have set bolLetMeOut = True when the cancel button is clicked

Option Compare Databas
Dim bolLetMeOut As Boolea

...
...
Private Sub btnCancel_Click(
bolLetMeOut = Tru
On Error Resume Nex
Application.Quit acQuitSaveNon

End Su

.....

Private Sub Form_Unload(Cancel As Integer
If bolLetMeOut = True The
'do nothin
Els
MsgBox "Not so fast!", vbExclamation, "Scumbag Alert
Cancel = Tru
End I

Unfortunatly that does not work... It still gives me the Scumbag Alert when I hit the cancel button.
 
Form_Unload will fire before the form unloads, regardless of >how< the user
tries to exit the application. So it is working "as designed".

You need a gloabl variable OK2Exit (or somesuch) in the form. Set it False
in Form_Open. Have the "proper" exit method (command button or whatever) set
it True. Then, in Form_Unload, if OK2Exit is True, just omit your own code &
let the unload proceed. If OK2Exit is False, you know he did not exit
properly, so let your code take over then.

HTH,
TC


jrk said:
I am trying to force our users to close out of an application correctly.
I stumbled upon the Upload event that will handle users trying to exit the
program. Below is the code I used:
Private Sub Form_Unload(Cancel As Integer)
' declare an integer to hold the results of our dialog
Dim intResult As Integer
' tell the user they cannot exit this way
intResult = MsgBox("Please exit the application correctly", vbOKOnly)
If intResult = vbOK Then
'force user back to exit correctly
Cancel = True
End If

The only problem with this code is that it will display the message even
if they exit correctly- Now it will let them exit correctly, but they get
the message before they are out.
Is there a way for me to check if the user is exiting correctly, and then
disable the message if they do?
 
your 'quit' line is in wrong place. Put it in the OnClose
event of your form. If the Unload event is cancelled the
OnClose event won't fire. I didn't see it in your reply,
but I'm assuming that you've also declared 'bolLetMeOut'
as a variable at the top of your module. You must do this
or your Unload event won't know that your command button
set the variable to true. You're almost there :)
-----Original Message-----
I am still getting the message when I try to use the
correct (in this case cancel) exit button. Here is the
cancel button code:
 
Your cancel button should close the form, not quit the
app...
----------------------------------
Private Sub btnCancel_Click()
On Error Resume Next
bolLetMeOut = True
DoCmd.Close acForm, Me.Name
End Sub
----------------------------------

The form's OnClose() event should quit the app...
----------------------------------
Private Sub Form_Close()
DoCmd.Quit acQuitSaveNone
End Sub
----------------------------------

The form's Unload() event is only needed to prevent the
OnClose() event from firing if necessary.
----------------------------------
Private Sub Form_Unload(Cancel As Integer)
If Not bolLetMeOut Then
Cancel = True
MsgBox "Not so fast!", vbExclamation, "Scumbag Alert"
End If
End Sub
----------------------------------


-----Original Message-----
Ok- I have the bolLetMeOut declared, on loading the form, the value is set to false

--------------------------------------
Option Compare Database
Public bolLetMeOut As Boolean
--------------------------------------

--------------------------------------
Private Sub Form_Load()
...
bolLetMeOut = False
...
End Sub
first thing that happens when the cancel button is
clicked, is the bolLetMeOut variable is set to True. If
we take out the app.quit in this section the cancel button
obviously wont work.
----------------------------------
Private Sub btnCancel_Click()
bolLetMeOut = True
On Error Resume Next
End Sub
bolLetMeOut, and handles the event based on it's value.
If they exited correctly, then the application quits, if
they exited incorrectly the application does not, and
displays the scumbag alert.
------------------------------------------------------
Private Sub Form_Unload(Cancel As Integer)
If bolLetMeOut = True Then
Application.Quit acQuitSaveNone
Cancel = False
Else
Cancel = True
MsgBox "Not so fast!", vbExclamation, "Scumbag Alert"
End If

End Sub
like the cancel button is not working correctly, or the
unload event happens before it has a chance to evaluate
bolLetMeOut.
 
Back
Top