Weird things happening... Logical explanation ?

F

Frenchie

Hello !

Given a form with an "Exit" button.

On this button "click" event, I have the following code:

blnExitFromButton = True
DoCmd.Quit acQuitSaveAll

--> blnExitFromButton is a module global variable allowing me to know
if the form was closed using the buttom or otherwise (ex: clicking on
the X).


In the "Form_Unload" event procedure, there's the following :

If blnExitFromButton = False Then
If MsgBox("Are you sure you want to exit".... = vbNo Then
Cancel = True
Exit Sub
End If
End If
...


Despite this, if I click on the Exit button, the blnExitFromButton
value upon entering the "Unload" procedure is "False" and the Msgbox
pops.. Moreover, even if I answer "No" to the msgbox, the applications
close anyway..

Looks like a case of corrupted DB.. or... is there something I'm
missing ?


Thanks
 
G

Guest

Not sure why the boolean always evaluates to false. Could you put a watch on
the variable and check it when it changes?

In terms of the MsgBox statement, you don't show all the arguments. In order
to trap for vbNo, you need vbYesNo for the button argument of the msgbox
function.

Barry
 
F

Frenchie

Thanks for your reply.

Concerning the arguments, here's the actual code:

*** code start

Private Sub cmdExit_Click()
Stop
blnExitFromButton = True
Application.Quit acQuitSaveAll

End Sub

Private Sub Form_Unload(Cancel As Integer)

If blnExitFromButton = False Then
If MsgBox("Are you sure you want to close application and exit
Access ?", vbExclamation + vbYesNo, "Exit not invoked from menu") =
vbNo Then
Cancel = True
Exit Sub
End If
End If

' Restore menu & toolbars
'
Dim varItem As Variant
If Not colBars Is Nothing Then
For Each varItem In colBars
DoCmd.ShowToolbar varItem, acToolbarYes
Next varItem
End If
Set colBars = Nothing

End Sub

*** code end

Notice the "stop" statement in the click event. If I single step (F8)
from that point, it wont show the statements in the Unload "event",
even if the MsgBox message is displayed.

The watch variable doesn't work either. It stops following it after
the Application.Quit statement.

Actually, when the MsgBox is displayed, it's like the application is
already closed and the only opened window I have *is* the msgbox.
Wether I answer yes or no doesn't change anything : the msgbox window
closes and there's nothing left.

It's like the "Application.Quit" takes effect before the form's Unload
event.



Dans son message précédent, Barry Gilbert a écrit :
 
D

Dirk Goldgar

Frenchie said:
Thanks for your reply.

Concerning the arguments, here's the actual code:

*** code start

Private Sub cmdExit_Click()
Stop
blnExitFromButton = True
Application.Quit acQuitSaveAll

End Sub

Private Sub Form_Unload(Cancel As Integer)

If blnExitFromButton = False Then
If MsgBox("Are you sure you want to close application and exit
Access ?", vbExclamation + vbYesNo, "Exit not invoked from menu") =
vbNo Then
Cancel = True
Exit Sub
End If
End If

' Restore menu & toolbars
'
Dim varItem As Variant
If Not colBars Is Nothing Then
For Each varItem In colBars
DoCmd.ShowToolbar varItem, acToolbarYes
Next varItem
End If
Set colBars = Nothing

End Sub

*** code end

Notice the "stop" statement in the click event. If I single step (F8)
from that point, it wont show the statements in the Unload "event",
even if the MsgBox message is displayed.

The watch variable doesn't work either. It stops following it after
the Application.Quit statement.

Actually, when the MsgBox is displayed, it's like the application is
already closed and the only opened window I have *is* the msgbox.
Wether I answer yes or no doesn't change anything : the msgbox window
closes and there's nothing left.

It's like the "Application.Quit" takes effect before the form's Unload
event.

Application.Quit and DoCmd.Quit tell Access to close regardless of what
it's doing at the moment. I believe Access will finish up what it's
currently doing, but not start anything new. It's not quite the same as
clicking the "x" button of the Access application.

Is the form involved here one that is supposed to be open at all times?
If so, you could rewrite your code to use the form's Unload event to
quit the application; like this:

'----- start of example code -----
Option Compare Database
Option Explicit

Dim blnExitFromButton As Boolean

Private Sub Command0_Click()

blnExitFromButton = True
DoCmd.Close acForm, Me.Name, acSaveNo

End Sub

Private Sub Form_Unload(Cancel As Integer)

If blnExitFromButton = True Then
Application.Quit acQuitSaveAll
Else
If MsgBox( _
"Are you sure you want to close application " & _
"and exit Access ?", _
vbExclamation + vbYesNo, _
"Exit not invoked from menu") _
= vbNo _
Then
Cancel = True
Else
Application.Quit acQuitSaveAll
End If
End If

End Sub

'----- end of example code -----
 
G

Guest

I see the issue. The Quit method cannot be cancelled in the form's OnUnload
event. If the closure of this form should always result in the closure of the
app, change the flow. Have the command button close the form and use the
form's OnUnload event to close the app:

Private Sub cmdExit_Click()
blnExitFromButton = True
DoCmd.Close
End Sub

Private Sub Form_Unload(Cancel As Integer)
If blnExitFromButton = False Then
If MsgBox("Are you sure you want to close application and exit
Access ?", _
vbExclamation + vbYesNo, "Exit not invoked from menu") = vbNo Then
Cancel = True
Else
Quit
End If
Else
blnExitFromButton = False ' In case it happens again, reset the
boolean.
End If
End Sub

Barry
 
D

Dirk Goldgar

Barry Gilbert said:
I see the issue. The Quit method cannot be cancelled in the form's
OnUnload event. If the closure of this form should always result in
the closure of the app, change the flow. Have the command button
close the form and use the form's OnUnload event to close the app:
[...]

Hah! Beat you by 12 minutes! <g>
 
F

Frenchie

Dirk Goldgar avait énoncé :
Barry Gilbert said:
I see the issue. The Quit method cannot be cancelled in the form's
OnUnload event. If the closure of this form should always result in
the closure of the app, change the flow. Have the command button
close the form and use the form's OnUnload event to close the app:
[...]

Hah! Beat you by 12 minutes! <g>

But you both win my appreciation ! ;o)

Your explanations were clear and the application is now working
correctly.

Thanks..
 
G

Guest

Missed it by that much! :)
How many minutes, though, is my resetting the boolean worth?

Barry

Dirk Goldgar said:
Barry Gilbert said:
I see the issue. The Quit method cannot be cancelled in the form's
OnUnload event. If the closure of this form should always result in
the closure of the app, change the flow. Have the command button
close the form and use the form's OnUnload event to close the app:
[...]

Hah! Beat you by 12 minutes! <g>

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
D

Dirk Goldgar

Frenchie said:
Dirk Goldgar avait énoncé :
Barry Gilbert said:
I see the issue. The Quit method cannot be cancelled in the form's
OnUnload event. If the closure of this form should always result in
the closure of the app, change the flow. Have the command button
close the form and use the form's OnUnload event to close the app:
[...]

Hah! Beat you by 12 minutes! <g>

But you both win my appreciation ! ;o)

Your explanations were clear and the application is now working
correctly.

Thanks..

Great! You're welcome. And to Barry -- I didn't really mean to step on
your toes.
 
D

Dirk Goldgar

Barry Gilbert said:
Missed it by that much! :)
How many minutes, though, is my resetting the boolean worth?

I dunno. Now that you make me go back and look closer, I think your
logic is flawed, though the idea was right. With the code you posted,
if the user clicks the Exit button, the form just closes without closing
the application.
 
F

Frenchie

Dunno, but if the form or the application is closed, I'd be surprised
the boolean will be used again. ;-)

By the way, I observed the following with one modal form : if I
right-click the title bar and select "Design Mode", the Unload Event
fires and the msgbox is displayed as expected.

If I answer "No", the form stays in form mode.

If later I redo the same (click to go in Design mode) there's no
reaction at all. Actually it's not a matter of boolean : the Unload
event doesn't even fire.

Any idea why ?

PS: should I get my starting gun ? lol


Barry Gilbert a formulé ce lundi :
Missed it by that much! :)
How many minutes, though, is my resetting the boolean worth?

Barry

Dirk Goldgar said:
Barry Gilbert said:
I see the issue. The Quit method cannot be cancelled in the form's
OnUnload event. If the closure of this form should always result in
the closure of the app, change the flow. Have the command button
close the form and use the form's OnUnload event to close the app:
[...]

Hah! Beat you by 12 minutes! <g>

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
G

Guest

Ouch. You're right. How about this?

If blnExitFromButton = False Then
If MsgBox("Are you sure you want to close application and exit
Access ?", _
vbExclamation + vbYesNo, "Exit not invoked from menu") = vbNo Then
' Reset this so this branch will be hit if the users clicks the
X again.
blnExitFromButton = False
Cancel = True
Else
Quit
End If
Else
Quit
End If

In terms of trapping design-mode stuff, I would assume you'd prevent your
users from going there anyway.

Barry
 
D

Dirk Goldgar

Barry Gilbert said:
Ouch. You're right. How about this?

If blnExitFromButton = False Then
If MsgBox("Are you sure you want to close application and exit
Access ?", _
vbExclamation + vbYesNo, "Exit not invoked from menu") =
vbNo Then ' Reset this so this branch will be hit if the
users clicks the
X again.
blnExitFromButton = False

I'm not sure why you would need to set blnExitFromButton = False inside
an If condition that has already ensured that the variable is False when
the statement is executed.
In terms of trapping design-mode stuff, I would assume you'd prevent
your users from going there anyway.

In an ideal world, yes -- you'd deliver an MDE or else a secured
database that doesn't give ordinary users design permissions.
 
D

Dirk Goldgar

Frenchie said:
By the way, I observed the following with one modal form : if I
right-click the title bar and select "Design Mode", the Unload Event
fires and the msgbox is displayed as expected.

If I answer "No", the form stays in form mode.

If later I redo the same (click to go in Design mode) there's no
reaction at all. Actually it's not a matter of boolean : the Unload
event doesn't even fire.

Any idea why ?

Not offhand. It doesn't sound like what I'd expect. If I get a little
spare time, I'll check it out.
PS: should I get my starting gun ? lol

LOL
 
G

Guest

Forget about the boolean. I should know better than to try to work on Labor
Day.

Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top